正在显示
1 个修改的文件
包含
48 行增加
和
42 行删除
| @@ -5,102 +5,108 @@ | @@ -5,102 +5,108 @@ | ||
| 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_mutex_t start = NULL; | ||
| 9 | -st_cond_t cond = NULL; | ||
| 10 | -st_mutex_t mutex = NULL; | 8 | +st_mutex_t sync_start = NULL; |
| 9 | +st_cond_t sync_cond = NULL; | ||
| 10 | +st_mutex_t sync_mutex = NULL; | ||
| 11 | 11 | ||
| 12 | -void* master(void* arg) | 12 | +void* sync_master(void* arg) |
| 13 | { | 13 | { |
| 14 | - // wait for main to start this thread. | ||
| 15 | - st_mutex_lock(start); | ||
| 16 | - st_mutex_unlock(start); | 14 | + // wait for main to sync_start this thread. |
| 15 | + st_mutex_lock(sync_start); | ||
| 16 | + st_mutex_unlock(sync_start); | ||
| 17 | 17 | ||
| 18 | st_usleep(100 * 1000); | 18 | st_usleep(100 * 1000); |
| 19 | - st_cond_signal(cond); | 19 | + st_cond_signal(sync_cond); |
| 20 | 20 | ||
| 21 | - st_mutex_lock(mutex); | 21 | + st_mutex_lock(sync_mutex); |
| 22 | srs_trace("2. st mutex is ok"); | 22 | srs_trace("2. st mutex is ok"); |
| 23 | - st_mutex_unlock(mutex); | 23 | + st_mutex_unlock(sync_mutex); |
| 24 | 24 | ||
| 25 | st_usleep(100 * 1000); | 25 | st_usleep(100 * 1000); |
| 26 | srs_trace("3. st thread is ok"); | 26 | srs_trace("3. st thread is ok"); |
| 27 | - st_cond_signal(cond); | 27 | + st_cond_signal(sync_cond); |
| 28 | 28 | ||
| 29 | return NULL; | 29 | return NULL; |
| 30 | } | 30 | } |
| 31 | 31 | ||
| 32 | -void* slave(void* arg) | 32 | +void* sync_slave(void* arg) |
| 33 | { | 33 | { |
| 34 | // lock mutex to control thread. | 34 | // lock mutex to control thread. |
| 35 | - st_mutex_lock(mutex); | 35 | + st_mutex_lock(sync_mutex); |
| 36 | 36 | ||
| 37 | - // wait for main to start this thread. | ||
| 38 | - st_mutex_lock(start); | ||
| 39 | - st_mutex_unlock(start); | 37 | + // wait for main to sync_start this thread. |
| 38 | + st_mutex_lock(sync_start); | ||
| 39 | + st_mutex_unlock(sync_start); | ||
| 40 | 40 | ||
| 41 | // wait thread to ready. | 41 | // wait thread to ready. |
| 42 | - st_cond_wait(cond); | 42 | + st_cond_wait(sync_cond); |
| 43 | srs_trace("1. st cond is ok"); | 43 | srs_trace("1. st cond is ok"); |
| 44 | 44 | ||
| 45 | // release mutex to control thread | 45 | // release mutex to control thread |
| 46 | st_usleep(100 * 1000); | 46 | st_usleep(100 * 1000); |
| 47 | - st_mutex_unlock(mutex); | 47 | + st_mutex_unlock(sync_mutex); |
| 48 | 48 | ||
| 49 | // wait thread to exit. | 49 | // wait thread to exit. |
| 50 | - st_cond_wait(cond); | 50 | + st_cond_wait(sync_cond); |
| 51 | srs_trace("4. st is ok"); | 51 | srs_trace("4. st is ok"); |
| 52 | 52 | ||
| 53 | return NULL; | 53 | return NULL; |
| 54 | } | 54 | } |
| 55 | 55 | ||
| 56 | -int main(int argc, char** argv) | 56 | +int sync_test() |
| 57 | { | 57 | { |
| 58 | - if (st_set_eventsys(ST_EVENTSYS_ALT) < 0) { | ||
| 59 | - srs_trace("st_set_eventsys failed"); | ||
| 60 | - return -1; | ||
| 61 | - } | ||
| 62 | - | ||
| 63 | - if (st_init() < 0) { | ||
| 64 | - srs_trace("st_init failed"); | 58 | + if ((sync_start = st_mutex_new()) == NULL) { |
| 59 | + srs_trace("st_mutex_new sync_start failed"); | ||
| 65 | return -1; | 60 | return -1; |
| 66 | } | 61 | } |
| 62 | + st_mutex_lock(sync_start); | ||
| 67 | 63 | ||
| 68 | - if ((start = st_mutex_new()) == NULL) { | ||
| 69 | - srs_trace("st_mutex_new start failed"); | ||
| 70 | - return -1; | ||
| 71 | - } | ||
| 72 | - st_mutex_lock(start); | ||
| 73 | - | ||
| 74 | - if ((cond = st_cond_new()) == NULL) { | 64 | + if ((sync_cond = st_cond_new()) == NULL) { |
| 75 | srs_trace("st_cond_new cond failed"); | 65 | srs_trace("st_cond_new cond failed"); |
| 76 | return -1; | 66 | return -1; |
| 77 | } | 67 | } |
| 78 | 68 | ||
| 79 | - if ((mutex = st_mutex_new()) == NULL) { | 69 | + if ((sync_mutex = st_mutex_new()) == NULL) { |
| 80 | srs_trace("st_mutex_new mutex failed"); | 70 | srs_trace("st_mutex_new mutex failed"); |
| 81 | return -1; | 71 | return -1; |
| 82 | } | 72 | } |
| 83 | 73 | ||
| 84 | - if (!st_thread_create(master, NULL, 0, 0)) { | 74 | + if (!st_thread_create(sync_master, NULL, 0, 0)) { |
| 85 | srs_trace("st_thread_create failed"); | 75 | srs_trace("st_thread_create failed"); |
| 86 | return -1; | 76 | return -1; |
| 87 | } | 77 | } |
| 88 | 78 | ||
| 89 | - if (!st_thread_create(slave, NULL, 0, 0)) { | 79 | + if (!st_thread_create(sync_slave, NULL, 0, 0)) { |
| 90 | srs_trace("st_thread_create failed"); | 80 | srs_trace("st_thread_create failed"); |
| 91 | return -1; | 81 | return -1; |
| 92 | } | 82 | } |
| 93 | 83 | ||
| 94 | // run all threads. | 84 | // run all threads. |
| 95 | - st_mutex_unlock(start); | 85 | + st_mutex_unlock(sync_start); |
| 86 | + | ||
| 87 | + return 0; | ||
| 88 | +} | ||
| 89 | + | ||
| 90 | +int main(int argc, char** argv) | ||
| 91 | +{ | ||
| 92 | + if (st_set_eventsys(ST_EVENTSYS_ALT) < 0) { | ||
| 93 | + srs_trace("st_set_eventsys failed"); | ||
| 94 | + return -1; | ||
| 95 | + } | ||
| 96 | + | ||
| 97 | + if (st_init() < 0) { | ||
| 98 | + srs_trace("st_init failed"); | ||
| 99 | + return -1; | ||
| 100 | + } | ||
| 101 | + | ||
| 102 | + if (sync_test() < 0) { | ||
| 103 | + srs_trace("sync_test failed"); | ||
| 104 | + return -1; | ||
| 105 | + } | ||
| 96 | 106 | ||
| 97 | // cleanup. | 107 | // cleanup. |
| 98 | st_thread_exit(NULL); | 108 | st_thread_exit(NULL); |
| 99 | 109 | ||
| 100 | - st_mutex_destroy(start); | ||
| 101 | - st_cond_destroy(cond); | ||
| 102 | - st_mutex_destroy(mutex); | ||
| 103 | - | ||
| 104 | return 0; | 110 | return 0; |
| 105 | } | 111 | } |
| 106 | 112 |
-
请 注册 或 登录 后发表评论