多线程学习系列一 线程创建
2015-08-20 14:39
375 查看
#include<stdio.h>
#include<pthread.h>
#include<unistd.h>
#include<stdlib.h>
struct Attr
{
int data;
char c;
};
void *Print(void *q)
{
struct Attr *p = (struct Attr *)q;
printf("Child thread: %u .Hello world!\n Function Attributes: %d %c\n",(unsigned int)pthread_self(),p->data,p->c);
pthread_exit((void *)2);
return (void *)0;
}
int main()
{
pthread_t thread;
int err;
struct Attr c = {88,'c'};
err = pthread_create(&thread,NULL,Print,&c);
if(err != 0)
printf("Create Thread ERROR!\n");
printf("main thread:%u\n",(unsigned int)pthread_self());
void *tmp;//=(struct Atrr*)malloc(sizeof(struct Attr));
pthread_join(thread,&tmp);
printf("Child Thread exit code: %d \n",(int)tmp);
return 0;
}
运行结果:
#include<pthread.h>
#include<unistd.h>
#include<stdlib.h>
struct Attr
{
int data;
char c;
};
void *Print(void *q)
{
struct Attr *p = (struct Attr *)q;
printf("Child thread: %u .Hello world!\n Function Attributes: %d %c\n",(unsigned int)pthread_self(),p->data,p->c);
pthread_exit((void *)2);
return (void *)0;
}
int main()
{
pthread_t thread;
int err;
struct Attr c = {88,'c'};
err = pthread_create(&thread,NULL,Print,&c);
if(err != 0)
printf("Create Thread ERROR!\n");
printf("main thread:%u\n",(unsigned int)pthread_self());
void *tmp;//=(struct Atrr*)malloc(sizeof(struct Attr));
pthread_join(thread,&tmp);
printf("Child Thread exit code: %d \n",(int)tmp);
return 0;
}
运行结果:
相关文章推荐
- Package APK file with command line
- 配置VMware Workstation 主机模式下让虚拟机上网设置详细
- 结构体对齐
- MediaButtonReceiver---独特的媒体广播接收器
- Ping of Death 攻击 和 ICMP 风暴
- 工作前的准备
- poj 1860 Currency Exchange(bellman-ford)
- 环形缓冲区的设计与实现
- android studio开发(一) 安卓的历史
- Vitamio中文API文档(3)—— MediaController
- android studio集成极光推送
- 微服务架构实践感悟
- poj 1236 Network of Schools(强连通分量)
- UE4 Mac 使用Xcode编译教程
- Xamarin生成的APK大小分析
- This与成员变量、成员方法、类变量和类方法:
- MySQL Flush导致的等待问题
- 均方差计算
- 正则表达式(三) 表达式助手
- POJ 1035 Spell checker