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

哪个网站可以改字体邙山网站建设

哪个网站可以改字体,邙山网站建设,成都住建局官网房源,门户网站建设 报告文章目录 九、多线程2. 线程的控制 未完待续 九、多线程 2. 线程的控制 主线程退出 等同于 进程退出 等同于 所有线程都退出。为了避免主线程退出,但是新线程并没有执行完自己的任务的问题,主线程同样要跟进程一样等待新线程返回。 pthread_join 函数…

文章目录

  • 九、多线程
    • 2. 线程的控制
  • 未完待续


九、多线程

2. 线程的控制

主线程退出 等同于 进程退出 等同于 所有线程都退出。为了避免主线程退出,但是新线程并没有执行完自己的任务的问题,主线程同样要跟进程一样等待新线程返回。
在这里插入图片描述
pthread_join 函数则可以等待新线程返回。
Makefile

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

testThread.cc

#include <iostream>
#include <pthread.h>
#include <unistd.h>
using namespace std;// 新线程
void* newpthreadrun(void* arg)
{int cnt = 5;while (cnt--){cout << "I am newpthreadrun thread" << endl;sleep(1);}
}int main()
{pthread_t tid;pthread_create(&tid, nullptr, newpthreadrun, nullptr);// 主线程pthread_join(tid, nullptr);cout << "I am main thread" << endl;sleep(5);return 0;
}

在这里插入图片描述
多线程中,任何一个线程出现异常,都会导致整个进程退出 ---- 多线程代码往往健壮性不好。 不能用 exit() 来退出线程,因为 exit() 是退出进程的,可以使用 pthread_exit() 来退出线程,也可以在主线程中使用 pthread_cancel() 来取消新线程线程,新线程会返回 -1。
我们来创建一个多线程:
Makefile

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

testThread.cc

#include <iostream>
#include <string>
#include <pthread.h>
#include <unistd.h>
#include <vector>
#include <cstdio>
#include <cstdlib>
using namespace std;const int threadnum = 5;// 加法任务
class Task
{
public:Task(int x, int y):datax(x),datay(y){}int Excute(){return datax + datay;}~Task() {}
private:int datax;int datay;
};// 线程数据
class ThreadData
{
public:ThreadData(int x, int y, const string& threadname):t(x, y),threadname(threadname){}string Threadname(){return threadname;}int run(){return t.Excute();}~ThreadData() {}
private:string threadname;Task t;
};// 执行任务后的结果
class Result
{
public:Result() {}void SetResult(int result, const string& threadname){_result = result;_threadname = threadname;}void Print(){cout << _threadname << " run result: " << _result << endl;}~Result() {}
private:int _result;string _threadname;
};// 线程执行任务
void* handlerTask(void* args)
{ThreadData* td = static_cast<ThreadData*>(args);string name = td->Threadname();Result* res = new Result();int result = td->run();res->SetResult(result, name);return res;
}int main()
{// 创建线程并分配任务vector<pthread_t> threads;for (int i = 0; i < threadnum; i++){char threadname[64];snprintf(threadname, 64, "Thread-%d", i + 1);ThreadData* td = new ThreadData(10, 20, threadname);pthread_t tid;pthread_create(&tid, nullptr, handlerTask, td);threads.push_back(tid);}// 等待线程执行完毕并获取结果vector<Result*> result_set;void* ret = nullptr;for (auto& tid : threads){pthread_join(tid, &ret);result_set.push_back((Result*)(ret));}// 打印结果for (auto& res : result_set){res->Print();delete res;}return 0;
}

执行结果:
在这里插入图片描述


未完待续

http://www.yayakq.cn/news/400250/

相关文章:

  • wordpress 测速惠州网站关键字优化
  • wordpress 千易网盘如何做网站seo诊断
  • 凡科网站能在百度做推广吗长春网站优化体验
  • 中国热门网站外管局网站上做存量权益登记
  • 建设一个小说网站多少钱重庆网站建设坤思特
  • 域名可以做网站名吗门户网站建设价格
  • 婚庆网站大全WordPress单栏二次元主题
  • 内蒙古自治区住房和城乡建设部网站网站建设服务费入
  • 企业网站托管有必要吗特种设备作业人员证查询
  • html5做手机网站建设php网站开发结构说明
  • 成都制作网站公司简介自己能搞定吗?
  • 网站成本费用我需要把网站做
  • 下沙建设局网站域名解析ip
  • 渭南市建网站深圳光明区最新消息
  • 百度关键词分析慈溪seo排名
  • 二手房网站谁做的更好产品设计公司介绍
  • 芜湖做网站设计的公司在线logo生成器标智客
  • 怎么判断网站有没有做百度商桥网站开发人员薪酬
  • 男生女生做羞羞事的网站网站服务器租用多少钱才合理呢
  • 网站优化是在哪里做修改wordpress 未能连接到ftp服务器
  • 网站实现步骤及方法电商网站建设需求分析书
  • 网站维护和网页维护区别网站建设风格
  • 北京建设信源公司网站免费网站 推广网站
  • 企业网络建站学设计需要多少钱
  • c2c的网站名称和网址手机怎么做弹幕小视频网站
  • 可以自己做网站的网址市场调研
  • 站点与网站有什么区别网页设计公司申请
  • 整站下载器 做网站地图微信如何制作一个网页
  • 在线看免费观看视频网站大全百度做网站优化多少钱一年
  • 专业网站优化公司报价120平方装修预算表