lwbaptx

State Threads 的一个使用样例

在examples下面建立一个huge_threads.c的源文件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
#include <stdio.h>
#include "st.h"

#define SLEEP_INTERVAL 30 //in ms

void* do_calc(void* arg) {
long pidx = *(long*)arg;
for (;;) {
printf("sthread [#%ld] usleep\n", pidx);
st_usleep(SLEEP_INTERVAL * 1000);
}
return NULL;
}

int main(int argc, char** argv) {
if (argc <= 1 ) {
printf("Test the concurrence of state-threads!\n"
"Usage: %s <sthread_count>\n"
"eg. %s 10000\n", argv[0], argv[0]);
return -1;
}

if (st_init() < 0) {
printf("state threads lib runtime init error!");
return -1;
}

int i, count = atoi(argv[1]);
for(i = 1; i <= count; i++){
if(st_thread_create(do_calc, (void*)&i, 0, 0) == NULL) {
printf("create state thread %d failed\n", i);
return -1;
}
}

st_thread_exit(NULL);
return 0;
}

gcc -I../obj -g huge_threads.c ../obj/libst.a -o huge_threads
./huge_threads 10000
./huge_threads 30000