winlin

refs #182: research st, add multiple threadds.

@@ -5,11 +5,16 @@ @@ -5,11 +5,16 @@
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;
8 st_cond_t cond = NULL; 9 st_cond_t cond = NULL;
9 st_mutex_t mutex = NULL; 10 st_mutex_t mutex = NULL;
10 11
11 -void* pfn(void* arg) 12 +void* master(void* arg)
12 { 13 {
  14 + // wait for main to start this thread.
  15 + st_mutex_lock(start);
  16 + st_mutex_unlock(start);
  17 +
13 st_usleep(100 * 1000); 18 st_usleep(100 * 1000);
14 st_cond_signal(cond); 19 st_cond_signal(cond);
15 20
@@ -24,6 +29,30 @@ void* pfn(void* arg) @@ -24,6 +29,30 @@ void* pfn(void* arg)
24 return NULL; 29 return NULL;
25 } 30 }
26 31
  32 +void* slave(void* arg)
  33 +{
  34 + // lock mutex to control thread.
  35 + st_mutex_lock(mutex);
  36 +
  37 + // wait for main to start this thread.
  38 + st_mutex_lock(start);
  39 + st_mutex_unlock(start);
  40 +
  41 + // wait thread to ready.
  42 + st_cond_wait(cond);
  43 + srs_trace("1. st cond is ok");
  44 +
  45 + // release mutex to control thread
  46 + st_usleep(100 * 1000);
  47 + st_mutex_unlock(mutex);
  48 +
  49 + // wait thread to exit.
  50 + st_cond_wait(cond);
  51 + srs_trace("4. st is ok");
  52 +
  53 + return NULL;
  54 +}
  55 +
27 int main(int argc, char** argv) 56 int main(int argc, char** argv)
28 { 57 {
29 if (st_set_eventsys(ST_EVENTSYS_ALT) < 0) { 58 if (st_set_eventsys(ST_EVENTSYS_ALT) < 0) {
@@ -36,40 +65,41 @@ int main(int argc, char** argv) @@ -36,40 +65,41 @@ int main(int argc, char** argv)
36 return -1; 65 return -1;
37 } 66 }
38 67
  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 +
39 if ((cond = st_cond_new()) == NULL) { 74 if ((cond = st_cond_new()) == NULL) {
40 - srs_trace("st_cond_new failed"); 75 + srs_trace("st_cond_new cond failed");
41 return -1; 76 return -1;
42 } 77 }
43 78
44 if ((mutex = st_mutex_new()) == NULL) { 79 if ((mutex = st_mutex_new()) == NULL) {
45 - srs_trace("st_mutex_new failed"); 80 + srs_trace("st_mutex_new mutex failed");
46 return -1; 81 return -1;
47 } 82 }
48 83
49 - if (!st_thread_create(pfn, NULL, 0, 0)) { 84 + if (!st_thread_create(master, NULL, 0, 0)) {
50 srs_trace("st_thread_create failed"); 85 srs_trace("st_thread_create failed");
51 return -1; 86 return -1;
52 } 87 }
53 88
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); 89 + if (!st_thread_create(slave, NULL, 0, 0)) {
  90 + srs_trace("st_thread_create failed");
  91 + return -1;
  92 + }
64 93
65 - // wait thread to exit.  
66 - st_cond_wait(cond);  
67 - srs_trace("4. st is ok"); 94 + // run all threads.
  95 + st_mutex_unlock(start);
68 96
69 // cleanup. 97 // cleanup.
  98 + st_thread_exit(NULL);
  99 +
  100 + st_mutex_destroy(start);
70 st_cond_destroy(cond); 101 st_cond_destroy(cond);
71 st_mutex_destroy(mutex); 102 st_mutex_destroy(mutex);
72 - st_thread_exit(NULL);  
73 103
74 return 0; 104 return 0;
75 } 105 }