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

网站开发工程师岗位概要/新网站seo

网站开发工程师岗位概要,新网站seo,什么是自建站,模板建站推荐东方靠谱文章目录 九、多线程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.ho-use.cn/article/85.html

相关文章:

  • 广东省建设部网站/网络优化网站
  • 企业做网站的发票会计分录/seo和sem分别是什么
  • 三峡建设委员会网站/网站推广途径和推广要点
  • 网站导航css源代码/云搜索app下载
  • 重庆做的好的房产网站好/百度搜索引擎收录入口
  • 长沙优秀网站建设/百度一下的网址
  • 信息化建设 网站建设等方面/爱站网综合查询
  • 普通话/济南seo外包服务
  • mvc5网站开发之美电子版/百度推广个人能开户吗
  • WordPress微信推广返佣/seo关键词布局
  • wordpress 整站ssl/百度权重怎么看
  • 网站优化和推广/线下推广渠道有哪些方式
  • wordpress批量上传商品/河北seo基础
  • 永兴县网站建设哪家好/友链目录网
  • 上海专业的网站建设公司排名/互联网公司网站模板
  • 网站开发为什么要用框架/百度推广托管
  • 如何申请做网站/百度竞价seo排名
  • 怎样做阿里巴巴网站/搜索引擎优化百度百科
  • 网站域名被做网站的公司擅自更改/seo搜索引擎优化是
  • wordpress制作论坛/网站seo入门基础教程
  • 有专门做序列图的网站/电商平台开发需要多少钱
  • 装修平面设计图的制作/暴疯团队seo课程
  • 昆明网站制作推荐/如何提升网站seo排名
  • 手机怎么做自己的网站/b站视频怎么快速推广
  • 网站开发工具论文/电商培训课程
  • 黄骅贴吧金宝/上海seo博客
  • 优易官方网站/国内搜索引擎排名第一的是
  • 网站 攻击 刷流量/图们网络推广
  • 坪山网站建设基本流程怎样在百度上建立网站
  • 社交和门户网站的区别/厦门人才网招聘官网