linux多线程编程实验报告,Linux多线程编程
8种机械键盘轴体对比
本人程序员,要买一个写代码的键盘,请问红轴和茶轴怎么选?
使用pthread_create函数创建线程。1
2
3
4
5
6
7
8int (pthread_t *__restrict __newthread,
__const pthread_attr_t *__restrict __attr,//线程属性
void *(*__start_routine) (void *),//新创建的线程从start_routine开始执行
void *__restrict __arg)//执行函数的参数
//返回值:成功-0,失败-返回错误编号
//用strerror(errno)函数得到错误信息。
例程:1
2
3int err;
pthread_t tid1;
err = pthread_create(&tid1, NULL, thread_func, NULL);//创建线程
终止线程
三种方式线程从执行函数返回,返回值是线程的退出码线程被同一进程的其他线程取消调用pthread_exit()函数退出。这里不是调用exit,因为线程调用exit函数,会导致线程所在的进程退出。
例程一
启动两个线程,一个线程对全局变量num执行加1操作,执行五百次,一个线程对全局变量执行减1操作,同样执行五次。1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62#include
#include
#include
#include
#include
int num = 0;
void *add(void *arg)
{//线程执行函数,执行5次加法
int i = 0, tmp;
for (; i < 5; i++)
{
tmp = num + 1;
num = tmp;
printf("add+1,result is:%dn", num);
}
return ((void *)0);
}
void *sub(void *arg)//线程执行函数,执行5次减法
{
int i = 0, tmp;
for (; i < 5; i++)
{
tmp = num - 1;
num = tmp;
printf("sub-1,result is:%dn", num);
}
return ((void *)0);
}
int main(int argc, char** argv)
{
pthread_t tid1, tid2;
int err;
void *tret;
err = pthread_create(&tid1, NULL, add, NULL);//创建线程
if (err != 0)
{
printf("pthread_create error:%sn", strerror(err));
exit(-1);
}
err = pthread_create(&tid2, NULL, sub, NULL);
if (err != 0)
{
printf("pthread_create error:%sn", strerror(err));
exit(-1);
}
err = pthread_join(tid1, &tret);//阻塞等待线程id为tid1的线程,直到该线程退出
if (err != 0)
{
printf("can not join with thread1:%sn", strerror(err));
exit(-1);
}
printf("thread 1 exit code %dn", (int)(intptr_t)tret);
err = pthread_join(tid2, &tret);
if (err != 0)
{
printf("can not join with thread1:%sn", strerror(err));
exit(-1);
}
printf("thread 2 exit code %dn", (int)(intptr_t)tret);
return 0;
}
编译命令: g++ -o 1_example 1_example.cpp -lpthread
问题: 两个线程可以对同一变量进行修改。假如线程1执行tmp=4+1后,被系统中断,此时线程2对num=5执行了减一操作,当线程1恢复,在执行num=tmp=5。而正确结果应为4。所以当多个线程对共享区域进行修改时,应该采用同步的方式。
线程同步(三种方式)
互斥量
互斥量用pthread_mutex_t数据类型来表示。第一种:赋值为常量PTHREAD_MUTEX_INITIALIZER;
第二种,当互斥量为动态分配是,使用pthread_mutex_init函数进行初始化,使用pthread_mutex_destroy函数销毁。1
2
3
4
5int pthread_mutex_init (pthread_mutex_t *__mutex,
__const pthread_mutexattr_t *__mutexattr);
int pthread_mutex_destroy (pthread_mutex_t *__mutex);
//返回值:成功-0,失败-错误编号
加解锁加锁调用pthread_mutex_lock
解锁调用pthread_mutex_unlock1
2
3int pthread_mutex_lock (pthread_mutex_t *__mutex);
int pthread_mutex_unlock (pthread_mutex_t *__mutex);
使用互斥修改程序的add和sub两个函数:1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27pthread_mutex_t mylock = PTHREAD_MUTEX_INITIALIZER;
void *add(void *arg)
{
int i = 0, tmp;
for (; i < 500; i++)
{
pthread_mutex_lock(&mylock);
tmp = num + 1;
num = tmp;
printf("+1,result is:%dn", num);
pthread_mutex_unlock(&mylock);
}
return ((void *)0);
}
void *sub(void *arg)
{
int i = 0, tmp;
for (; i < 500; i++)
{
pthread_mutex_lock(&mylock);
tmp = num - 1;
num = tmp;
printf("-1,result is:%dn", num);
pthread_mutex_unlock(&mylock);
}
return ((void *)0);
}
读写锁
允许多个线程同时读,只能有一个线程同时写。适用于读的次数远大于写的情况。
读写锁初始化:1
2
3
4
5int pthread_rwlock_init (pthread_rwlock_t *__restrict __rwlock,
__const pthread_rwlockattr_t *__restrict__attr);
int pthread_rwlock_destroy (pthread_rwlock_t *__rwlock);
//返回值:成功--0,失败-错误编号
加锁,分为读加锁和写加锁。
解锁使用同一个函数。1
2
3
4
5
6//读加锁:
int pthread_rwlock_rdlock (pthread_rwlock_t *__rwlock)
//写加锁:
int pthread_rwlock_wrlock (pthread_rwlock_t *__rwlock)
//解锁用同一个函数:
int pthread_rwlock_unlock (pthread_rwlock_t *__rwlock)
条件变量
条件变量用pthread_cond_t数据类型表示。
条件变量本身由互斥量保护,所以在改变条件状态前必须锁住互斥量。
条件变量初始化1
2
3
4
5//第一种,赋值常量PTHREAD_COND_INITIALIZER;
//第二种,使用pthread_cond_init函数
int pthread_cond_init (pthread_cond_t *__restrict __cond,
__const pthread_condattr_t *__restrict__cond_attr);
int pthread_cond_destroy (pthread_cond_t *__cond);
条件等待
使用pthread_cond_wait等待条件为真。1pthread_cond_wait (pthread_cond_t *__restrict __cond,pthread_mutex_t *__restrict __mutex)
这里需要注意的是,调用pthread_cond_wait传递的互斥量已锁定,pthread_cond_wait将调用线程放入等待条件的线程列表,然后释放互斥量,在pthread_cond_wait返回时,再次锁定互斥量。
唤醒线程
pthread_cond_signal唤醒等待该条件的某个线程,pthread_cond_broadcast唤醒等待该条件的所有线程。1
2int pthread_cond_signal (pthread_cond_t *__cond);
int pthread_cond_broadcast (pthread_cond_t *__cond)
例程二
主线程启动4个线程,每个线程有一个参数i(i=生成顺序),无论线程的启动顺序如何,执行顺序只能为,线程0、线程1、线程2、线程3。1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66#include
#include
#include
#include
#include
#define DEBUG 1
int num = 0;
pthread_mutex_t mylock = PTHREAD_MUTEX_INITIALIZER;//互斥量
pthread_cond_t qready = PTHREAD_COND_INITIALIZER;//条件变量--本身由互斥量保护,所以在改变条件状态前必须锁住互斥量。
void * thread_func(void *arg)
{
int i = (int)arg;
int ret;
sleep(5 - i);//线程睡眠,然最先生成的线程,最后苏醒
pthread_mutex_lock(&mylock);//调用pthread_cond_wait前,必须获得互斥锁
while (i != num)
{
#ifdef DEBUG
printf("thread %d waitingn", i);
#endif
ret = pthread_cond_wait(&qready, &mylock);//该函数把线程放入等待条件的线程列表,然后对互斥锁进行解锁,这两部都是原子操作。并且在pthread_cond_wait返回时,互斥量再次锁住。
if (ret == 0)
{
#ifdef DEBUG
printf("thread %d wait successn", i);
#endif
}
else
{
#ifdef DEBUG
printf("thread %d wait failed:%sn", i, strerror(ret));
#endif
}
}
printf("thread %d is running n", i);
num++;
pthread_mutex_unlock(&mylock);//解锁
pthread_cond_broadcast(&qready);//唤醒等待该条件的所有线程
return (void *)0;
}
int main(int argc, char** argv)
{
int i = 0, err;
pthread_t tid[4];
void *tret;
for (; i < 4; i++)
{
err = pthread_create(&tid[i], NULL, thread_func, (void *)i);//创建线程
if (err != 0)
{
printf("thread_create error:%sn", strerror(err));
exit(-1);
}
}
for (i = 0; i < 4; i++)
{
err = pthread_join(tid[i], &tret);//线程阻塞
if (err != 0)
{
printf("can not join with thread %d:%sn", i, strerror(err));
exit(-1);
}
}
return 0;
}
在非DEBUG模式,执行结果如下所示:1
2
3
4
5
6
7[email protected]: g++ -o 1_example 1_example.cpp -lpthread
[email protected]: thread 0 is running
[email protected]: thread 1 is running
[email protected]: thread 2 is running
[email protected]: thread 3 is running
[email protected]: thread 4 is running
在DEBUG模式,执行结果如下所示:1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18[email protected]: g++ -o 1example 1example.cpp -lpthread
[email protected]: thread 3 waiting
[email protected]: thread 2 waiting
[email protected]: thread 1 waiting
[email protected]: thread 0 is running
[email protected]: thread 3 wait success
[email protected]: thread 3 waiting
[email protected]: thread 2 wait success
[email protected]: thread 2 waiting
[email protected]: thread 1 wait success
[email protected]: thread 1 running
[email protected]: thread 3 wait success
[email protected]: thread 3 waiting
[email protected]: thread 2 wait success
[email protected]: thread 2 running
[email protected]: thread 3 wait success
[email protected]: thread 3 running
在DEBUG模式可以看出,线程3先被唤醒,然后执行pthread_cond_wait(输出thread 3 waiting),此时在pthread_cond_wait中先解锁互斥量,然后进入等待状态。这是thread 2加锁互斥量成功,进入pthread_cond_wait(输出thread 2 waiting) ,同样解锁互斥量,然后进入等待状态。直到线程0,全局变量与线程参数i一致,满足条件,不进入条件等待,输出thread 0 is running。全局变量num执行加1操作,解锁互斥量,然后唤醒所有等待该条件的线程。thread 3 被唤醒,输出thread 3 wait success。但是不满足条件,再次执行pthread_cond_wait。如此执行下去,满足条件的线程执行,不满足条件的线程等待。
linux多线程编程实验报告,Linux多线程编程相关推荐
- linux shell程序设计实验报告,linux的shell脚本实验报告
<linux的shell脚本实验报告>由会员分享,可在线阅读,更多相关<linux的shell脚本实验报告(7页珍藏版)>请在人人文库网上搜索. 1.第二次实验内容一.实验名称 ...
- linux文件目录操作实验报告,Linux操作系统实验4文件和目录操作
<Linux操作系统>课程实验报告 实验4 文件和目录操作 一.实验目的 1.理解Linux文件系统的结构和目录组织方式: 2.掌握Linux常用目录和文件命令的使用. 二.实验内容与要求 ...
- linux使用环境实验报告,Linux 及其使用环境实验报告.doc
计算机语言与程序设计上机实验报告 学号: 姓名: 所在系: 班级: 实验名称: 实验1 Linux 及其使用环境 实验日期 实验指导教师 实验机房及机号 ----------------------- ...
- linux系统进程控制实验报告,Linux进程控制实验报告.doc
里奴性进程控制实验报告 实验名称: Linux进程控制 实验要求:一.编写一个Linux系统C程序,由父亲创建2个子进程,再由子进程各自从控制台接收一串字符串,保存在各自的全局字符串变量中,然后正常结 ...
- 熟悉linux操作系统的使用实验报告,Linux系统的熟悉与使用操作系统实验报告
Linux系统的熟悉与使用操作系统实验报告 实验一 Linux系统熟悉与使用 Linux有两种不同的含义.从严格的技术定义讲,Linux指的是开放源代码的Unix类操作系统的内核.然而,目前大多数人用 ...
- 实验四 linux进程控制实验报告,Linux系统进程控制操作系统实验报告4
实验课程名称:操作系统 实验项目名称Linux系统进程控制实验成绩 实验者专业班级组别 同组者实验日期年月日第一部分:实验分析与设计(可加页) 实验内容描述(问题域描述) 要求:掌握Linux系统中进 ...
- linux信号处理编程实验报告,Linux编程之信号处理
信号是类Unix系统中的一种通知机制.在Linux下,我们可以通过kill -l命令来查看有哪些信号.Linux下有64个信号,其中前32个信号是经典信号,后32个是用于驱动开发要用到的. 那么信号是 ...
- linux系统文件查找实验报告,Linux 文件查找与打包
Linux运维与DevOps实战-实验7 一.find # find 命令在目录层次下搜寻查找一个文件 # 并且会打印所找到文件的有关信息 # 语法 find [path...] [expressio ...
- linux dhcp配置实验报告,Linux DHCP服务器配置实验报告.doc
实验一 DHCP服务器配置 实验课程名:Red Hat Enterprise Linux系统管理 专业班级: 学号: 姓名: 实验时间: 实验地点: 指导教师: 一.实验目的 (1) 掌握Linux下 ...
最新文章
- 一个球从100米高度自由落下,每次落地后反弹回原高度的一半; * 再落下,求在第几次之后反弹高度小于0.1米, * 并计算在这一次落地时共经过多少米?...
- 劝你别把开源的AI项目写在简历上了!!!
- 关于iOS知识的提升
- Spring Data Solr教程:分页
- 产品认知:说说产品经理的底层思维——用户思维
- 价值80元的emlog博客用户注册插件
- Visual Studio Code 使用指南
- ZetCode JavaScript 教程
- 机器人动力学建模之牛顿欧拉法推导
- IoT平台功能架构图
- US1MF-ASEMI贴片薄体封装二极管US1M
- ADB使用及日志分析
- 名字真好听的五子棋——12周进度
- 多个迹象表明,瑞幸咖啡已进入新的发展阶段
- @Cacheable缓存注解使用
- jQuery实现平年闰年判断
- html 圆环实现多种颜色,Echart饼图实现(圆环图)+状态颜色控制
- ettercap 中间人攻击
- 开源中最好的Web开发的资源 ——来自CoolShell.cn
- Linux aarch64交叉编译之 assimp模型库