Linux系统基础——线程机制

Linux系统线程

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

1 背景知识

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

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

  • 第一,创建进程占用资源太多;
  • 第二,进程之间的通信需要数据在不同的内存空间传来传去,无法共享。

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

2 代码示例

1、 编辑&编译&运行

//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、 总体流程

3 线程数据

1、 线程数据分类

  • 线程栈上的本地数据,相当于函数中的局部变量。通过ulimit -a命令查看线程栈的空间大小。
  • 在整个进程里共享的全局数据,相当于程序中的全部变量。多个线程同时对共享资源访问会造成冲突,所以需要额外的机制控制。
  • 线程私有数据,通过key-value形式存储。通过函数接口操作。多线程可以使用同一个key值,但是各自有不同的value;在线程退出自动析构释放value。

2、 总结框图

3、 数据保护

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

  1. 互斥锁(Mutex)
  2. 互斥锁(Mutex) + 条件变量
  3. 信号量

9 References

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

hinzer

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值