root / tests / testthread.c @ 2edcdce3
History | View | Annotate | Download (1019 Bytes)
1 | 1b6b029e | bellard | #include <stdlib.h> |
---|---|---|---|
2 | 1b6b029e | bellard | #include <stdio.h> |
3 | 7fb9a24e | bellard | #include <string.h> |
4 | 1b6b029e | bellard | #include <signal.h> |
5 | 1b6b029e | bellard | #include <unistd.h> |
6 | 1b6b029e | bellard | #include <inttypes.h> |
7 | 1b6b029e | bellard | #include <pthread.h> |
8 | 1b6b029e | bellard | #include <sys/wait.h> |
9 | 1b6b029e | bellard | #include <sched.h> |
10 | 1b6b029e | bellard | |
11 | 1b6b029e | bellard | void *thread1_func(void *arg) |
12 | 1b6b029e | bellard | { |
13 | 1b6b029e | bellard | int i;
|
14 | 1b6b029e | bellard | char buf[512]; |
15 | 1b6b029e | bellard | |
16 | 1b6b029e | bellard | for(i=0;i<10;i++) { |
17 | 1b6b029e | bellard | snprintf(buf, sizeof(buf), "thread1: %d %s\n", i, (char *)arg); |
18 | 1b6b029e | bellard | write(1, buf, strlen(buf));
|
19 | 1b6b029e | bellard | usleep(100 * 1000); |
20 | 1b6b029e | bellard | } |
21 | 1b6b029e | bellard | return NULL; |
22 | 1b6b029e | bellard | } |
23 | 1b6b029e | bellard | |
24 | 1b6b029e | bellard | void *thread2_func(void *arg) |
25 | 1b6b029e | bellard | { |
26 | 1b6b029e | bellard | int i;
|
27 | 1b6b029e | bellard | char buf[512]; |
28 | 1b6b029e | bellard | for(i=0;i<20;i++) { |
29 | 1b6b029e | bellard | snprintf(buf, sizeof(buf), "thread2: %d %s\n", i, (char *)arg); |
30 | 1b6b029e | bellard | write(1, buf, strlen(buf));
|
31 | 1b6b029e | bellard | usleep(150 * 1000); |
32 | 1b6b029e | bellard | } |
33 | 1b6b029e | bellard | return NULL; |
34 | 1b6b029e | bellard | } |
35 | 1b6b029e | bellard | |
36 | 1b6b029e | bellard | void test_pthread(void) |
37 | 1b6b029e | bellard | { |
38 | 1b6b029e | bellard | pthread_t tid1, tid2; |
39 | 1b6b029e | bellard | |
40 | 1b6b029e | bellard | pthread_create(&tid1, NULL, thread1_func, "hello1"); |
41 | 1b6b029e | bellard | pthread_create(&tid2, NULL, thread2_func, "hello2"); |
42 | 1b6b029e | bellard | pthread_join(tid1, NULL);
|
43 | 1b6b029e | bellard | pthread_join(tid2, NULL);
|
44 | 1b6b029e | bellard | printf("End of pthread test.\n");
|
45 | 1b6b029e | bellard | } |
46 | 1b6b029e | bellard | |
47 | 1b6b029e | bellard | int main(int argc, char **argv) |
48 | 1b6b029e | bellard | { |
49 | 1b6b029e | bellard | test_pthread(); |
50 | 1b6b029e | bellard | return 0; |
51 | 1b6b029e | bellard | } |