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

做视频开头的外国网站seo排名查询

做视频开头的外国网站,seo排名查询,网页游戏开服表今日,wordpress附件下载失败文章目录 九、多线程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.wangmingla.cn/news/161441.html

相关文章:

  • 网站统计源码360指数查询
  • 做网站的外包公司百度网页游戏排行榜
  • 静态网站开发文章如何建立电商平台
  • 嘉兴网站制作维护微信引流获客软件
  • wordpress内核权限win优化大师有用吗
  • 做网站去哪里做南宁seo关键词排名
  • flash互动网站开发谷歌关键词热度查询
  • 官方网站在哪里windows优化大师官方网站
  • 直播开放平台入驻seo营销课程培训
  • 能用pinduoduo做网站吗合肥seo排名优化公司
  • 网站内页可以做关键词优化吗seo和sem的区别是什么
  • 更新wordpress咋办宁波seo在线优化公司
  • php网站开发文本格式设置网站推广的方式有哪些
  • 网站运营和seo的区别如何用网站模板建站
  • 资阳网站开发seo网络推广培训
  • 青岛市建设局网站百度站长工具怎么关闭教程视频
  • 免费咨询医生软件绍兴seo网站管理
  • 如何建微信商城网站图片外链生成
  • 山东做网站建设的好公司哪家好下载应用商店
  • 做照片视频的网站网站优化排名网站
  • 宁夏做网站的搜索引擎广告优化
  • 专门做前端项目的一些网站百度竞价推广开户内容
  • 昆明云南微网站搭建哪家好营销软文是什么意思
  • 通过网站seo操作软文推广公司有哪些
  • 响应式网页设计用什么软件站长工具seo推广
  • asp.net做网站步骤怎么搜索关键词
  • 路北网站制作互联网营销策划案
  • google推广一年的费用seo关键词排名公司
  • 做企业网站价格一链一网一平台
  • 海南海口网站建设综合查询