信号量、Windows事件实现线程同步
2016-03-25 01:12
393 查看
#include <stdio.h> #include <Windows.h> #include <process.h> using namespace std; HANDLE sem_add, sem_subtract; int val(0); unsigned int __stdcall add(void* lpa) { printf("add\n"); while (true) { WaitForSingleObject(sem_add, INFINITE); ++val; printf("add: %d\n", val); ReleaseSemaphore(sem_subtract, 1, NULL); } //return 999; } unsigned int __stdcall subtract(void* lpa) { printf("subtract\n"); while (true) { WaitForSingleObject(sem_subtract, INFINITE); --val; printf("subtract: %d\n", val); ReleaseSemaphore(sem_add, 1, NULL); } //return 999; } int main(int argc, char ** argv) { sem_add = CreateSemaphore(NULL, 1, 10, NULL); sem_subtract = CreateSemaphore(NULL, 0, 10, NULL); //ReleaseSemaphore(); printf("[%d]: sem init 0\n", __LINE__); HANDLE hThr; hThr = (HANDLE)_beginthreadex(NULL, 0, add, NULL, 0, NULL); HANDLE hThr2; hThr2 = (HANDLE)_beginthreadex(NULL, 0, subtract, NULL, 0, NULL); WaitForSingleObject(hThr, INFINITE); WaitForSingleObject(hThr2, INFINITE); return 0; }
#include <stdio.h> #include <semaphore.h> #include <pthread.h> using namespace std; int val(0); sem_t sem_add, sem_subtract; void * addFun(void * arg) { while(true) { sem_wait(&sem_add); ++val; printf("addFun: %d\n", val); sem_post(&sem_subtract); } } void * subtractFun(void * arg) { while(true) { sem_wait(&sem_subtract); --val; printf("subtractFun: %d\n", val); sem_post(&sem_add); } } int main(int argc, char **argv) { int ret(0); sem_init(&sem_add, 0, 10); sem_init(&sem_subtract, 0, 0); // printf("[%d]: %d\n", __LINE__, sem_val); pthread_t t_id; ret = pthread_create(&t_id, NULL, addFun, NULL); pthread_t t_id2; ret = pthread_create(&t_id2, NULL, subtractFun, NULL); pthread_join(t_id, NULL); pthread_join(t_id2, NULL); return 0; }
#include <stdio.h> #include <Windows.h> #include <process.h> using namespace std; //HANDLE sem_add, sem_subtract; HANDLE eve_add, eve_subtract; int val(0); unsigned int __stdcall add(void* lpa) { printf("add\n"); while (true) { WaitForSingleObject(eve_add, INFINITE); //ResetEvent(eve_add); ++val; printf("add: %d\n", val); SetEvent(eve_subtract); } } unsigned int __stdcall subtract(void* lpa) { printf("subtract\n"); while (true) { WaitForSingleObject(eve_subtract, INFINITE); //ResetEvent(eve_subtract); --val; printf("subtract: %d\n", val); SetEvent(eve_add); } } int main(int argc, char ** argv) { eve_add = CreateEvent(NULL, false, TRUE, NULL); eve_subtract = CreateEvent(NULL, false, FALSE, NULL); printf("[%d]: sem init 0\n", __LINE__); HANDLE hThr; hThr = (HANDLE)_beginthreadex(NULL, 0, add, NULL, 0, NULL); HANDLE hThr2; hThr2 = (HANDLE)_beginthreadex(NULL, 0, subtract, NULL, 0, NULL); WaitForSingleObject(hThr, INFINITE); WaitForSingleObject(hThr2, INFINITE); return 0; }
相关文章推荐
- kernel logo 更换
- HDU 1875 畅通工程再续
- ST_chapter1
- 关于gitlab、github的使用
- uva1220Party at Hali-Bula(树形dp)
- 创业维艰--最艰难的时候
- 避免误删文件:Linux回收站机制
- PAT1023. Have Fun with Numbers (20)
- realloc、malloc、new 时进程直接退出的原因
- ACM学习经验
- 第16课:RDD实战学习笔记
- Objective-C与JavaScript交互的那些事
- 一个类实现一个接口,可以重写接口中方法的参数类型吗?
- Linux进程管理之“四大名捕”
- iOS学习路线图
- 链式(响应式)编程的简单理解
- Java设计模式之“简单工厂模式”
- Python入门 之 dict
- 深入Java虚拟机(4)——网络移动性
- C++之:引用