xref: /openbmc/qemu/tests/tcg/multiarch/testthread.c (revision 709395f8)
1 #include <assert.h>
2 #include <stdlib.h>
3 #include <stdio.h>
4 #include <string.h>
5 #include <unistd.h>
6 #include <inttypes.h>
7 #include <pthread.h>
8 #include <sys/wait.h>
9 #include <sched.h>
10 
11 void checked_write(int fd, const void *buf, size_t count)
12 {
13     ssize_t rc = write(fd, buf, count);
14     assert(rc == count);
15 }
16 
17 void *thread1_func(void *arg)
18 {
19     int i;
20     char buf[512];
21 
22     for(i=0;i<10;i++) {
23         snprintf(buf, sizeof(buf), "thread1: %d %s\n", i, (char *)arg);
24         checked_write(1, buf, strlen(buf));
25         usleep(100 * 1000);
26     }
27     return NULL;
28 }
29 
30 void *thread2_func(void *arg)
31 {
32     int i;
33     char buf[512];
34     for(i=0;i<20;i++) {
35         snprintf(buf, sizeof(buf), "thread2: %d %s\n", i, (char *)arg);
36         checked_write(1, buf, strlen(buf));
37         usleep(150 * 1000);
38     }
39     return NULL;
40 }
41 
42 void test_pthread(void)
43 {
44     pthread_t tid1, tid2;
45 
46     pthread_create(&tid1, NULL, thread1_func, "hello1");
47     pthread_create(&tid2, NULL, thread2_func, "hello2");
48     pthread_join(tid1, NULL);
49     pthread_join(tid2, NULL);
50     printf("End of pthread test.\n");
51 }
52 
53 int main(int argc, char **argv)
54 {
55     test_pthread();
56     return 0;
57 }
58