c++实现加锁解锁

c++编程实现:

/*
 * Filename: Mutex.h
 *                
 * Version:       
 * Author:        haha <haha@126.com>
 * Created at:    Tue Oct 13 09:14:19 2015
 *                
 * Description:   
 *
 */

#include <cstdio>
#include <cstring>
#include <string>
#include <cstdlib>
#include <pthread.h>
#include <iostream>

class Mutex
{
public:
    Mutex();
    virtual ~Mutex();

	void Lock();
	void UnLock();
private:
friend class CondVar;
	pthread_mutex_t m_mutex;
	Mutex(const Mutex&);
	void operator=(const Mutex&);
};

class CondVar
{
public:
    explicit CondVar(Mutex* mu);
    virtual ~CondVar();
	
	void Wait();
	void Signal();
	void SignalAll();
private:
	pthread_cond_t m_cond;
	Mutex *m_mu;
};


#include "Mutex.h"
#include <errno.h>

static void MutexErr(const char* err, int res)
{
	if(res != 0)
	{
		fprintf(stderr, "pthread %s: %s\n", err, strerror(res));
		return;
	}
}

Mutex::Mutex()
{
	MutexErr("init mutex", pthread_mutex_init(&m_mutex, NULL));
}

Mutex::~Mutex()
{
	MutexErr("destroy mutex", pthread_mutex_destroy(&m_mutex));
}

void Mutex::Lock()
{
	MutexErr("lock mutex", pthread_mutex_lock(&m_mutex));
}

void Mutex::UnLock()
{
	MutexErr("unlock mutex", pthread_mutex_unlock(&m_mutex));
}

CondVar::CondVar(Mutex* mu):m_mu(mu)
{
	MutexErr("init cond", pthread_cond_init(&m_cond, NULL));
}

CondVar::~CondVar()
{
	MutexErr("destroy cond", pthread_cond_destroy(&m_cond));
}

void CondVar::Wait()
{
	MutexErr("wait cond", pthread_cond_wait(&m_cond, &m_mu->m_mutex));
}

void CondVar::Signal()
{
	MutexErr("signal cond", pthread_cond_signal(&m_cond));
}

void CondVar::SignalAll()
{
	MutexErr("broadcast cond", pthread_cond_broadcast(&m_cond));
}

测试:

#include "Mutex.h"

static int num = 0;
Mutex mu;
CondVar cond(&mu);

void *recvsignal(void *arg)
{
	printf("the pid %lld\n", (long long)pthread_self());

	mu.Lock();
	while(num < 30)
	{
		cond.Wait();		
	}
	mu.UnLock();
	printf("the function exit\n");
}
int main(int argc, char *argv[])
{

	pthread_t pid;
	pthread_create(&pid, NULL, (void*(*)(void*))recvsignal, NULL);

	for(;;)
	{
		mu.Lock();
		num += 1;
		mu.UnLock();
		if(num > 30)
		{		
			cond.Signal();			
			break;
		}
		usleep(500000);
	}
	
    return 0;
}

编译:

g++ -g -o testmutex Mutex.h Mutex.cpp testmutex.cpp -lpthread


最新实现代码,添加CAS锁:

#include <cstdlib>  
#include <pthread.h>  
#include <errno.h>
#include <unistd.h>
#include <cstdio>
#include <string.h>
class Mutex  
{  
public:  
    Mutex();  
    virtual ~Mutex();  
  
    void Lock();  
    void UnLock();  
private:  
friend class CondVar;  
    pthread_mutex_t m_mutex;  
    Mutex(const Mutex&);  
    void operator=(const Mutex&);  
};  
  
class CondVar  
{  
public:  
    explicit CondVar(Mutex* mu);  
    virtual ~CondVar();  
      
    void Wait();  
    void Signal();  
    void SignalAll();  
private:  
    pthread_cond_t m_cond;  
    Mutex *m_mu;  
};  

class CASLock
{
public:
    CASLock(void* lock);
    virtual ~CASLock();
    void Lock();
	void UnLock();
	bool TryLock();
private:
	void* m_lock;
};

static void MutexErr(const char* err, int res)  
{  
    if(res != 0)  
    {  
        fprintf(stderr, "pthread %s: %s\n", err, strerror(res));  
        return;  
    }  
}  
  
Mutex::Mutex()  
{  
    MutexErr("init mutex", pthread_mutex_init(&m_mutex, NULL));  
}  
  
Mutex::~Mutex()  
{  
    MutexErr("destroy mutex", pthread_mutex_destroy(&m_mutex));  
}  
  
void Mutex::Lock()  
{  
    MutexErr("lock mutex", pthread_mutex_lock(&m_mutex));  
}  
  
void Mutex::UnLock()  
{  
    MutexErr("unlock mutex", pthread_mutex_unlock(&m_mutex));  
}  
  
CondVar::CondVar(Mutex* mu):m_mu(mu)  
{  
    MutexErr("init cond", pthread_cond_init(&m_cond, NULL));  
}  
  
CondVar::~CondVar()  
{  
    MutexErr("destroy cond", pthread_cond_destroy(&m_cond));  
}  
  
void CondVar::Wait()  
{  
    MutexErr("wait cond", pthread_cond_wait(&m_cond, &m_mu->m_mutex));  
}  
  
void CondVar::Signal()  
{  
    MutexErr("signal cond", pthread_cond_signal(&m_cond));  
}  
  
void CondVar::SignalAll()  
{  
    MutexErr("broadcast cond", pthread_cond_broadcast(&m_cond));  
}

CASLock::CASLock(void* lock):m_lock(lock){}
CASLock::~CASLock(){}

void CASLock::Lock()
{
	while(!__sync_bool_compare_and_swap((int*)m_lock, 0, 1))
	{
		sched_yield();
	}
}

void CASLock::UnLock()
{
	*((int*)m_lock) = 0;
}

bool CASLock::TryLock()
{
	if(__sync_bool_compare_and_swap((int*)m_lock, 0, 1))
		return 0;
	else
		return -1;
}




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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值