信号量、Windows事件实现线程同步

 1 #include <stdio.h>
 2 #include <Windows.h>
 3 #include <process.h>
 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 <stdio.h>
 2 #include <semaphore.h>
 3 #include <pthread.h>
 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 <stdio.h>
 2 #include <Windows.h>
 3 #include <process.h>
 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 }

 

posted @ 2016-03-25 01:12  挨踢淫才  阅读(581)  评论(0编辑  收藏  举报