了解线程

网友投稿 510 2022-05-29

特此说明: 刘超的趣谈linux操作系统是比较重要的参考资料,本文大部分内容和所有图片来源于这个专栏。

背景知识

之前了解了进程的创建过程,其实进程默认也有一个主线程(也可以包含其他线程)。线程是负责执行二进制指令的,它会根据项目执行计划书,一行一行执行下去。进程要比线程管的宽多了,除了执行指令之外,内存、文件系统等等都要它来管。

例如,此时有一个开发网站的项目。我们想把它拆解成多个网页任务,并行执行,最后做一个整合。类似的,在程序实现上,也可以将一个功能拆分成多个子任务。可以使用多进程的并行方案,但是有两个问题

第一,创建进程占用资源太多;

第二,进程之间的通信需要数据在不同的内存空间传来传去,无法共享。

使用多线程可以很好的解决这两个问题(多个线程是共享一个进程的资源)。那么,如何创建一个线程任务,线程间又如何对数据操作的呢?

代码示例

1、 编辑&编译&运行

# download.c

//download.c #include #include #include #include #define NUM_OF_TASKS 5 void *downloadfile(void *filename) { printf("I am downloading the file %s!\n", (char *)filename); sleep(5); long downloadtime = rand()%100; printf("I finish downloading the file within %d minutes!\n", downloadtime); pthread_exit((void *)downloadtime); } int main(int argc, char *argv[]) { char files[NUM_OF_TASKS][20]={"file1.avi","file2.rmvb","file3.mp4","file4.wmv","file5.flv"}; pthread_t threads[NUM_OF_TASKS]; // 声明线程对象 int rc; int t; int downloadtime[NUM_OF_TASKS]; // 声明线程属性 pthread_attr_t thread_attr; pthread_attr_init(&thread_attr); pthread_attr_setdetachstate(&thread_attr,PTHREAD_CREATE_JOINABLE); for(t=0;t

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

# 编译

gcc download.c -lpthread

1

# 运行

# ./a.out creating thread 0, please help me to download file1.avi creating thread 1, please help me to download file2.rmvb I am downloading the file file1.avi! creating thread 2, please help me to download file3.mp4 I am downloading the file file2.rmvb! creating thread 3, please help me to download file4.wmv I am downloading the file file3.mp4! creating thread 4, please help me to download file5.flv I am downloading the file file4.wmv! I am downloading the file file5.flv! I finish downloading the file within 83 minutes! I finish downloading the file within 77 minutes! I finish downloading the file within 86 minutes! I finish downloading the file within 15 minutes! I finish downloading the file within 93 minutes! Thread 0 downloads the file file1.avi in 83 minutes. Thread 1 downloads the file file2.rmvb in 86 minutes. Thread 2 downloads the file file3.mp4 in 77 minutes. Thread 3 downloads the file file4.wmv in 93 minutes. Thread 4 downloads the file file5.flv in 15 minutes.

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

2、 总体流程

线程数据

了解线程

1、 线程数据分类

线程栈上的本地数据,相当于函数中的局部变量。通过ulimit -a命令查看线程栈的空间大小。

在整个进程里共享的全局数据,相当于程序中的全部变量。多个线程同时对共享资源访问会造成冲突,所以需要额外的机制控制。

线程私有数据,通过key-value形式存储。通过函数接口操作。多线程可以使用同一个key值,但是各自有不同的value;在线程退出自动析构释放value。

2、 总结框图

3、 数据保护

对共享数据的访问过程中,使用同步互斥机制

互斥锁(Mutex)

互斥锁(Mutex) + 条件变量

信号量

参考资料

极客专栏 - 线程:如何让复杂的项目并行执行?

线程同步和互斥的区别

任务调度

版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:ChemDataExtractor:从PDF、HTM、文本等中提取化学数据
下一篇:[mysql] [innodb]14.7.1 InnoDB 锁
相关文章