您的位置:首页 > 其它

thread_ para

2015-12-11 00:00 148 查看
摘要: error = pthread_create(&tidp, NULL, create, (void *)b); 创建线程时,传递参数给线程.

hyh@hyh-OptiPlex-9020:~/git/hyh_home/1512_home/应用程序设计/多线程$ cat thread_struct.c
#include <stdio.h>
#include <pthread.h>
#include <unistd.h>
#include <stdlib.h>

struct menber
{
int a;
char *s;
};

void *create(void *arg)
{

struct menber *temp;
temp=(struct menber *)arg;

printf("th:menber->a = %d \n",temp->a);
printf("th:menber->s = %s \n",temp->s);
temp->a = 3;
temp->s = "create";

return (void *)0;
}

int main(int argc,char *argv[])
{
pthread_t tidp;
int error;
struct menber *b;

b=(struct menber *)malloc( sizeof(struct menber) );
if (NULL == b) {
perror("malloc()");
exit(-1);
}
b->a = 4;
b->s = "zieckey";

error = pthread_create(&tidp, NULL, create, (void *)b);
if( error )
{
printf("phread is not created...\n");
return -1;
}

//sleep(1);
printf("pthread is created... \n");
printf("menber->a = %d \n",b->a);
printf("menber->s = %s \n",b->s);
printf("main modify... \n");
b->a = 5;
b->s = "zieckey5";
printf("menber->a = %d \n",b->a);
printf("menber->s = %s \n",b->s);

return 0;
}
hyh@hyh-OptiPlex-9020:~/git/hyh_home/1512_home/应用程序设计/多线程$
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: