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

 

}

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值