pthread_mutex_t封装
来源:互联网 发布:奥格斯堡同盟 知乎 编辑:程序博客网 时间:2024/06/06 00:51
//Mutex.cpp
#include <pthread.h>
#include <iostream>
usingnamespace std;
class ThreadMutex
{
public:
ThreadMutex()
{
pthread_mutex_init(&mtx, NULL);
}
~ThreadMutex()
{
pthread_mutex_destroy( &mtx );
}
inline void Lock()
{
pthread_mutex_lock( &mtx );
}
inline void UnLock()
{
pthread_mutex_unlock( &mtx );
}
private:
pthread_mutex_t mtx;
};
//以下为测试用例
ThreadMutex g_Mutex;
void*PrintMsg(void*lpPara)
{
char*msg = (char*)lpPara;
g_Mutex.Lock();
for(int i=0; i< 5; i++ )
{
cout << msg<< endl;
sleep( 1 );
}
g_Mutex.UnLock();
return NULL;
}
int main()
{
pthread_t t1,t2;
//创建两个工作线程,第1个线程打印10个1,第2个线程打印10个2。
pthread_create( &t1, NULL,&PrintMsg, (void*)"First print thread" );
pthread_create( &t2, NULL,&PrintMsg, (void*)"Second print thread" );
//等待线程结束
pthread_join( t1, NULL);
pthread_join( t2, NULL);
return0;
}
#include <pthread.h>
#include <iostream>
usingnamespace std;
class ThreadMutex
{
public:
ThreadMutex()
{
pthread_mutex_init(&mtx, NULL);
}
~ThreadMutex()
{
pthread_mutex_destroy( &mtx );
}
inline void Lock()
{
pthread_mutex_lock( &mtx );
}
inline void UnLock()
{
pthread_mutex_unlock( &mtx );
}
private:
pthread_mutex_t mtx;
};
//以下为测试用例
ThreadMutex g_Mutex;
void*PrintMsg(void*lpPara)
{
char*msg = (char*)lpPara;
g_Mutex.Lock();
for(int i=0; i< 5; i++ )
{
cout << msg<< endl;
sleep( 1 );
}
g_Mutex.UnLock();
return NULL;
}
int main()
{
pthread_t t1,t2;
//创建两个工作线程,第1个线程打印10个1,第2个线程打印10个2。
pthread_create( &t1, NULL,&PrintMsg, (void*)"First print thread" );
pthread_create( &t2, NULL,&PrintMsg, (void*)"Second print thread" );
//等待线程结束
pthread_join( t1, NULL);
pthread_join( t2, NULL);
return0;
}
- pthread_mutex_t封装
- pthread_mutex_t封装
- pthread_mutex_t封装
- pthread_mutex_t封装
- POSIX pthread_mutex_t 的简单封装
- pthread_mutex_t封装的读优先锁
- pthread_mutex_t锁
- 互斥锁pthread_mutex_t
- 【linux】pthread_mutex_t
- 互斥锁--pthread_mutex_t
- Pthread_mutex_t锁
- pthread_mutex_t 互斥锁
- pthread_mutex_t死锁
- pthread_mutex_t锁
- pthread_mutex_t和pthread_cond_t
- 关于pthread_mutex_t的初始化
- 互斥锁pthread_mutex_t的使用
- 互斥锁pthread_mutex_t的使用
- 经典的位运算实例(转)
- 有关Windows和Linux双系统环境如何正确卸载Linux系统的方法总结
- 汇编指令的英文全称
- UESTC Training for Graph Theory——D、Distance Queries
- hdu1166依旧是大山跑线段树题
- pthread_mutex_t封装
- 总结4种线程中操作UI界面的方法
- IVR语音导航
- 关于Eclipse代码补全的一点小技巧
- java concurrency基础
- UESTC Training for Graph Theory——E、Distance Queries
- c类型总结
- 菜鸟与大神成员的区别
- UESTC Training for Graph Theory——F、Instantaneous Transference