blob: 810ba5de67b3b1c762112517aa3697ee69b56ff9 [file] [log] [blame]
Catalin Patuleaf71d6122012-10-29 14:01:07 -04001#include <assert.h>
bellard1b6b0292003-03-22 17:31:38 +00002#include <stdlib.h>
3#include <stdio.h>
bellard7fb9a242003-04-11 00:13:04 +00004#include <string.h>
bellard1b6b0292003-03-22 17:31:38 +00005#include <unistd.h>
6#include <inttypes.h>
7#include <pthread.h>
8#include <sys/wait.h>
9#include <sched.h>
10
Catalin Patuleaf71d6122012-10-29 14:01:07 -040011void 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
bellard1b6b0292003-03-22 17:31:38 +000017void *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);
Catalin Patuleaf71d6122012-10-29 14:01:07 -040024 checked_write(1, buf, strlen(buf));
bellard1b6b0292003-03-22 17:31:38 +000025 usleep(100 * 1000);
26 }
27 return NULL;
28}
29
30void *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);
Catalin Patuleaf71d6122012-10-29 14:01:07 -040036 checked_write(1, buf, strlen(buf));
bellard1b6b0292003-03-22 17:31:38 +000037 usleep(150 * 1000);
38 }
39 return NULL;
40}
41
42void 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
53int main(int argc, char **argv)
54{
55 test_pthread();
56 return 0;
57}