了解线程
【摘要】
特此说明: 刘超的趣谈linux操作系统是比较重要的参考资料,本文大部分内容和所有图片来源于这个专栏。
背景知识
之前了解了进程的创建过程,其实进程默认也有一个主线程(也可以包含其他线程)。...
特此说明: 刘超的趣谈linux操作系统是比较重要的参考资料,本文大部分内容和所有图片来源于这个专栏。
背景知识
之前了解了进程的创建过程,其实进程默认也有一个主线程(也可以包含其他线程)。线程是负责执行二进制指令的,它会根据项目执行计划书,一行一行执行下去。进程要比线程管的宽多了,除了执行指令之外,内存、文件系统等等都要它来管。
例如,此时有一个开发网站的项目。我们想把它拆解成多个网页任务,并行执行,最后做一个整合。类似的,在程序实现上,也可以将一个功能拆分成多个子任务。可以使用多进程
的并行方案,但是有两个问题
- 第一,创建进程占用资源太多;
- 第二,进程之间的通信需要数据在不同的内存空间传来传去,无法共享。
使用多线程
可以很好的解决这两个问题(多个线程是共享一个进程的资源)。那么,如何创建一个线程任务,线程间又如何对数据操作的呢?
代码示例
1、 编辑&编译&运行
# download.c
//download.c
#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#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<NUM_OF_TASKS;t++){
printf("creating thread %d, please help me to download %s\n", t, files[t]);
rc = pthread_create(&threads[t], &thread_attr, downloadfile, (void *)files[t]); // 创建线程
if (rc){
printf("ERROR; return code from pthread_create() is %d\n", rc);
exit(-1);
}
}
pthread_attr_destroy(&thread_attr); // 销毁线程属性
for(t=0;t<NUM_OF_TASKS;t++){
pthread_join(threads[t],(void**)&downloadtime[t]); //获取其他线程的返回值
printf("Thread %d downloads the file %s in %d minutes.\n",t,files[t],downloadtime[t]);
}
pthread_exit(NULL); // 结束线程,返回NULL
}
# 编译
gcc download.c -lpthread
# 运行
# ./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.
2、 总体流程
线程数据
1、 线程数据分类
线程栈上的本地数据
,相当于函数中的局部变量。通过ulimit -a
命令查看线程栈的空间大小。在整个进程里共享的全局数据
,相当于程序中的全部变量。多个线程同时对共享资源访问会造成冲突,所以需要额外的机制控制。线程私有数据
,通过key-value形式存储。通过函数接口操作。多线程可以使用同一个key值,但是各自有不同的value;在线程退出自动析构释放value。
2、 总结框图
3、 数据保护
对共享数据的访问过程中,使用同步互斥机制
- 互斥锁(Mutex)
- 互斥锁(Mutex) + 条件变量
- 信号量
参考资料
文章来源: blog.csdn.net,作者:hinzer,版权归原作者所有,如需转载,请联系作者。
原文链接:blog.csdn.net/feit2417/article/details/105521618
【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱:
cloudbbs@huaweicloud.com
- 点赞
- 收藏
- 关注作者
评论(0)