Windows下C/C++ 多线程同步(mutex)

#ifndef __LOCK

#define __LOCK

 

class Lock

{

public:

 

virtual void lock() = 0;

 

virtual void unlock() = 0;

};

 

#endif

 

=======================================

 

#include <windows.h>

#include "Lock.hpp"

 

#ifndef _MUTEX_LOCK

#define _MUTEX_LOCK

 

class MutexLock : public Lock

{

 

private: 

 

HANDLE mutex;

 

public:

MutexLock();

 

MutexLock(const char* name);

 

void lock();

 

void lock(HANDLE mutex);

 

void unlock();

 

void unlock(HANDLE mutex);

 

HANDLE getMutex();

};

 

#endif

 

========================================

 

#include <stdlib.h>

#include <stdio.h>

#include <windows.h>

#include "log.h"

#include "MutexLock.hpp"

 

MutexLock::MutexLock()

{

this->mutex = CreateMutex(NULL, FALSE, NULL);

if (this->mutex == NULL) 

{

DWORD error = GetLastError();

warn("create mutex error %d", error);

}

debug("MutexLock()");

}

 

MutexLock::MutexLock(const char* name) 

{

this->mutex = CreateMutex(NULL, FALSE, name);

if (this->mutex == NULL) 

{

DWORD error = GetLastError();

warn("create mutex error %d", error);

}

else 

{

DWORD error = GetLastError();

if (error == ERROR_ALREADY_EXISTS) 

{

debug("mutex name %s already exists, use the existed mutex instead.", name);

}

}

}

 

void MutexLock::lock()

{

lock(this->mutex);

}

 

void MutexLock::lock(HANDLE mutex)

{

DWORD event = WaitForSingleObject(mutex, INFINITE);

if (event == WAIT_FAILED) 

{

DWORD error = GetLastError();

warn("wait error %d", error);

}

else if(event == WAIT_TIMEOUT) 

{

debug("wait time out");

}

else if(event == WAIT_ABANDONED) 

{

debug("wait abandoned");

}

}

 

void MutexLock::unlock()

{

this->unlock(this->mutex);

}

 

void MutexLock::unlock(HANDLE mutex) 

{

BOOL isr = ReleaseMutex(this->mutex);

if (! isr) 

{

DWORD error = GetLastError();

warn("release mutex error %d", error);

}

}

 

HANDLE MutexLock::getMutex()

{

return this->mutex;

}

 

========================================

 

#define _WIN32_WINNT 0x0400

#include <stdio.h>

#include <windows.h>

#include "../MutexLock.hpp"

#include "../Thread.hpp"

 

 

#ifndef MESSAGE_DESTINATION

#define MESSAGE_DESTINATION

 

class MessageDestination 

{

private: 

MutexLock lock;

 

int i;

 

public:

MessageDestination()

{

i = 0;

}

 

void add()

{

lock.lock();

i++;

printf("[Producer] message %d\n", this->i);

lock.unlock();

 

}

 

void reduce()

{

lock.lock();

i--;

printf("[Consumer] message %d\n", i);

lock.unlock();

}

};

#endif

 

===========================================

 

#include <stdio.h>

#include "../MutexLock.hpp"

#include "../Thread.hpp"

#include "MessageDestination.hpp"

 

class MessageConsumer : public Thread 

{

private:

MessageDestination *destination;

 

 

public: 

MessageConsumer()

{

this->destination = NULL;

}

 

void registerConsumer(MessageDestination *destination) 

{

this->destination = destination;

}

 

 

void run() 

{

while (1)

{

if (destination != NULL) 

{

this->destination->reduce();

}

//Sleep(500);

}

}

};

 

=========================================

 

#include <stdio.h>

#include "../MutexLock.hpp"

#include "../Thread.hpp"

#include "MessageDestination.hpp"

 

class MessageProducer : public Thread 

{

private:

MessageDestination *destination;

 

public: 

MessageProducer()

{

this->destination = NULL;

}

 

void registerProducer(MessageDestination *destination) 

{

this->destination = destination;

}

 

void run()

{

while (1) 

{

if (destination != NULL) 

{

destination->add();

}

//Sleep(5000);

}

}

};

 

==========================================

 

#include "MessageDestination.hpp"

#include "MessageProducer.hpp"

#include "MessageConsumer.hpp"

 

void main()

{

///*

MessageDestination *destination = new MessageDestination();

 

MessageProducer *producer = new MessageProducer();

producer->registerProducer(destination);

producer->start();

 

MessageConsumer *consumer = new MessageConsumer();

consumer->registerConsumer(destination);

consumer->start();

 

 

ExitThread(0);

 

}

 

你可能感兴趣的:(windows)