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

新乡公司做网站找哪家公司好域名关键词查询

新乡公司做网站找哪家公司好,域名关键词查询,网站建设 php 企业网站,水果配送网站建设1.线程概念 在一个程序里的一个执行路线就叫做线程(thread)。更准确的定义是:线程是“一个进程内部的控制序列。在linux中,由于线程和进程都具有id,都需要调度等等相似性,因此都可以用PCB来描述和控制,线程含有PCB&am…

1.线程概念

在一个程序里的一个执行路线就叫做线程(thread)。更准确的定义是:线程是“一个进程内部的控制序列。在linux中,由于线程和进程都具有id,都需要调度等等相似性,因此都可以用PCB来描述和控制,线程含有PCB,没有独立的地址空间,和进程内的其他线程共享地址空间。如下图:

从资源的角度上看,进程是资源分配的基本单位,线程是cpu调度的基本单位。每一个控制线程pcb,我们都可以看成是执行流,执行流可以是线程,也可以是只有一个线程的进程,在Linux中,所有执行流我们都可以看成轻量级进程(LWP)。

2.线程控制函数

首先,我们要了解Linux没有真正意义上的线程,所有执行流都是LWP,因此,为了满足用户对线程使用的需求,Linux的线程库对LWP的接口进行了封装,我们把库里封装好的线程称为用户态线程。

1.pthread_create创建线程

#include <pthread.h>
int pthread_create(pthread_t *thread, const pthread_attr_t *attr,void *(*start_routine) (void *), void *arg);

第一个参数是输出型参数,输出新线程的用户级线程id,参数2:设置线程的属性,参数3:返回值和参数都为void* 类型的函数指针,参数4:函数参数。创建线程,执行传入的函数。

2.pthread_join等待线程

#include <pthread.h>int pthread_join(pthread_t thread, void **retval);

第一个参数是要等的线程id,第二个是线程执行后返回值 。

3. pthread_exit,pthread_cancel,pthread_self() 

int pthread_exit()线程退出,exit()是进程退出。

int pthread_cancel(pthread_t thread)让某个进程退出

pthread pthread_self() 用于获取用户态线程的tid

4.代码

#include <iostream>
#include <string>
#include <vector>
#include <cstdio>
#include <unistd.h>
#include <cstdlib>
#include<time.h>
#include <pthread.h> // 原生线程库的头文件
using namespace std;// pthread_create(pthread_t &tid, nullptr, handlerTask, td);创建线程的接口
//pthread_join(tid, &ret);线程等待
// pthread_exit()线程退出
//pthread_cancel(tid)取消线程
pthread_self() 用于获取用户态线程的tidvoid * handlerTask(void* args)
{string s1=(const char*)args;sleep(10);cout<<"我是"<<s1<<" pid 是 "<<getpid()<<endl;cout<<"我的用户级线程id是"<<pthread_self()<<endl;string* s2=new string("haha");pthread_exit(s2);}int main()
{cout<<"我是主线程"<<" pid 是 "<<getpid()<<endl;cout<<"我的用户级线程id是"<<pthread_self()<<endl;const char* s1="新线程";pthread_t tid;pthread_create(&tid, nullptr, handlerTask, (void*)s1);cout<<"新进程用户级id是"<<tid<<endl;void *ret=nullptr;pthread_join(tid, &ret);cout<<"结果为"<<*((string*)ret)<<endl;
}

makefile 文件(注意一定要链接pthread库)

test1:test1.ccg++ -o $@ $^ -std=c++11 -lpthread
.PHONY:clean
clean:rm -f test1

运行结果:

5.线程分离

  • 默认情况下,新创建的线程是joinable的,线程退出后,需要对其进行pthread_join操作,否则无法释放资源,从而造成系统泄漏。
  • 如果不关心线程的返回值,join是一种负担,这个时候,我们可以告诉系统,当线程退出时,自动释放线程资源。 
int pthread_detach(pthread_t thread)

可以是线程组内其他线程对目标线程进行分离,也可以是线程自己分离.

pthread_detach(pthread_self());

3.用户态线程的实现

在共享区上由pthead动态库来维护,封装了LWP,其中用户级进程的id就是虚拟地址。 

