用c++简单的封装线程c中互斥锁
来源:互联网 发布:工业控制网络视频 编辑:程序博客网 时间:2024/06/05 16:27
#include <iostream>#include <pthread.h>#include <stdio.h>//用c++简单的封装互斥锁class LOCK{private:pthread_mutex_t mutex;public:LOCK();virtual ~LOCK();void lock_t();void unlock_t();void trylock_t();};LOCK::LOCK(){pthread_mutex_init(&mutex, NULL); //在构造函数中直接初始化互斥锁}LOCK::~LOCK(){}void LOCK::lock_t(){pthread_mutex_lock(&mutex);}void LOCK::unlock_t(){pthread_mutex_unlock(&mutex);}void LOCK::trylock_t(){pthread_mutex_trylock(&mutex);}//全局变量int a = 0;//创建锁对象LOCK lock;//线程回调函数void * thread1(void * arg){while(1){printf("I am thread1\n");lock.lock_t();a++;printf("thread1-a === %d\n",a);lock.unlock_t();sleep(1);}}void * thread2(void *arg){while(1){printf("I am thread2\n");lock.lock_t();a++;printf("thread2-a === %d\n",a);lock.unlock_t();sleep(2);}}int main(int argc,char * argv[]){//创建两个线程pthread_t test1; pthread_t test2;pthread_create(&test1,NULL,thread1,NULL);pthread_create(&test2,NULL,thread2,NULL);pthread_join(test1,NULL);pthread_join(test2,NULL);return 0;}
阅读全文
2 0
- 用c++简单的封装线程c中互斥锁
- 一个通用简单线程池实现的初步封装(C语言)
- 简单易用的mysql c api 封装类
- curl的简单封装(c版本)
- C语言实现简单的日志封装
- C语言封装的轻量线程环境 Protothreads
- C入门:简单的win32线程
- 简单Linux C线程池的实现
- 最简单的C线程池
- [C++]_[线程池的简单实现]
- c语言线程池的简单实现
- C#Thread 线程的简单使用
- C语言实现简单的线程池
- 简单C语言线程池的编写
- 简单的工作者线程封装
- objective-c一个简单的异步下载的封装
- 关于一个简单shellcode封装成C语言的形式
- C语言封装线程与日志类
- 关于js对象键顺序的更改
- 通俗理解KMP字符串匹配算法
- 494. Target Sum
- Java并发编程(三)——线程的状态
- Linux CRC校验码
- 用c++简单的封装线程c中互斥锁
- PAT (Advanced Level) Practise 1120 Friend Numbers (20)
- C# ACCESS数据库操作类
- POJ2348 UVa10368 HDU1525 Euclid's Game【博弈】
- 144. Binary Tree Preorder Traversal
- Counting Bits
- linux --学习1
- ACache源码分析
- You called this URL via POST, but the URL doesn't end in a slash and you hav。。。