From 9aba47cbe3752dff4d85024d0859babb02bf600f Mon Sep 17 00:00:00 2001 From: winlin Date: Sun, 26 Oct 2014 14:04:31 +0800 Subject: [PATCH] refs #182: research st, add multiple threadds. --- trunk/research/st/srs.c | 66 ++++++++++++++++++++++++++++++----------- 1 file changed, 48 insertions(+), 18 deletions(-) diff --git a/trunk/research/st/srs.c b/trunk/research/st/srs.c index 77bccd159..3d10d24ff 100644 --- a/trunk/research/st/srs.c +++ b/trunk/research/st/srs.c @@ -5,11 +5,16 @@ #define srs_trace(msg, ...) printf(msg, ##__VA_ARGS__);printf("\n") +st_mutex_t start = NULL; st_cond_t cond = NULL; st_mutex_t mutex = NULL; -void* pfn(void* arg) +void* master(void* arg) { + // wait for main to start this thread. + st_mutex_lock(start); + st_mutex_unlock(start); + st_usleep(100 * 1000); st_cond_signal(cond); @@ -24,6 +29,30 @@ void* pfn(void* arg) return NULL; } +void* slave(void* arg) +{ + // lock mutex to control thread. + st_mutex_lock(mutex); + + // wait for main to start this thread. + st_mutex_lock(start); + st_mutex_unlock(start); + + // wait thread to ready. + st_cond_wait(cond); + srs_trace("1. st cond is ok"); + + // release mutex to control thread + st_usleep(100 * 1000); + st_mutex_unlock(mutex); + + // wait thread to exit. + st_cond_wait(cond); + srs_trace("4. st is ok"); + + return NULL; +} + int main(int argc, char** argv) { if (st_set_eventsys(ST_EVENTSYS_ALT) < 0) { @@ -36,40 +65,41 @@ int main(int argc, char** argv) return -1; } + if ((start = st_mutex_new()) == NULL) { + srs_trace("st_mutex_new start failed"); + return -1; + } + st_mutex_lock(start); + if ((cond = st_cond_new()) == NULL) { - srs_trace("st_cond_new failed"); + srs_trace("st_cond_new cond failed"); return -1; } if ((mutex = st_mutex_new()) == NULL) { - srs_trace("st_mutex_new failed"); + srs_trace("st_mutex_new mutex failed"); return -1; } - if (!st_thread_create(pfn, NULL, 0, 0)) { + if (!st_thread_create(master, NULL, 0, 0)) { srs_trace("st_thread_create failed"); return -1; } - // lock mutex to control thread. - st_mutex_lock(mutex); + if (!st_thread_create(slave, NULL, 0, 0)) { + srs_trace("st_thread_create failed"); + return -1; + } - // wait thread to ready. - st_cond_wait(cond); - srs_trace("1. st cond is ok"); - - // release mutex to control thread - st_usleep(100 * 1000); - st_mutex_unlock(mutex); - - // wait thread to exit. - st_cond_wait(cond); - srs_trace("4. st is ok"); + // run all threads. + st_mutex_unlock(start); // cleanup. + st_thread_exit(NULL); + + st_mutex_destroy(start); st_cond_destroy(cond); st_mutex_destroy(mutex); - st_thread_exit(NULL); return 0; }