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

#ifndef __LOCK

#define __LOCK

 

class Lock

{

public:

 

virtual void lock() = 0;

 

virtual void unlock() = 0;

};

 

#endif

 

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

 

#include <windows.h>

#include "Lock.hpp"

 

#ifndef _EVENT_LOCK

#define _EVENT_LOCK

 

class EventLock : public Lock

{

 

private: 

 

HANDLE event;

 

public:

EventLock();

 

EventLock(const char* name);

 

virtual void lock();

 

void lock(HANDLE event);

 

virtual void unlock();

 

void unlock(HANDLE event);

 

HANDLE getEvent();

};

 

#endif

 

 

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

 

 

 

#include <stdlib.h>

#include <stdio.h>

#include <windows.h>

#include "log.h"

#include "EventLock.hpp"

 

EventLock::EventLock()

{

this->event = CreateEvent(NULL, FALSE, TRUE, NULL);

if (this->event == NULL) 

{

DWORD error = GetLastError();

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

}

debug("EventLock()");

}

 

EventLock::EventLock(const char* name) 

{

this->event = CreateEvent(NULL, FALSE, FALSE, name);

if (this->event == NULL) 

{

DWORD error = GetLastError();

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

}

else 

{

DWORD error = GetLastError();

if (error == ERROR_ALREADY_EXISTS) 

{

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

}

}

}

 

void EventLock::lock()

{

lock(this->event);

}

 

void EventLock::lock(HANDLE event)

{

DWORD e = WaitForSingleObject(event, INFINITE);

if (e == WAIT_FAILED) 

{

DWORD error = GetLastError();

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

}

else if(e == WAIT_TIMEOUT) 

{

debug("wait time out");

}

else if(e == WAIT_ABANDONED) 

{

debug("wait abandoned");

}

}

 

void EventLock::unlock()

{

this->unlock(this->event);

}

 

void EventLock::unlock(HANDLE event) 

{

BOOL isr = SetEvent(this->event);

if (! isr) 

{

DWORD error = GetLastError();

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

}

}

 

HANDLE EventLock::getEvent()

{

return this->event;

}

 

 

 

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

 

#define _WIN32_WINNT 0x0400

#include <stdio.h>

#include <windows.h>

#include "../Lock.hpp"

#include "../EventLock.hpp"

#include "../Thread.hpp"

 

 

#ifndef TEST_EVENT_LOCK_MESSAGE_DESTINATION

#define TEST_EVENT_LOCK_MESSAGE_DESTINATION

 

class TestEventLockMessageDestination 

{

private: 

Lock *lock;

//HANDLE hEvent;

int i;

 

public:

TestEventLockMessageDestination()

{

lock = new EventLock();

//hEvent = CreateEvent(NULL, FALSE, FALSE, NULL);

i = 0;

}

 

void add()

{

lock->lock();

i++;

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

/*

if (i > 0) 

{

SetEvent(hEvent);

}

*/

lock->unlock();

 

}

 

void reduce()

{

lock->lock();

/*

if (i <= 0) 

{

ResetEvent(hEvent);

WaitForSingleObject(hEvent, INFINITE);

}

*/

 

i--;

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

 

lock->unlock();

}

};

#endif

 

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

 

#include <stdio.h>

#include "../Thread.hpp"

#include "TestEventLockMessageDestination.hpp"

 

#ifndef TEST_EVENT_LOCK_MESSAGE_PRODUCER

#define TEST_EVENT_LOCK_MESSAGE_PRODUCER

 

class TestEventLockMessageProducer : public Thread 

{

private:

TestEventLockMessageDestination *destination;

 

public: 

TestEventLockMessageProducer()

{

this->destination = NULL;

}

 

void registerProducer(TestEventLockMessageDestination *destination) 

{

this->destination = destination;

}

 

void run()

{

while (1) 

{

if (destination != NULL) 

{

destination->add();

}

Sleep(5000);

}

}

};

#endif

 

 

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

 

 

#include <stdio.h>

#include "../Thread.hpp"

#include "TestEventLockMessageDestination.hpp"

 

#ifndef TEST_EVENT_LOCK_MESSAGE_CONSUMER

#define TEST_EVENT_LOCK_MESSAGE_CONSUMER

 

class TestEventLockMessageConsumer : public Thread 

{

private:

TestEventLockMessageDestination *destination;

 

 

public: 

TestEventLockMessageConsumer()

{

this->destination = NULL;

}

 

void registerConsumer(TestEventLockMessageDestination *destination) 

{

this->destination = destination;

}

 

 

void run() 

{

while (1)

{

if (destination != NULL) 

{

this->destination->reduce();

}

Sleep(500);

}

}

};

#endif

 

 

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

 

 

#include "TestEventLockMessageDestination.hpp"

#include "TestEventLockMessageProducer.hpp"

#include "TestEventLockMessageConsumer.hpp"

 

void main()

{

///*

TestEventLockMessageDestination *destination = new TestEventLockMessageDestination();

 

TestEventLockMessageProducer *producer = new TestEventLockMessageProducer();

producer->registerProducer(destination);

producer->start();

 

TestEventLockMessageConsumer *consumer = new TestEventLockMessageConsumer();

consumer->registerConsumer(destination);

consumer->start();

 

/*

while(1)

{

Sleep(2000);

}

*/

ExitThread(0);

 

 

/*

DWORD threadID;

CreateThread(NULL, 0, Callback1, (LPVOID) NULL, 0, &threadID);

printf("Thread %ld started...\n", threadID);

 

CreateThread(NULL, 0, Callback2, (LPVOID) NULL, 0, &threadID);

printf("Thread %ld started...\n", threadID);

 

ExitThread(0);

*/

}

 

你可能感兴趣的:(windows)