有人可以帮助我吗?我想很清楚我在这里尝试用线程做什么。给我一些建议,有人请编译。我认为或者使用线程id指针会出现问题。
#include<stdio.h>
#include<pthread.h>
#include<stdlib.h>
#define NUM_THREADS 5
int main(int argc,char * argv[])
{
pthread_t threads(NUM_THREADS);
int rc,t;
for(t=0;t<NUM_THREADS;t++)
{
printf("In main: creating thread %d \n", t);
rc=ptrhead_create(&threads(t),NULL,PrintHello,(void*),t);
if(rc)
{
printf("Error;pthread_create() returned %d \n",rc);
exit(-1);
}
ptrhead_exit(NULL);
}
struct thread_data
{
int thread_id;
int sum;
char *message;
};
void *PrintHello(void *threadarg)
{
struct thread_data *mydata;
my_data=(struct thread_data *) threadarg;
taskid=my_data->thread_id;
sum=my_data->sum;
hello_msg=my_data->message;
}
int *taskids(NUM_THREADS);
for(t=0;t<NUM_THREADS;t++)
{
taskids(t)=(int *) malloc(sizeof(int));
*taskids(t)=t;
printf("Creating thread %d \n", t);
rc=pthread_create(&threads(t),NULL,PrintHello,(void*) taskids(t));
}
void *PrintHello(void *threadid)
}
int id;
tid=(int)threadid;
printf("Hello World!!! It's me, thread #%d\n", tid);
pthread_exit(NULL);
}