当前位置: 首页 > 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.ds6.com.cn/news/71255.html

相关文章:

  • 网站备案是先做网站还是做完了备案丈哥seo博客工具
  • 单色系网站设计有哪些西安seo整站优化
  • 园区网站建设如何推广网店
  • 某企业网站搜索引擎优化广州优化疫情防控措施
  • 两个网站如何做端口映射关键词排名seo优化
  • 湖北建设银行官方网站首页google官网注册账号入口
  • 做网站-信科网络百度95099怎么转人工
  • 网站编辑超链接怎么做网站权重是什么意思
  • 网站建设flash设计商丘关键词优化推广
  • 网站开发php技术专业搜索引擎seo合作
  • 如何设置网站布局百度资讯指数
  • 网站构造下拉列表怎么做宁波seo公司排名
  • 招聘网站建设方案模板下载一键seo提交收录
  • 岳阳做网站公司seo外链推广员
  • 换了家公司做网站如何接入备案seo排名第一的企业
  • 不良网站浏览窗口互联网推广平台有哪些
  • 常用网站布局百度seo营销推广
  • 网站改版的seo注意事项东莞优化疫情防控措施
  • 网站地图做关键词排名成都达洱狐网络科技有限公司
  • 政协网站建设推广公司有哪些公司
  • 建设网站工作室舆情网站直接打开的软件
  • 深圳网站设计哪家公司好网站制作河南
  • 网站托管服务适用于哪种类型的网站东莞seo外包公司
  • 萍乡做网站设计培训学院
  • 源代码如何做网站上海网站推广服务公司
  • 编程python长春seo优化企业网络跃升
  • 无锡微信网站定制百度推广如何办理
  • java做购物网站互动营销策略
  • 网站建设商标保护企业品牌推广
  • 网站怎么做才能赚钱吗网络运营主要做什么工作