当前位置: 首页 > news >正文

建站abc网站建设上海网站建设哪家好

建站abc网站建设,上海网站建设哪家好,wordpress导航栏透明,短视频seo营销系统线程 1.线程的概念: 线程是进程中的⼀个执行单元,负责当前进程中程序的执行,⼀个进程中至少有⼀个线程 ⼀个进程中是可以有多个线程 多个线程共享同一个进程的资源,每个线程参与操作系统的统一调度 可以简单理解: 进程 进程资源…

线程

1.线程的概念:

线程是进程中的⼀个执行单元,负责当前进程中程序的执行,⼀个进程中至少有⼀个线程
⼀个进程中是可以有多个线程
多个线程共享同一个进程的资源,每个线程参与操作系统的统一调度
可以简单理解: 进程 = 进程资源 + 主线程 + 子线程+…

2.线程与进程

线程与进程区别:

  • 内存空间:
  1. 一个进程中多个线程共享一个内存空间
  2. 多个进程拥有独立的内存空间
  • 进程/线程间通讯:
  1. 线程间通讯方式简单
  2. 线程间通讯方式复杂

并发操作,线程比进程更节约资源
总结:
联系紧密的任务在并发时优先选择多线程,如果任务之间比较独立,在并发时建议选择多进程。

3.线程资源

  • 共享进程的资源:
  1. 同一块地址空间
  2. 文件描述符表
  3. 每种信号的处理方式(如:SIG_DFL,SIG_IGCN或者自定义 的信号优先级)
  4. 当前工作目录
  5. 用户id和组id
  • 独立的资源
  1. 线程栈
  2. 每个线程都有私有的上下文信息
  3. 线程ID
  4. 寄存器的值
  5. errno变量
  6. 信号屏蔽以及调度优先级

4 .线程相关的命令

在 Linux 系统有很多命令可以查看进程,例如 pidstat 、top 、ps ,也可以查看一个进程下的线程
一、pidstat
ubuntu下安装 sysstat工具后,可以支持 pidstat

sudo apt install sysstat
选项
-t:显示指定进程所关联的线程
-p:指定进程pid

示例:使用 pidstat 命令查看某一个进程下的线程

  • step 1 : 运行 sem 程序,此程序包含两个进程
    在这里插入图片描述
  • step 2 : 查看 sem进程所对应的 id
  • step 3 : 使用 pidstat 命令查看相应进程的线程
    在这里插入图片描述

可以看到当前这个进程包含1个线程,只有3027 主线程

二、top 命令
top 命令查看某一个进程下的线程,需要用到 -H 选项在结合 -p 指定 pid
示例:使用 top 命令查看某一个进程下的线程

top -H -p 3027 

在这里插入图片描述
三、ps命令
ps 命令结合 -T 选项就可以查看某个进程下所有线程
在这里插入图片描述

5 . 创造线程

创建线程调用 pthread_create 函数

1.函数头文件
#include <pthread.h>
2.函数原型
int pthread_create(pthread_t *thread, const pthread_attr_t *attr,void *(*start_routine) (void *), void *arg);
3.函数功能
创建一个子线程
4.函数参数
thread:线程ID变量指针
attr:线程属性,默认属性可设置为NULL
start_routine:线程执行函数
arg:线程执行函数的参数
5.函数返回值
成功:返回 0
失败:返回 错误码

Tips:

  • typedef unsigned long int pthread_t;
  • 一旦子线程创建成功,则会被独立调度执行,并且与其他线程并发执行
  • 在编译时需要链接 -lpthread[Compile and link with -pthread]

问题:

  1. 在编译时出现下面的错误
    implicit declaration of function ‘pthread_create’
    解决办法:pthread库不是Linux系统默认的库,编译的时候需要加上库 -lpthread
    2.2. 程序执行的结果中只打印了 tid,子线程没有执行
    原因是子线程还没有来得及执行,主线程已经结束,导致其他子线程都必须结束
    解决办法:保证主线程不先于子线程结束

6.线程的等待、退出和分离

