4,465
社区成员




#include <pthread.h>
#include <unistd.h>
#include <stdio.h>
#include <sys/time.h>
#include <string.h>
#define MAX 10
pthread_t thread[10];
pthread_mutex_t mut;
int number=0, i;
int a = 1;
void *thread1(void* arr)
{
if (a == -1)
printf ("thread1 : I'm thread 1\n");
else
a = -1;
while(1+1>1){
printf("thread 1 running\n");
}
pthread_exit(NULL);
}
void *thread2(void* arr)
{
if (a == -1)
printf("thread2 : I'm thread 2\n");
else
a = -1;
while(1+1>1){
printf("thread 2 running\n");
}
pthread_exit(NULL);
}
void *thread3(void* arr)
{
if (a == -1)
printf("thread3 : I'm thread 3\n");
else
a = -1;
while(1+1>1){
printf("thread 3 running\n");
}
pthread_exit(NULL);
}
void *thread4(void* arr)
{
if (a == -1)
printf("thread4 : I'm thread 4\n");
else
a = -1;
while(1+1>1){
printf("thread 4 running\n");
}
pthread_exit(NULL);
}
void thread_create(void)
{
int temp;
memset(&thread, 0, sizeof(thread)); //comment1
printf("create thread 1\n");
if((temp = pthread_create(&thread[0], NULL, thread1, NULL)) != 0) //comment2
{
}
else
{
}
printf("create thread 2\n");
if((temp = pthread_create(&thread[1], NULL, thread2, NULL)) != 0) //comment3
{}
else
{}
printf("create thread 3\n");
if((temp = pthread_create(&thread[2], NULL, thread3, NULL)) != 0) //comment3
{}
else
{}
printf("create thread 4\n");
if((temp = pthread_create(&thread[3], NULL, thread4, NULL)) != 0) //comment3
{}
else
{}
}
void thread_wait(void)
{
if(thread[0] !=0) { //comment4
pthread_join(thread[0],NULL);
}
if(thread[1] !=0) { //comment5
pthread_join(thread[1],NULL);
}
}
int main()
{
pthread_mutex_init(&mut,NULL);
thread_create();
thread_wait();
return 0;
}