#include <stdio.h> #include <stdlib.h> #include <string.h> #include <pthread.h> #include <unistd.h> //####################################################### // // 李刚 // 2016.8.17 // pthread 线程参数传递 // //######################################################## struct CTime{ int id; char name[12]; char Num[12]; }; void *GetThread(void *arg){ struct CTime *t_time = (struct CTime *)arg; // printf("pthread_t=%d ",(int)pthread_self()); printf("%d " , t_time->id); printf("%s " , t_time->name); printf("%s " , t_time->Num); return 0; } void *GetInstace(void * arg){ int* a = ((void **)arg)[0]; float* b = ((void **)arg)[1]; char* c = ((void **)arg)[2]; printf("this is thread: %d, %.3f, %s ", *a, *b, c); return 0; } int main(int argc, char*argv[]){ pthread_t tid; int err = 0; pthread_attr_t attr; struct CTime t_time; int a = 12; float b = 23.5f; char c[] = "hello,world"; void *arg[3] = {&a, &b, c}; // t_time.id =12; memcpy(t_time.name, "tom json", sizeof("tom json")); memcpy(t_time.Num, "1234567", sizeof("1234567")); pthread_attr_init(&attr); // if((err = pthread_create(&tid, &attr, &GetThread, (void *) &t_time)) != 0) printf("Error err = %d ", err); pthread_join(tid, NULL); if((err = pthread_create(&tid, &attr, &GetInstace, (void *) arg)) != 0) printf("Error "); pthread_join(tid, NULL); return 0; }