pthread的使用
写一个test_pthread.c,练习pthread的用法
#include <pthread.h>
#include <unistd.h>
#include <stdio.h>
void *thread(void *str)
{
int i;
for (i = 0; i < 10; ++i)
{
sleep(2);
printf( "This in the thread : %dn" , i );
}
return NULL;
}
int main()
{
pthread_t pth;
int i;
int ret = pthread_create(&pth, NULL, thread, NULL);
pthread_join(pth, NULL);
for (i = 0; i < 10; ++i)
{
sleep(1);
printf( "This in the main : %dn" , i );
}
return 0;
}
编译的时候发现,报错对‘pthread_create’未定义的引用,由于pthread库不是Linux系统默认的库,连接时需要使用库libpthread.a,所以在使用pthread_create创建线程时,在编译中要加-lpthread参数:然后重新编译
gcc -o pthread pthread_test.c -lpthread
运行
./pthread
运行结果
This in the thread : 0
This in the thread : 1
This in the thread : 2
This in the thread : 3
This in the thread : 4
This in the thread : 5
This in the thread : 6
This in the thread : 7
This in the thread : 8
This in the thread : 9
This in the main : 0
This in the main : 1
This in the main : 2
This in the main : 3
This in the main : 4
This in the main : 5
This in the main : 6
This in the main : 7
This in the main : 8
This in the main : 9
这说明:pthread_join函数的调用者在等待子线程退出后才继续执行!
pthread_join函数:
函数pthread_join用来等待一个线程的结束。 函数定义: int pthread_join(pthread_t thread, void **retval); 描述 : pthread_join()函数,以阻塞的方式等待thread指定的线程结束。当函数返回时,被等待线程的资源被收回。如果进程已经结束,那么该函数会立即返回。并且thread指定的线程必须是joinable的。
参数 : thread: 线程标识符,即线程ID,标识唯一线程。 retval: 用户定义的指针,用来存储被等待线程的返回值。 返回值 : 0代表成功。 失败,返回的则是错误号。 现在屏蔽pthread_join函数:
运行结果为:
This in the main : 0
This in the thread : 0
This in the main : 1
This in the main : 2
This in the thread : 1
This in the main : 3
This in the main : 4
This in the thread : 2
This in the main : 5
This in the main : 6
This in the thread : 3
This in the main : 7
This in the main : 8
This in the thread : 4
This in the main : 9
说明在子线程没有运行完毕,main函数就退出了。
pthread_create函数:
声明:
int pthread_create(pthread_t *thread,
const pthread_attr_t *restrict_attr,
void*(*start_rtn)(void*),
void *restrict arg);
参数:
第一个参数*thread为指向线程标识符的指针。 第二个参数*restrict_attr用来设置线程属性,上面也可以用NULL,表示使用默认的属性。 第三个参数是线程运行函数的起始地址。 最后一个参数是运行函数的参数,NULL表示无参数。 另外,在编译时注意加上-lpthread参数,以调用链接库。因为pthread并非Linux系统的默认库,而是posix线程库,在Linux中将其作为一个库来使用,因此加上 -lpthread(或-pthread)以显示的链接该库。函数在执行错误时的错误信息将作为返回值返回,并不修改系统全局变量errno,当然也无法使用perror()打印错误信息。
pthread_t:pthread_t用于声明线程ID!
类型定义: typedef unsigned long int pthread_t; //come from /usr/include/bits/pthread.h sizeof (pthread_t) =4;
pthread_attr_init函数:
声明:int pthread_attr_init(pthread_attr_t*attr);
返回值:返回0,表示函数初始化对象成功。失败时返回一个错误代码。 参数:指向一个线程属性的指针。 函数例程如下:
int main(int argc, char *argv[])
{
pthread_t tid;/*线程标示符*/
pthread_attr_t attr;
if (argc != 2)/*如果参数不为2个*/
{
fprintf (stderr, "usage:a.out<integer value>n");/*报错*/
return -1;
}
if (atoi(argv[1] ) < 0)
{
fprintf (stderr, "%d must be <= 0n", atoi(argv[1]));
return -1;
}
pthread_attr_init(&attr); /*初始化,得到默认的属性值*/
pthread_create(&tid, &attr, runner, argv[1]);/*创建一个线程*/
pthread_join(tid, NULL);/*等待子线程执行完毕*/
printf ("sum = %dn", sum);
return 0;
}
void *runner(void *param)/*子线程将会执行这个函数*/
{
int i, upper = atoi(param);
sum = 0;
printf("upper:%dn",upper);
for (i = 1; i <= upper; i++)
{
sum += i;
}
pthread_exit(0);
}
运行:
./pthread 1
得到结果
upper:1
sum = 1
如果屏蔽
//pthread_attr_init(&attr); /*初始化,得到默认的属性值*/
运行结果时,发现sum=0,没有upper值打印,说明,没有进入子线程,如果把attr变为NULL,
pthread_create(&tid, NULL, runner, argv[1]);/*创建一个线程*/
运行,输出结果又是
upper:1
sum = 1
- 【陆勤创业】德鲁克:如何管理自己?
- 基于FPGA的非线性滤波器(三)
- Mesos+Marathon+Docker构建docker集群化管理
- 【测试】两种数据库,四种分页算法的效率比较
- 【开源】QuickPager 分页控件的内部结构,和OO原则与设计模式
- 【实体类变形】—— 元数据(另类ORM) 描述字段的数据
- 【陆勤践行】奇异值分解 - 最清晰易懂的svd 科普
- 快速自检电脑是否被黑客入侵过(Linux版)
- 【陆勤践行】机器学习中距离和相似性度量方法
- 运维工作常用的shell命令
- SVN搭建指南
- python3 源码编译并配置jupyterhub
- 【小试身手】几个自定义控件的组合应用,实现简单的“增删改查”功能(有源码)
- 如何手动给Docker容器设置静态IP
- JavaScript 教程
- JavaScript 编辑工具
- JavaScript 与HTML
- JavaScript 与Java
- JavaScript 数据结构
- JavaScript 基本数据类型
- JavaScript 特殊数据类型
- JavaScript 运算符
- JavaScript typeof 运算符
- JavaScript 表达式
- JavaScript 类型转换
- JavaScript 基本语法
- JavaScript 注释
- Javascript 基本处理流程
- Javascript 选择结构
- Javascript if 语句
- Javascript if 语句的嵌套
- Javascript switch 语句
- Javascript 循环结构
- Javascript 循环结构实例
- Javascript 跳转语句
- Javascript 控制语句总结
- Javascript 函数介绍
- Javascript 函数的定义
- Javascript 函数调用
- Javascript 几种特殊的函数
- JavaScript 内置函数简介
- Javascript eval() 函数
- Javascript isFinite() 函数
- Javascript isNaN() 函数
- parseInt() 与 parseFloat()
- escape() 与 unescape()
- Javascript 字符串介绍
- Javascript length属性
- javascript 字符串函数
- Javascript 日期对象简介
- Javascript 日期对象用途
- Date 对象属性和方法
- Javascript 数组是什么
- Javascript 创建数组
- Javascript 数组赋值与取值
- Javascript 数组属性和方法
- 下载并安装Git
- 线程的六种状态转换
- 线程池的基本使用
- JavaScript闭包
- Js中Currying的应用
- Qt音视频开发37-USB摄像头解码ffmpeg方案
- Navicat 环境测试 innodb 的事务隔离级别 产生的幻读 和 不可重复读
- leetcode之最长回文串
- Discourse CentOS 8 全新安装手册
- 3分钟短文:Laravel路子真野啊!路由昵称前缀中间件
- CSS中重要的BFC概念
- Redis哨兵集群中哨兵挂了,主从库还能切换吗?
- 你的 Redis 为什么变慢了?
- 解决Maven依赖冲突的好帮手,这款IDEA插件了解一下?
- Python爬虫实现HTTP网络请求多种实现方式