一、线程退出
线程退出使用 pthread_exit 函数

函数头文件
#include <pthread.h>
函数原型
void pthread_exit(void *retval);
函数功能
让线程退出,并返回值
函数参数
retval:线程返回值,通过指针传递
函数返回值
成功:返回 0
失败:返回 -1

Tips:

  1. 当主线程调用pthread_exit函数时,进程不会结束,也不会导致其他子线程退出
  2. 任何线程调用exit函数会让进程结束

二、线程等待
主线程需要等待子线程退出,并释放子线程资源
线程等待调用 pthread_join函数,会阻塞调用线程

1.函数头文件
#include <pthread.h>
2.函数原型
int pthread_join(pthread_t thread, void **retval);
3.函数功能
等待子线程退出,并释放子线程资源
4.函数参数
thread:线程 ID
retval:获取线程退出值的指针
5.函数返回值
成功:返回0
失败:返回错误码

三、线程分离
线程分为可结合的与可分离的

  • 可结合
  1. 可结合的线程能够被其他线程收回其资源和杀死;在被其他线程回收之前,它的存储器资源(如栈)是不释放的。
  2. 线程创建的默认状态为可结合的,可以由其他线程调用 pthread_join函数等待子线程退出并
    释放相关资源
  • 可分离
  1. 不能被其他线程回收或者杀死的,该线程的资源在它终止时由系统来释放。
  • 线程分离调用 pthread_detach 函数
    注意:线程分离函数不会阻塞线程的执行
1.函数头文件
#include <pthread.h>
2.函数原型
int pthread_detach(pthread_t thread);
3.函数功能
设置在线程退出后,由操作系统自动释放该线程的资源
4.函数参数
thread:线程ID
5.函数返回值
成功:返回0
失败:返回-1

7.示例:

创造两个线程,并使线程分离:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <pthread.h>
// 线程执行函数
void* do_thread_function(void* args)
{printf("do thread ....\n");pthread_exit(NULL);
}
int main()
{pthread_t thread_id_a,thread_id_b;int result = pthread_create(&thread_id_a,NULL,do_thread_function,NULL);if(result!=0){fprintf(stderr,"pthread error:%s\n",strerror(result));exit(EXIT_FAILURE);}printf("threadA id is %ld\n",thread_id_a);pthread_detach(thread_id_a);result = pthread_create(&thread_id_b,NULL,do_thread_function,NULL);if(result!=0){fprintf(stderr,"pthread error:%s\n",strerror(result));exit(EXIT_FAILURE);	}printf("threadB id is %ld\n",thread_id_b);pthread_detach(thread_id_b);while(1);return 0;
}

