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);

*/

}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值