正在显示
1 个修改的文件
包含
41 行增加
和
3 行删除
| @@ -5,10 +5,22 @@ | @@ -5,10 +5,22 @@ | ||
| 5 | 5 | ||
| 6 | #define srs_trace(msg, ...) printf(msg, ##__VA_ARGS__);printf("\n") | 6 | #define srs_trace(msg, ...) printf(msg, ##__VA_ARGS__);printf("\n") |
| 7 | 7 | ||
| 8 | +st_cond_t cond = NULL; | ||
| 9 | +st_mutex_t mutex = NULL; | ||
| 10 | + | ||
| 8 | void* pfn(void* arg) | 11 | void* pfn(void* arg) |
| 9 | { | 12 | { |
| 10 | - st_sleep(1); | ||
| 11 | - srs_trace("st thread is ok"); | 13 | + st_usleep(100 * 1000); |
| 14 | + st_cond_signal(cond); | ||
| 15 | + | ||
| 16 | + st_mutex_lock(mutex); | ||
| 17 | + srs_trace("2. st mutex is ok"); | ||
| 18 | + st_mutex_unlock(mutex); | ||
| 19 | + | ||
| 20 | + st_usleep(100 * 1000); | ||
| 21 | + srs_trace("3. st thread is ok"); | ||
| 22 | + st_cond_signal(cond); | ||
| 23 | + | ||
| 12 | return NULL; | 24 | return NULL; |
| 13 | } | 25 | } |
| 14 | 26 | ||
| @@ -23,14 +35,40 @@ int main(int argc, char** argv) | @@ -23,14 +35,40 @@ int main(int argc, char** argv) | ||
| 23 | srs_trace("st_init failed"); | 35 | srs_trace("st_init failed"); |
| 24 | return -1; | 36 | return -1; |
| 25 | } | 37 | } |
| 38 | + | ||
| 39 | + if ((cond = st_cond_new()) == NULL) { | ||
| 40 | + srs_trace("st_cond_new failed"); | ||
| 41 | + return -1; | ||
| 42 | + } | ||
| 43 | + | ||
| 44 | + if ((mutex = st_mutex_new()) == NULL) { | ||
| 45 | + srs_trace("st_mutex_new failed"); | ||
| 46 | + return -1; | ||
| 47 | + } | ||
| 26 | 48 | ||
| 27 | if (!st_thread_create(pfn, NULL, 0, 0)) { | 49 | if (!st_thread_create(pfn, NULL, 0, 0)) { |
| 28 | srs_trace("st_thread_create failed"); | 50 | srs_trace("st_thread_create failed"); |
| 29 | return -1; | 51 | return -1; |
| 30 | } | 52 | } |
| 31 | 53 | ||
| 32 | - srs_trace("st is ok"); | 54 | + // lock mutex to control thread. |
| 55 | + st_mutex_lock(mutex); | ||
| 56 | + | ||
| 57 | + // wait thread to ready. | ||
| 58 | + st_cond_wait(cond); | ||
| 59 | + srs_trace("1. st cond is ok"); | ||
| 60 | + | ||
| 61 | + // release mutex to control thread | ||
| 62 | + st_usleep(100 * 1000); | ||
| 63 | + st_mutex_unlock(mutex); | ||
| 64 | + | ||
| 65 | + // wait thread to exit. | ||
| 66 | + st_cond_wait(cond); | ||
| 67 | + srs_trace("4. st is ok"); | ||
| 33 | 68 | ||
| 69 | + // cleanup. | ||
| 70 | + st_cond_destroy(cond); | ||
| 71 | + st_mutex_destroy(mutex); | ||
| 34 | st_thread_exit(NULL); | 72 | st_thread_exit(NULL); |
| 35 | 73 | ||
| 36 | return 0; | 74 | return 0; |
-
请 注册 或 登录 后发表评论