/
testc.c
150 lines (121 loc) · 2.81 KB
/
testc.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
#include <pthread.h>
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/time.h>
#include <time.h>
#define TEST_COUNT 1000
pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
pthread_cond_t cond = PTHREAD_COND_INITIALIZER;
int i = 1;
#if 0
/*
* @brief 线程休眠指定的毫秒数
* 此函数是可重入函数,可以并行多次调用
* TODO: 此函数有问题,执行468次后无法加锁
*/
void thread_sleep(const long ms)
{
pthread_cond_t *cond;
pthread_mutex_t *mutex;
struct timeval now;
struct timespec outtime;
long sec, usec;
mutex = (pthread_mutex_t*)malloc(sizeof(pthread_mutex_t));
if (!mutex)
{
perror("fail to malloc mutex!\n");
goto _end;
}
pthread_mutex_init(mutex, NULL);
cond = (pthread_cond_t*)malloc(sizeof(pthread_cond_t));
if (!cond)
{
perror("fail to malloc cond!\n");
goto _end1;
}
pthread_cond_init(cond, NULL);
printf("sleep lock!\n");
pthread_mutex_lock(mutex);
printf("sleep lock OK!\n");
// calc time difference
sec = (long)(ms/1000);
usec = (long)(ms%1000);
printf("sec = %ld, usec = %ld\n", sec, usec);
// get current time
gettimeofday(&now, NULL);
// set timeout
outtime.tv_sec = now.tv_sec + sec;
outtime.tv_nsec = now.tv_usec + usec * 1000;
// just wait timeout
pthread_cond_timedwait(cond, mutex, &outtime);
pthread_mutex_unlock(mutex);
// release resources
free(cond);
_end1:
free(mutex);
_end:
return;
}
#endif
void *thread2(void *junk)
{
pthread_mutex_lock(&mutex); //互斥锁
printf("---------------------------------\n");
printf("thread2: id %lu\n", pthread_self());
printf("thread2: send signal to thread1...\n");
if(!pthread_cond_signal(&cond))
printf("thread2: send signal ok!\n");
else
perror("thread2: send signal fail!\n");
pthread_mutex_unlock(&mutex);
//sleep(1);
return junk;
}
void *thread1(void* junk)
{
int i;
pthread_t tmp_id;
for (i=0; i<TEST_COUNT; i++)
{
pthread_mutex_lock(&mutex);
printf("---------------- %d ------------------\n", i+1);
printf("thread1: create new thread...\n");
if (!pthread_create(&tmp_id, NULL, thread2, NULL))
{
printf("thread1: OK, id is %lu\n", (unsigned long)tmp_id);
printf("thread1: waiting new thread cond...\n");
if(!pthread_cond_wait(&cond,&mutex))
printf("thread1: recv cond ok!\n");
else
perror("thread1: recv cond fail!\n");
}
else
{
perror("thread1: create failed!\n");
}
// sleep 500ms
struct timespec to;
to.tv_sec = 0;
to.tv_nsec = 1000 * 1000 * 500;
nanosleep(&to, NULL);
//thread_sleep(500);
pthread_mutex_unlock(&mutex);
}
return junk;
}
int main()
{
pthread_t thread1_id;
if (pthread_create(&thread1_id, NULL, thread1, NULL))
{
perror("create thread1:");
return -1;
}
if (pthread_join(thread1_id, NULL))
{
perror("join thread1 error:");
return -1;
}
return 0;
}