一直担心在线程里加sleep会引起进程所有的线程挂起,测试过了不会,这样可以用sleep来判断程序是否是多线程
- #include <pthread.h>
- #include <stdio.h>
- #include <sys/time.h>
- #include <string.h>
- #include <unistd.h>
- #define MAX 10
- pthread_t thread[2];
- void * thread1()
- {
- while(1)printf ("thread1 : I'm thread 1\n");
- }
- void * thread2()
- {
- printf("thread2 : I'm thread 2\n");
- sleep(3);
- printf("thread2 : I'm thread 2 end\n");
- exit(0);
- }
- void thread_create(void)
- {
- int temp;
- memset(&thread, 0, sizeof(thread));
- if((temp = pthread_create(&thread[0], NULL, thread1, NULL)) != 0)
- printf("create thread 1 fail\n");
- else
- printf("create thread 1 success\n");
- if((temp = pthread_create(&thread[1], NULL, thread2, NULL)) != 0)
- printf("create thread 2 fail\n");
- else
- printf("create thread 2 success\n");
- }
- void thread_wait(void)
- {
- if(thread[0] !=0) { //comment4
- pthread_join(thread[0],NULL);
- printf("thread1 end\n");
- }
- if(thread[1] !=0) { //comment5
- pthread_join(thread[1],NULL);
- printf("thread2 end\n");
- }
- }
- int main()
- {
- printf("i am main\n");
- thread_create();
- printf("mail waitin \n");
- thread_wait();
- return 0;
- }