参数为结构体
将信息传递给子线程,子线程修改信息后(在buf后加个时间)传递给主线程
#include <stdio.h>
#include <pthread.h>
#include <string.h>
#include <stdlib.h>
#include <time.h>
#include <strings.h>
typedef struct
{
int num;
char buf[256];
}INFO;
void * th(void * arg)
{
INFO * temp = (INFO *)arg;
printf("%d %s\n",temp->num,temp->buf);
temp->num++;
time_t tm;
time(&tm);
strcat(temp->buf,ctime(&tm));
pthread_exit(temp);
}
int main(int argc, const char *argv[])
{
pthread_t tid;
char buf[] = "这是个主线程参数\n";
INFO info;
bzero(&info,sizeof(info));
info.num = 20;
strcpy(info.buf,buf);
pthread_create(&tid,NULL,th,&info);
void *ret;
pthread_join(tid,&ret);
printf("main th done... %d %s\n", ((INFO*)ret)->num,((INFO*)ret)->buf);
return 0;
}