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/应用程序设计/多线程$
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/应用程序设计/多线程$
相关文章推荐
- linux 进程间通信 pipe
- linux 进程间通信 fifo
- linux 进程间通信 fifo_read
- linux 进程间通信fifo_write
- panic in gccgo
- discuz门户发布文章编辑名字用realname
- php开发工具 zend studio 12.5.1 中文版汉化
- 【嘉兴东臣php】HTML+CSS+JS基础学习周总结
- viewcontroller的跳转问题
- DB2 单ixf 恢复表数据
- python中通过main参数删除文件
- 怎样在iMindMap中添加分支
- 代理模式
- 最长LCS
- 线程间通信
- 在asp中,在子页面中如何让服务器控件的id不在运行后改变的方法
- 奇光影业招聘PHP工程师
- emoji表情
- Mesos简要调研日志
- Tachyon简要调研日志