Last change
on this file since 502a107 was
c26bea2a,
checked in by Peter A. Buhr <pabuhr@…>, 15 months ago
|
first attempt at renaming directory tests/concurrent to tests/concurrency to harmonize with other concurrency directory names
|
-
Property mode set to
100644
|
File size:
1.0 KB
|
Rev | Line | |
---|
[428adbc] | 1 | /* small test of pthread cond */ |
---|
[20be782] | 2 | |
---|
| 3 | #include <fstream.hfa> |
---|
| 4 | #include <thread.hfa> |
---|
| 5 | #include <pthread.h> |
---|
| 6 | |
---|
| 7 | int done_flag = 0; |
---|
| 8 | pthread_mutex_t _mutex; |
---|
| 9 | pthread_cond_t cond; |
---|
| 10 | |
---|
| 11 | extern "C"{ |
---|
| 12 | void* S1(void* arg){ |
---|
| 13 | pthread_mutex_lock(&_mutex); |
---|
| 14 | for (int i = 0; i < 1000; i++) sout | "S1 done " | i; |
---|
| 15 | done_flag = 1; |
---|
| 16 | pthread_mutex_unlock(&_mutex); |
---|
| 17 | pthread_cond_signal(&cond); |
---|
| 18 | return NULL; |
---|
| 19 | } |
---|
| 20 | |
---|
| 21 | void* S2(void* arg){ |
---|
| 22 | pthread_mutex_lock(&_mutex); |
---|
| 23 | if (!done_flag) pthread_cond_wait(&cond, &_mutex); |
---|
| 24 | sout | "S2 statement done!"; |
---|
| 25 | pthread_mutex_unlock(&_mutex); |
---|
| 26 | return NULL; |
---|
| 27 | } |
---|
| 28 | } |
---|
| 29 | |
---|
| 30 | |
---|
| 31 | |
---|
| 32 | int main(int argc, char const *argv[]) |
---|
| 33 | { |
---|
| 34 | /* code */ |
---|
| 35 | pthread_mutex_init(&_mutex, NULL); |
---|
| 36 | pthread_cond_init(&cond, NULL); |
---|
| 37 | pthread_t s1,s2; |
---|
| 38 | pthread_create(&s1, NULL, S1, NULL); |
---|
| 39 | pthread_create(&s2, NULL, S2, NULL); |
---|
| 40 | void* res = NULL; |
---|
| 41 | pthread_join(s1, &res); |
---|
| 42 | pthread_join(s2, &res); |
---|
| 43 | pthread_mutex_destroy(&_mutex); |
---|
| 44 | pthread_cond_destroy(&cond); |
---|
| 45 | return 0; |
---|
| 46 | } |
---|
Note: See
TracBrowser
for help on using the repository browser.