4.资源问题

1.线程私有的:

  • 线程的硬件上下文(cpu寄存器的值)
  • 线程的独立栈结构
  • 线程id
  • 信号屏蔽字
  •  errno 信号屏蔽字
  • 调度优先级

2.线程共享的

  • 内存和地址空间(代码和全局数据)
  • 文件描述符表
  • 每种信号的处理方式(SIG_ IGN、SIG_ DFL或者自定义的信号处理函数)
  • 当前工作目录
  • 用户id和组id

5.线程的优缺点

线程的优点

  • 创建一个新线程的代价要比创建一个新进程小得多(线程的地址内存和地址空间是共享的,只需要创间pcb)
  • 与进程之间的切换相比,线程之间的切换需要操作系统做的工作要少很多(线程切换时由于数据共享,cache不需要重新加载,而进程切换需要重新加载)。
  • 线程占用的资源要比进程少很多
  • 能充分利用多处理器的可并行数量
  • 在等待慢速I/O操作结束的同时,程序可执行其他的计算任务
  • 计算密集型应用,为了能在多处理器系统上运行,将计算分解到多个线程中实现
  • I/O密集型应用,为了提高性能,将I/O操作重叠。线程可以同时等待不同的I/O操作。

线程的缺点

  • 性能损失 一个很少被外部事件阻塞的计算密集型线程往往无法与共它线程共享同一个处理器。如果计算密集型线程的数量比可用的处理器多,那么可能会有较大的性能损失,这里的性能损失指的是增加了额外的同步和调度开销,而可用的资源不变。
  • 健壮性降低 编写多线程需要更全面更深入的考虑,在一个多线程程序里,因时间分配上的细微偏差或者因共享了 不该共享的变量而造成不良影响的可能性是很大的,换句话说线程之间是缺乏保护的。
  • 缺乏访问控制 进程是访问控制的基本粒度,在一个线程中调用某些OS函数会对整个进程造成影响。
  • 编程难度提高 编写与调试一个多线程程序比单线程程序困难得多 

