博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
信号量、Windows事件实现线程同步
阅读量:6040 次
发布时间:2019-06-20

本文共 3648 字,大约阅读时间需要 12 分钟。

1 #include 
2 #include
3 #include
4 5 using namespace std; 6 7 HANDLE sem_add, sem_subtract; 8 9 int val(0);10 11 unsigned int __stdcall add(void* lpa)12 {13 printf("add\n");14 while (true)15 {16 WaitForSingleObject(sem_add, INFINITE);17 ++val;18 printf("add: %d\n", val);19 ReleaseSemaphore(sem_subtract, 1, NULL);20 }21 22 //return 999;23 }24 25 unsigned int __stdcall subtract(void* lpa)26 { 27 printf("subtract\n");28 while (true)29 {30 WaitForSingleObject(sem_subtract, INFINITE);31 --val;32 printf("subtract: %d\n", val);33 ReleaseSemaphore(sem_add, 1, NULL);34 }35 36 //return 999;37 }38 39 int main(int argc, char ** argv)40 {41 sem_add = CreateSemaphore(NULL, 1, 10, NULL);42 43 sem_subtract = CreateSemaphore(NULL, 0, 10, NULL);44 45 //ReleaseSemaphore();46 printf("[%d]: sem init 0\n", __LINE__);47 48 HANDLE hThr;49 hThr = (HANDLE)_beginthreadex(NULL, 0, add, NULL, 0, NULL);50 51 HANDLE hThr2;52 hThr2 = (HANDLE)_beginthreadex(NULL, 0, subtract, NULL, 0, NULL);53 54 55 WaitForSingleObject(hThr, INFINITE);56 WaitForSingleObject(hThr2, INFINITE);57 58 return 0;59 }

 

 

1 #include 
2 #include
3 #include
4 5 using namespace std; 6 7 int val(0); 8 9 sem_t sem_add, sem_subtract;10 11 void * addFun(void * arg)12 {13 while(true)14 {15 sem_wait(&sem_add);16 ++val;17 printf("addFun: %d\n", val);18 sem_post(&sem_subtract);19 }20 }21 22 void * subtractFun(void * arg)23 {24 while(true)25 {26 sem_wait(&sem_subtract);27 --val;28 printf("subtractFun: %d\n", val);29 sem_post(&sem_add);30 }31 }32 33 int main(int argc, char **argv)34 {35 int ret(0);36 sem_init(&sem_add, 0, 10);37 sem_init(&sem_subtract, 0, 0);38 // printf("[%d]: %d\n", __LINE__, sem_val);39 40 pthread_t t_id;41 ret = pthread_create(&t_id, NULL, addFun, NULL);42 43 pthread_t t_id2;44 ret = pthread_create(&t_id2, NULL, subtractFun, NULL);45 46 pthread_join(t_id, NULL);47 pthread_join(t_id2, NULL);48 49 return 0;50 }

 

 

1 #include 
2 #include
3 #include
4 5 using namespace std; 6 7 //HANDLE sem_add, sem_subtract; 8 HANDLE eve_add, eve_subtract; 9 10 11 int val(0);12 13 unsigned int __stdcall add(void* lpa)14 {15 printf("add\n");16 while (true)17 {18 WaitForSingleObject(eve_add, INFINITE);19 //ResetEvent(eve_add);20 ++val;21 printf("add: %d\n", val);22 SetEvent(eve_subtract);23 }24 }25 26 unsigned int __stdcall subtract(void* lpa)27 {28 printf("subtract\n");29 while (true)30 {31 WaitForSingleObject(eve_subtract, INFINITE);32 //ResetEvent(eve_subtract);33 --val;34 printf("subtract: %d\n", val); 35 SetEvent(eve_add);36 }37 }38 39 int main(int argc, char ** argv)40 {41 eve_add = CreateEvent(NULL, false, TRUE, NULL);42 43 eve_subtract = CreateEvent(NULL, false, FALSE, NULL);44 45 printf("[%d]: sem init 0\n", __LINE__);46 47 HANDLE hThr;48 hThr = (HANDLE)_beginthreadex(NULL, 0, add, NULL, 0, NULL);49 50 HANDLE hThr2;51 hThr2 = (HANDLE)_beginthreadex(NULL, 0, subtract, NULL, 0, NULL);52 53 54 WaitForSingleObject(hThr, INFINITE);55 WaitForSingleObject(hThr2, INFINITE);56 57 return 0;58 }

 

转载于:https://www.cnblogs.com/itpoorman/p/5317963.html

你可能感兴趣的文章
js正則表達式语法
查看>>
Android 测试工具集02
查看>>
【CF】148D Bag of mice
查看>>
Wi-Fi万能钥匙:说是破解,其实有危险(转)
查看>>
微软历史最高市值是多少?
查看>>
Boost中的智能指针(转)
查看>>
EntityFramework 实体拆分和表拆分
查看>>
web应用程序访问串口
查看>>
简介数据库日志文件的增长
查看>>
Weblogic
查看>>
Don't Repeat Yourself (不要重复你自己)
查看>>
json \u unicode字符串转化 c++
查看>>
基于HTML5 SVG和CSS3炫酷蹦床式图片切换特效
查看>>
C语言指针的初始化和赋值
查看>>
AsyncTask測试多任务
查看>>
OpenGL------三维变换
查看>>
Android Studio系列教程一--下载与安装
查看>>
Python 变量类型
查看>>
CentOS6无法本地登陆,ssh远程登陆没问题
查看>>
总结 xib
查看>>