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