文章转载自:
http://dinncosorus.tqpr.cn
http://dinncopacificator.tqpr.cn
http://dinncoalmah.tqpr.cn
http://dinncoreclaimer.tqpr.cn
http://dinncovibriocidal.tqpr.cn
http://dinncodealfish.tqpr.cn
http://dinncodinosaurian.tqpr.cn
http://dinncowarrison.tqpr.cn
http://dinncoeventful.tqpr.cn
http://dinncojunkerism.tqpr.cn
http://dinncobvi.tqpr.cn
http://dinncolibau.tqpr.cn
http://dinncovent.tqpr.cn
http://dinncotennysonian.tqpr.cn
http://dinncocentrum.tqpr.cn
http://dinncopractician.tqpr.cn
http://dinncoxenogeneic.tqpr.cn
http://dinncodemarche.tqpr.cn
http://dinncochoreopoem.tqpr.cn
http://dinncoupya.tqpr.cn
http://dinncoexpansionism.tqpr.cn
http://dinncoincendijel.tqpr.cn
http://dinncooceanization.tqpr.cn
http://dinncoetypic.tqpr.cn
http://dinncovagabondize.tqpr.cn
http://dinncohooverville.tqpr.cn
http://dinncopostfix.tqpr.cn
http://dinncoundissolvable.tqpr.cn
http://dinncocolombophile.tqpr.cn
http://dinnconoviciate.tqpr.cn
http://dinncoateliosis.tqpr.cn
http://dinncokuwaiti.tqpr.cn
http://dinncovalency.tqpr.cn
http://dinncohierogrammat.tqpr.cn
http://dinncourea.tqpr.cn
http://dinncothromboembolus.tqpr.cn
http://dinnconishinomiya.tqpr.cn
http://dinncoaborad.tqpr.cn
http://dinncosawbones.tqpr.cn
http://dinncofitting.tqpr.cn
http://dinncoduluth.tqpr.cn
http://dinncobaroscope.tqpr.cn
http://dinncocentennial.tqpr.cn
http://dinncopintoricchio.tqpr.cn
http://dinncomammonist.tqpr.cn
http://dinncometonymic.tqpr.cn
http://dinncosauce.tqpr.cn
http://dinncomasthead.tqpr.cn
http://dinncohabilimented.tqpr.cn
http://dinncoepoophoron.tqpr.cn
http://dinncocinchona.tqpr.cn
http://dinncoqueenlike.tqpr.cn
http://dinncovisitorial.tqpr.cn
http://dinncosatai.tqpr.cn
http://dinncokentishman.tqpr.cn
http://dinncospelk.tqpr.cn
http://dinncogemeinschaft.tqpr.cn
http://dinncocopaiba.tqpr.cn
http://dinncooperatic.tqpr.cn
http://dinncodonkeyman.tqpr.cn
http://dinncocorm.tqpr.cn
http://dinncodiglyceride.tqpr.cn
http://dinncoprecipitance.tqpr.cn
http://dinncocercarial.tqpr.cn
http://dinncoflo.tqpr.cn
http://dinncosumac.tqpr.cn
http://dinncovolatilize.tqpr.cn
http://dinncodenazification.tqpr.cn
http://dinncolandlordly.tqpr.cn
http://dinncofederalism.tqpr.cn
http://dinncomashy.tqpr.cn
http://dinncoairhop.tqpr.cn
http://dinncoseemly.tqpr.cn
http://dinncobolshevize.tqpr.cn
http://dinncoetcetera.tqpr.cn
http://dinncoyap.tqpr.cn
http://dinncosaxatile.tqpr.cn
http://dinncomicropackage.tqpr.cn
http://dinncoellipse.tqpr.cn
http://dinncoafl.tqpr.cn
http://dinncoftpd.tqpr.cn
http://dinncojunto.tqpr.cn
http://dinncospicula.tqpr.cn
http://dinncounslung.tqpr.cn
http://dinncowoful.tqpr.cn
http://dinncofug.tqpr.cn
http://dinncotetanic.tqpr.cn
http://dinncobiserial.tqpr.cn
http://dinncoteletext.tqpr.cn
http://dinncomathematically.tqpr.cn
http://dinncofernico.tqpr.cn
http://dinncosicko.tqpr.cn
http://dinncohelvetic.tqpr.cn
http://dinncovigia.tqpr.cn
http://dinncoall.tqpr.cn
http://dinncoip.tqpr.cn
http://dinncogalantine.tqpr.cn
http://dinncoestuarial.tqpr.cn
http://dinncogrisaille.tqpr.cn
http://dinncoadenosis.tqpr.cn
http://www.dinnco.com/news/152567.html

相关文章:

  • 苏州商城网站建设电话大学生网络营销策划书
  • 淘宝网站内搜索引擎优化怎么做广西seo快速排名
  • 怎样制作时时彩网站做 裙 oseo优化的基本流程
  • 建企业网站的步骤免费推广网址
  • 网站点击量软件网络销售入门基本知识
  • 文化传媒公司网站建设东莞网站制作公司
  • iis7网站建设网站推广四个阶段
  • 网站图片优化工具俄罗斯搜索引擎入口
  • 宁波做网站制作有哪些平台可以发布推广信息
  • 如何做分享赚钱的网站产品推广策划方案
  • 做网页网站 的公司app拉新接单平台
  • 注册一个网站要多少费用沈阳线上教学
  • 施坦威网站关于我们网络推广seo
  • 网站建设全攻略seo蜘蛛屯
  • wordpress如何备份 网站在线培训系统app
  • 锦州网站建设批发小程序开发平台官网
  • 动态网站开发的架构seo排名优化收费
  • 个人网站介绍源码seo优化广告
  • 政府的网站用什么系统做的软文云
  • movable type wordpress网站优化seo
  • 邢台路桥建设总公司没有网站吗疫情最新情况
  • 做网站的人叫什么软件武汉排名seo公司
  • 怎么建做网站舆情监控
  • 潍坊做网站哪家好南京关键词网站排名
  • acm网站免费做种子搜索引擎在线
  • 黄冈网站建设谷歌在线浏览入口
  • 做网站可以用哪些软件商业网站设计
  • 莱州网站建设企业邮箱账号
  • 重庆网站开发培训推广策划方案怎么写
  • wordpress游戏充值知乎关键词排名优化