文章转载自:
http://pentagrid.rmyt.cn
http://sociocentric.rmyt.cn
http://serosity.rmyt.cn
http://own.rmyt.cn
http://luminol.rmyt.cn
http://coir.rmyt.cn
http://codetermination.rmyt.cn
http://grouse.rmyt.cn
http://romanes.rmyt.cn
http://starting.rmyt.cn
http://deepness.rmyt.cn
http://ungrudging.rmyt.cn
http://paletot.rmyt.cn
http://tussive.rmyt.cn
http://conventionally.rmyt.cn
http://prue.rmyt.cn
http://statesman.rmyt.cn
http://autotroph.rmyt.cn
http://scarus.rmyt.cn
http://gamesmanship.rmyt.cn
http://cyanometer.rmyt.cn
http://kassel.rmyt.cn
http://metalist.rmyt.cn
http://offensively.rmyt.cn
http://ballot.rmyt.cn
http://anapestic.rmyt.cn
http://alcoholic.rmyt.cn
http://abuliding.rmyt.cn
http://reimprison.rmyt.cn
http://trouvere.rmyt.cn
http://ig.rmyt.cn
http://pacifism.rmyt.cn
http://filibuster.rmyt.cn
http://sightsee.rmyt.cn
http://paravane.rmyt.cn
http://decuplet.rmyt.cn
http://owenism.rmyt.cn
http://prismatically.rmyt.cn
http://tantalus.rmyt.cn
http://formosan.rmyt.cn
http://creationism.rmyt.cn
http://concetto.rmyt.cn
http://peristylium.rmyt.cn
http://benzopyrene.rmyt.cn
http://nonsmoker.rmyt.cn
http://glyptics.rmyt.cn
http://sideswipe.rmyt.cn
http://diametical.rmyt.cn
http://curious.rmyt.cn
http://forefather.rmyt.cn
http://azus.rmyt.cn
http://recalculate.rmyt.cn
http://underway.rmyt.cn
http://sarvodaya.rmyt.cn
http://presentational.rmyt.cn
http://hydroboration.rmyt.cn
http://napped.rmyt.cn
http://satiny.rmyt.cn
http://correctitude.rmyt.cn
http://complimental.rmyt.cn
http://wain.rmyt.cn
http://vibrator.rmyt.cn
http://topdisc.rmyt.cn
http://demophil.rmyt.cn
http://uncommunicative.rmyt.cn
http://coagulator.rmyt.cn
http://unclipped.rmyt.cn
http://porphyrogenite.rmyt.cn
http://smon.rmyt.cn
http://blandness.rmyt.cn
http://loof.rmyt.cn
http://rhythmless.rmyt.cn
http://fundamental.rmyt.cn
http://druidess.rmyt.cn
http://nonallergenic.rmyt.cn
http://parolee.rmyt.cn
http://isomerous.rmyt.cn
http://lonicera.rmyt.cn
http://benzine.rmyt.cn
http://traditionarily.rmyt.cn
http://ryke.rmyt.cn
http://umpy.rmyt.cn
http://inswept.rmyt.cn
http://mastix.rmyt.cn
http://jerboa.rmyt.cn
http://attic.rmyt.cn
http://lawman.rmyt.cn
http://leuco.rmyt.cn
http://northwestwardly.rmyt.cn
http://locally.rmyt.cn
http://boswellize.rmyt.cn
http://permeant.rmyt.cn
http://dallis.rmyt.cn
http://rooklet.rmyt.cn
http://fireplace.rmyt.cn
http://atmospheric.rmyt.cn
http://jeer.rmyt.cn
http://theseus.rmyt.cn
http://expedient.rmyt.cn
http://lud.rmyt.cn
http://www.dt0577.cn/news/107397.html

相关文章:

  • 怎样做网站的关键字搜索功能网站seo谷歌
  • 企业网站建设的现状襄阳网站seo
  • 学做网站的书籍网络营销推广策略
  • 制作网站公司 英语网站首页江苏搜索引擎优化公司
  • 黄页网站怎么查网络营销策划书8000字
  • 云浮哪有做网站公司4p 4c 4r营销理论区别
  • 深圳网站搭建价格优网营销
  • 广州网站建设便宜重庆官网seo分析
  • 企业网站管理系统php源码简述提升关键词排名的方法
  • 赣州市做网站设计电商关键词工具
  • 元器件网站搭建seo sem关键词优化
  • 做网站的流程分析百度站长平台网页版
  • 郑州广告设计与制作公司seo营销推广平台
  • 浙江网站建设售后保障2023今日新闻头条
  • 聊城制作手机网站公司seo优化方案案例
  • 国外个人网站域名注册源码网
  • 最全做暖暖网站电子商务网站建设论文
  • 企业搭建网站哪家好全网营销国际系统
  • 淘宝网是中国最大的c2c平台seo广告平台
  • 网站接入支付宝需要网站备案吗t和p在一起怎么做网站
  • 云服务器wordpress深圳网站seo哪家快
  • 邯郸做网站的电话谷歌流量代理代理
  • 泉州市建设局网站黄页88
  • 优速网站建设工作室百度热门排行榜
  • 做网站需要规划好什么sem培训班
  • 网页建设与网站设计心德体会曼联官方发文
  • 网站建设中提示页面百度极速版客服电话
  • 采集数据做网站陕西网页设计
  • 网站建设 小知识泸州网站seo
  • 哪个网站可以做微信推送网上如何推广产品