在编译多线程程序的时候,需要连接libpthread文件:

gcc pthread.c  -o  pthread  -lpthread;

所有线程一律平等,没有父子关系,线程属于进程。

创建线程用 pthread_create()函数,其函数原型是:

#include<pthread.h>

int pthread_create(pthread_t *restrict thread,const pthread_attr_t *restric attr,void* (*start_routine)(void *),void *restrict arg);

第一个参数为指向线程标识符的指针,第二个参数用来设置线程属性,大多数情况下,

我们不需要设置线程的属性,那么空指针 NULL 就可以了,第三个参数是线程运行函数的起

始地址,最后一个参数是运行函数的参数。

当创建线程成功时,函数返回 0,若不为 0 则说明创建线程失败。

创建代码如下:

#include<stdio.h>
#include<pthread.h>
#include<unistd.h>
void *thread(void*agrv)
{
int i;
for(i=;i<;i++)
{
printf("run pthread..\n");//sleep(1); }
} int main()
{ int i;
int ret;
pthread_t pthid;//创建线程标识符
ret=pthread_create(&pthid,NULL,thread,NULL);
if(ret!=)
printf("create pthread error!\n"); for(i=;i<;i++)
{
printf("run main..\n");//sleep(1); }
}

注释掉sleep(1)后输出结果为:

run main..
run main..
run main..
run main..
run main..

这是因为主线程结束以后进程就直接结束了。我们可以加一个sleep函数,等待子线程的完成。

加入sleep(1)后结果为:

run main..
run pthread..
run main..
run pthread..
run main..
run pthread..
run main..
run pthread..
run main..
run pthread..

可以看出两个线程的输出是交替的,当一个线程进入睡眠时,另外一个线程就会被调度执行。

接下来修改一下程序,让子进程里面的程序循环10次:此时,由于在main函数中,主线程输出5次进程就会结束,所以子线程里面剩余的输出将不会进行,输出结果会和上面的一样。我们可以添加一个等待函数,注意的是,一个线程不能被多个线程等待!!

pthread_join():这个函数是主线程用于等待子线程结束的,返回值为int型;其函数原型是:

#include<pthread.h>

int pthread_join(pthread_t thread,void **value_ptr);

第一个参数为被等待的线程标识符,第二个参数为一个用户定义的指针,它可以用来存储被等待线程的返回值。这个函数是一个线程阻塞的函数,调用它的函数将一直等待到被等待的线程结束为止,当函数返回时,被等待线程的资源被收回。

代码为:

#include<stdio.h>
#include<pthread.h>
#include<unistd.h>
void *thread(void*agrv)
{
int i;
for(i=;i<;i++)
{
printf("run pthread..\n");sleep(); }
} int main()
{ int i;
int ret;
pthread_t pthid;//创建线程标识符
ret=pthread_create(&pthid,NULL,thread,NULL);
// pthread_join( pthid,NULL); // 等待函数放在这会先把子线程打印完毕再打印主线程里面的内容
if(ret!=)
printf("create pthread error!\n"); for(i=;i<;i++)
{
printf("run main..\n");sleep(); }
pthread_join(pthid,NULL);//一般放在末尾
}

输出结果为:

run main..
run pthread..
run main..
run pthread..
run main..
run pthread..
run main..
run pthread..
run main..
run pthread..
run pthread..
run pthread..
run pthread..
run pthread..
run pthread..

一个线程的结束有两种途径,一种是和示例 3-10 一样,函数结束了,调用它的线程也

就结束了:另一种方式是通过函数 pthread_exit()来实现。它的函数原型为:

#include<pthread.h>

void pthread_exit(void *value_ptr)

函数的参数是函数的返回代码,只要 pthread_join 中的第二个参数 value_ptr 不是NULL,这个值将被传递给主线程。

代码如下:

 #include<stdio.h>
#include<pthread.h>
#include<unistd.h>
void *thread(void*agrv)
{
int i;
for(i=;i<;i++)
{
printf("run pthread..\n");sleep(); }
} int main()
{ int i;
int ret;
pthread_t pthid;//创建线程标识符
ret=pthread_create(&pthid,NULL,thread,NULL); if(ret!=)
printf("create pthread error!\n"); pthread_exit(NULL);//主线程结束,但是程序并没有结束,会打印子线程的内容 for(i=;i<;i++)
{
printf("run main..\n");sleep(); }
}

输出结果为:

run pthread..
run pthread..
run pthread..
run pthread..
run pthread..
run pthread..
run pthread..
run pthread..
run pthread..
run pthread..

最新文章

  1. 解决Spring MVC @ResponseBody返回中文字符串乱码问题
  2. Dynamics AX 2012 R2 从代码中调用SSRS Report
  3. iOS开发系列通讯录、蓝牙、内购、GameCenter、iCloud、Passbook系统服务开
  4. oracle视图总结(转)
  5. Discuz!NT中集成Memcached分布式缓存
  6. JS 节流阀
  7. python JavaScript
  8. Java基础--二维数组
  9. LeetCode算法题-Maximum Product of Three Numbers(Java实现)
  10. [再寄小读者之数学篇](2014-06-22 求导数 [中国科学技术大学2014年高等数学B考研试题])
  11. HTTP协议08-请求首部字段
  12. checkbox默认选中
  13. c# json 序列化如何去掉null值
  14. ELKstack5.6.5
  15. python xlrd使用
  16. Win7 VS2013环境编译Lua5.3.1
  17. 关于socket的知识总结
  18. 用Qemu模拟vexpress-a9 (六) --- 多核
  19. 剑指offer-序列化二叉树
  20. SHELL有用的命令

热门文章

  1. 205. jetcache:你需要知道的小技巧
  2. leetcode78
  3. python中的多进程与多线程(二)
  4. Intersect交集Except差集Union并集实例
  5. 数据库中多对多关系的处理 User---Role
  6. gitlab 常用维护命令
  7. Java获取工程目录
  8. C++11 constexpr常量表达式
  9. Could not GET &#39;https://dl.google.com/dl/android/maven2/com/android/tools/build/gradle/3.1.2/gradle-3
  10. 738. Monotone Increasing Digits 单调递增的最接近数字