- ubuntu12.04环境下使用kvm ioctl接口实现最简单的虚拟机
- Ubuntu 通过无线网络安装Ubuntu Server启动系统后连接无线网络的方法
- 在Ubuntu上搭建网桥的方法
- ubuntu 虚拟机上网方式及相关配置详解
CFSDN坚持开源创造价值,我们致力于搭建一个资源共享平台,让每一个IT人在这里找到属于你的精彩世界.
这篇CFSDN的博客文章Linux多线程编程快速入门由作者收集整理,如果你对这篇文章有兴趣,记得点赞哟.
本文主要对Linux下的多线程进行一个入门的介绍,虽然是入门,但是十分详细,希望大家通过本文所述,对Linux多线程编程的概念有一定的了解。具体如下.
进程是资源管理的基本单元,而线程是系统调度的基本单元,线程是操作系统能够进行调度运算的最小单位,它被包含在进程之中,是进程中的实际运作单位。一条线程指的是进程中一个单一顺序的控制流,一个进程中可以并发多个线程,每条线程并行执行不同的任务.
一个进程在某一个时刻只能做一件事情,有了多个控制线程以后,在程序的设计成在某一个时刻能够做不止一件事,每个线程处理独自的任务.
需要注意的是:即使程序运行在单核处理器上,也能够得到多线程编程模型的好处。处理器的数量并不影响程序结构,所以不管处理器个数多少,程序都可以通过线程得以简化.
linux操作系统使用符合POSIX线程作为系统标准线程,该POSIX线程标准定义了一整套操作线程的API.
与进程有一个ID一样,每个线程有一个线程ID,所不同的是,进程ID在整个系统中是唯一的,而线程是依附于进程的,其线程ID只有在所属的进程中才有意义。线程ID用pthread_t表示.
1
2
3
|
//pthread_self直接返回调用线程的ID
include <pthread.h>
pthread_t pthread_self(
void
);
|
判断两个线程ID的大小是没有任何意义的,但有时可能需要判断两个给定的线程ID是否相等,使用以下接口:
1
2
3
|
//pthread_equal如果t1和t2所指定的线程ID相同,返回0;否则返回非0值。
include <pthread.h>
int
pthread_equal(pthread_t t1, pthread_t t2);
|
一个线程的生命周期起始于它被创建的那一刻,创建线程的接口:
1
2
3
|
#include <pthread.h>
int
pthread_create(pthread_t *
thread
,
const
pthread_attr_t *attr,
void
*(*start_routine) (
void
*),
void
*arg);
|
函数参数:
1
2
3
4
|
thread(输出参数),由pthread_create在线程创建成功后返回的线程句柄,该句柄在后续操作线程的API中用于标志该新建的线程;
start_routine(输入参数),新建线程的入口函数;
arg(输入参数),传递给新线程入口函数的参数;
attr(输入参数),指定新建线程的属性,如线程栈大小等;如果值为NULL,表示使用系统默认属性。
|
函数返回值:
1
2
|
成功,返回0;
失败,返回相关错误码。
|
需要注意:
1.主线程,这是一个进程的初始线程,其入口函数为main函数。 2.新线程的运行时机,一个线程被创建之后有可能不会被马上执行,甚至,在创建它的线程结束后还没被执行;也有可能新线程在当前线程从pthread_create前就已经在运行,甚至,在pthread_create前从当前线程返回前新线程就已经执行完毕.
程序实例:
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
|
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#include <unistd.h>
void
printids(
const
char
*s){
pid_t pid;
pthread_t tid;
pid = getpid();
tid = pthread_self();
printf
(
"%s, pid %lu tid %lu (0x%lx)\n"
,s,(unsigned
long
)pid,(unsigned
long
)tid,
(unsigned
long
)tid);
}
void
*thread_func(
void
*arg){
printids(
"new thread: "
);
return
((
void
*)0);
}
int
main() {
int
err;
pthread_t tid;
err = pthread_create(&tid,NULL,thread_func,NULL);
if
(err != 0) {
fprintf
(stderr,
"create thread fail.\n"
);
exit
(-1);
}
printids(
"main thread:"
);
sleep(1);
return
0;
}
|
注意上述的程序中,主线程休眠一秒,如果不休眠,则主线程不休眠,则其可能会退出,这样新线程可能不会被运行,我自己注释掉sleep函数,发现好多次才能让新线程输出.
编译命令:
1
|
gcc -o
thread
thread
.c -lpthread
|
运行结果如下:
1
2
|
main
thread
:, pid 889 tid 139846854309696 (0x7f30a212f740)
new
thread
: , pid 889 tid 139846845961984 (0x7f30a1939700)
|
可以看到两个线程的进程ID是相同的。其共享进程中的资源.
线程的终止分两种形式:被动终止和主动终止 。
被动终止有两种方式:
1.线程所在进程终止,任意线程执行exit、_Exit或者_exit函数,都会导致进程终止,从而导致依附于该进程的所有线程终止。 2.其他线程调用pthread_cancel请求取消该线程.
主动终止也有两种方式:
1.在线程的入口函数中执行return语句,main函数(主线程入口函数)执行return语句会导致进程终止,从而导致依附于该进程的所有线程终止。 2.线程调用pthread_exit函数,main函数(主线程入口函数)调用pthread_exit函数, 主线程终止,但如果该进程内还有其他线程存在,进程会继续存在,进程内其他线程继续运行.
线程终止函数:
1
2
|
include <pthread.h>
void
pthread_exit(
void
*retval);
|
线程调用pthread_exit函数会导致该调用线程终止,并且返回由retval指定的内容。 注意:retval不能指向该线程的栈空间,否则可能成为野指针! 。
一个线程的终止对于另外一个线程而言是一种异步的事件,有时我们想等待某个ID的线程终止了再去执行某些操作,pthread_join函数为我们提供了这种功能,该功能称为线程的连接:
1
2
|
include <pthread.h>
int
pthread_join(pthread_t
thread
,
void
**retval);
|
参数说明:
1
2
|
thread(输入参数),指定我们希望等待的线程
retval(输出参数),我们等待的线程终止时的返回值,就是在线程入口函数中return的值或者调用pthread_exit函数的参数
|
返回值:
1
2
|
成功时,返回0
错误时,返回正数错误码
|
当线程X连接线程Y时,如果线程Y仍在运行,则线程X会阻塞直到线程Y终止;如果线程Y在被连接之前已经终止了,那么线程X的连接调用会立即返回.
连接线程其实还有另外一层意义,一个线程终止后,如果没有人对它进行连接,那么该终止线程占用的资源,系统将无法回收,而该终止线程也会成为僵尸线程。因此,当我们去连接某个线程时,其实也是在告诉系统该终止线程的资源可以回收了.
注意:对于一个已经被连接过的线程再次执行连接操作, 将会导致无法预知的行为! 。
有时我们并不在乎某个线程是不是已经终止了,我们只是希望如果某个线程终止了,系统能自动回收掉该终止线程所占用的资源。pthread_detach函数为我们提供了这个功能,该功能称为线程的分离:
1
2
|
#include <pthread.h>
int
pthread_detach(pthread_t
thread
);
|
默认情况下,一个线程终止了,是需要在被连接后系统才能回收其占有的资源的。如果我们调用pthread_detach函数去分离某个线程,那么该线程终止后系统将自动回收其资源.
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
67
68
69
70
71
72
|
/*
* 文件名: thread_sample1.c
* 描述:演示线程基本操作
*/
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <pthread.h>
/*子线程1入口函数*/
void
*thread_routine1(
void
*arg)
{
fprintf
(stdout,
"thread1: hello world!\n"
);
sleep(1);
/*子线程1在此退出*/
return
NULL;
}
/*子线程2入口函数*/
void
*thread_routine2(
void
*arg)
{
fprintf
(stdout,
"thread2: I'm running...\n"
);
pthread_t main_thread = (pthread_t)arg;
/*分离自我,不能再被连接*/
pthread_detach(pthread_self());
/*判断主线程ID与子线程2ID是否相等*/
if
(!pthread_equal(main_thread, pthread_self())) {
fprintf
(stdout,
"thread2: main thread id is not equal thread2\n"
);
}
/*等待主线程终止*/
pthread_join(main_thread, NULL);
fprintf
(stdout,
"thread2: main thread exit!\n"
);
fprintf
(stdout,
"thread2: exit!\n"
);
fprintf
(stdout,
"thread2: process exit!\n"
);
/*子线程2在此终止,进程退出*/
pthread_exit(NULL);
}
int
main(
int
argc,
char
*argv[])
{
/*创建子线程1*/
pthread_t t1;
if
(pthread_create(&t1, NULL, thread_routine1, NULL)!=0) {
fprintf
(stderr,
"create thread fail.\n"
);
exit
(-1);
}
/*等待子线程1终止*/
pthread_join(t1, NULL);
fprintf
(stdout,
"main thread: thread1 terminated!\n\n"
);
/*创建子线程2,并将主线程ID传递给子线程2*/
pthread_t t2;
if
(pthread_create(&t2, NULL, thread_routine2, (
void
*)pthread_self())!=0) {
fprintf
(stderr,
"create thread fail.\n"
);
exit
(-1);
}
fprintf
(stdout,
"main thread: sleeping...\n"
);
sleep(3);
/*主线程使用pthread_exit函数终止,进程继续存在*/
fprintf
(stdout,
"main thread: exit!\n"
);
pthread_exit(NULL);
fprintf
(stdout,
"main thread: never reach here!\n"
);
return
0;
}
|
最终的执行结果如下:
1
2
3
4
5
6
7
8
9
10
|
thread1: hello world!
main
thread
: thread1 terminated!
main
thread
: sleeping...
thread2: I'm running...
thread2: main
thread
id is not equal thread2
main
thread
:
exit
!
thread2: main
thread
exit
!
thread2:
exit
!
thread2: process
exit
!
|
以上就是本文关于Linux多线程编程快速入门的全部内容,希望对大家有所帮助。感兴趣的朋友可以继续参阅本站其他相关专题,如有不足之处,欢迎留言指出。感谢朋友们对本站的支持! 。
原文链接:http://blog.csdn.net/taoyanqi8932/article/details/56288950 。
最后此篇关于Linux多线程编程快速入门的文章就讲到这里了,如果你想了解更多关于Linux多线程编程快速入门的内容请搜索CFSDN的文章或继续浏览相关文章,希望大家以后支持我的博客! 。
我将 Bootstrap 与 css 和 java 脚本结合使用。在不影响前端代码的情况下,我真的很难在css中绘制这个背景。在许多问题中,人们将宽度和高度设置为 0%。但是由于我的导航栏,我不能使用
我正在用 c 编写一个程序来读取文件的内容。代码如下: #include void main() { char line[90]; while(scanf("%79[^\
我想使用 javascript 获取矩阵数组的所有对 Angular 线。假设输入输出如下: input = [ [1,2,3], [4,5,6], [7,8,9], ] output =
可以用pdfmake绘制lines,circles和other shapes吗?如果是,是否有documentation或样本?我想用jsPDF替换pdfmake。 最佳答案 是的,有可能。 pdfm
我有一个小svg小部件,其目的是显示角度列表(参见图片)。 现在,角度是线元素,仅具有笔触,没有填充。但是现在我想使用一种“内部填充”颜色和一种“笔触/边框”颜色。我猜想line元素不能解决这个问题,
我正在为带有三角对象的 3D 场景编写一个非常基本的光线转换器,一切都工作正常,直到我决定尝试从场景原点 (0/0/0) 以外的点转换光线。 但是,当我将光线原点更改为 (0/1/0) 时,相交测试突
这个问题已经有答案了: Why do people write "#!/usr/bin/env python" on the first line of a Python script? (22 个回
如何使用大约 50 个星号 * 并使用 for 循环绘制一条水平线?当我尝试这样做时,结果是垂直(而不是水平)列出 50 个星号。 public void drawAstline() { f
这是一个让球以对角线方式下降的 UI,但球保持静止;线程似乎无法正常工作。你能告诉我如何让球移动吗? 请下载一个球并更改目录,以便程序可以找到您的球的分配位置。没有必要下载足球场,但如果您愿意,也可以
我在我的一个项目中使用 Jmeter 和 Ant,当我们生成报告时,它会在报告中显示 URL、#Samples、失败、成功率、平均时间、最短时间、最长时间。 我也想在报告中包含 90% 的时间线。 现
我有一个不寻常的问题,希望有人能帮助我。我想用 Canvas (android) 画一条 Swing 或波浪线,但我不知道该怎么做。它将成为蝌蚪的尾部,所以理想情况下我希望它的形状更像三角形,一端更大
这个问题已经有答案了: Checking Collision of Shapes with JavaFX (1 个回答) 已关闭 8 年前。 我正在使用 JavaFx 8 库。 我的任务很简单:我想检
如何按编号的百分比拆分文件。行数? 假设我想将我的文件分成 3 个部分(60%/20%/20% 部分),我可以手动执行此操作,-_-: $ wc -l brown.txt 57339 brown.tx
我正在努力实现这样的目标: 但这就是我设法做到的。 你能帮我实现预期的结果吗? 更新: 如果我删除 bootstrap.css 依赖项,问题就会消失。我怎样才能让它与 Bootstrap 一起工作?
我目前正在构建一个网站,但遇到了 transform: scale 的问题。我有一个按钮,当用户将鼠标悬停在它上面时,会发生两件事: 背景以对 Angular 线“扫过” 按钮标签颜色改变 按钮稍微变
我需要使用直线和仿射变换绘制大量数据点的图形(缩放图形以适合 View )。 目前,我正在使用 NSBezierPath,但我认为它效率很低(因为点在绘制之前被复制到贝塞尔路径)。通过将我的数据切割成
我正在使用基于 SVM 分类的 HOG 特征检测器。我可以成功提取车牌,但提取的车牌除了车牌号外还有一些不必要的像素/线。我的图像处理流程如下: 在灰度图像上应用 HOG 检测器 裁剪检测到的区域 调
我有以下图片: 我想填充它的轮廓(即我想在这张图片中填充线条)。 我尝试了形态学闭合,但使用大小为 3x3 的矩形内核和 10 迭代并没有填满整个边界。我还尝试了一个 21x21 内核和 1 迭代,但
我必须找到一种算法,可以找到两组数组之间的交集总数,而其中一个数组已排序。 举个例子,我们有这两个数组,我们向相应的数字画直线。 这两个数组为我们提供了总共 7 个交集。 有什么样的算法可以帮助我解决
简单地说 - 我想使用透视投影从近裁剪平面绘制一条射线/线到远裁剪平面。我有我认为是使用各种 OpenGL/图形编程指南中描述的方法通过单击鼠标生成的正确标准化的世界坐标。 我遇到的问题是我的光线似乎
我是一名优秀的程序员,十分优秀!