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

公司网站制作商电商营销推广方案

公司网站制作商,电商营销推广方案,建站重庆,如何用iis做网站本文章记录c创建线程&#xff0c;启动线程和结束线程的代码。 需要注意&#xff0c;编译时需要添加-lpthread依赖。 代码&#xff1a; ThreadTest.h #ifndef TEST_THREAD_TEST_H #define TEST_THREAD_TEST_H#include <thread> #include <mutex>class ThreadTes…

本文章记录c++创建线程,启动线程和结束线程的代码。

需要注意,编译时需要添加-lpthread依赖。

代码:

ThreadTest.h

#ifndef TEST_THREAD_TEST_H
#define TEST_THREAD_TEST_H#include <thread>
#include <mutex>class ThreadTest
{public:void start();void stop();void threadLoop(int a);volatile bool started = false;private:std::thread *mThread;std::mutex mMutex;
};static void threadRun(ThreadTest* threadTest);#endif // TEST_THREAD_TEST_H

ThreadTest.cpp

#include "ThreadTest.h"
#include "iostream"// thread entrance.
static void threadRun(ThreadTest* threadTest){printf("thread start!\n");int a = 0;while (threadTest->started){a++;threadTest->threadLoop(a);std::this_thread::sleep_for(std::chrono::milliseconds(1000));}printf("threadRun method exit!\n");
};// start thread.
void ThreadTest::start(){mMutex.lock();if(started){mMutex.unlock();return;}started = true;printf("thread starting!\n");mThread = new std::thread(threadRun, this);printf("thread started!\n");mMutex.unlock();
};// stop thread.
void ThreadTest::stop(){mMutex.lock();if(!started) {mMutex.unlock();return;}if(started && mThread != nullptr && mThread->joinable()) {started = false;mThread->join();}printf("thread stopped!\n");mMutex.unlock();
};// run in thread.
void ThreadTest::threadLoop(int a){printf("threadLoop, a:%d!\n", a);
};

Test.cpp

#include "ThreadTest.h"
#include "iostream"// thread entrance.
static void threadRun(ThreadTest* threadTest){printf("thread method called!\n");int a = 0;while (threadTest->started){a++;threadTest->threadLoop(a);std::this_thread::sleep_for(std::chrono::milliseconds(1000));}printf("threadRun method exit!\n");
};// start thread.
void ThreadTest::start(){mMutex.lock();if(started){mMutex.unlock();return;}started = true;printf("thread starting!\n");mThread = new std::thread(threadRun, this);printf("thread started!\n");mMutex.unlock();
};// stop thread.
void ThreadTest::stop(){mMutex.lock();if(!started) {mMutex.unlock();return;}if(started && mThread != nullptr && mThread->joinable()) {started = false;mThread->join();}printf("thread stopped!\n");mMutex.unlock();
};// run in thread.
void ThreadTest::threadLoop(int a){printf("threadLoop, a:%d!\n", a);
};

执行:

导入IDE执行,或用g++:
g++ -o test Test.cpp -I ThreadTest.h ThreadTest.cpp -lpthread
./test

输出

hello world!
thread starting!
thread started!
thread method called!
threadLoop, a:1!
threadLoop, a:2!
threadLoop, a:3!
threadRun method exit!
thread stopped!
-----------------
thread starting!
thread method called!
threadLoop, a:1!
thread started!
threadLoop, a:2!
threadLoop, a:3!
threadRun method exit!
thread stopped!
thread starting!
thread method called!
threadLoop, a:1!
thread started!
threadRun method exit!
thread stopped!
thread starting!
thread started!
thread method called!
threadRun method exit!
thread stopped!
thread starting!
thread method called!
threadLoop, a:1!
thread started!
threadRun method exit!
thread stopped!
thread starting!
thread started!
thread method called!
threadRun method exit!
thread stopped!
thread starting!
thread method called!
threadLoop, a:1!
thread started!
threadRun method exit!
thread stopped!
hello world end!
http://www.ahscrl.com/news/12750.html

相关文章:

  • 布吉做网站5118大数据平台官网
  • 深圳网站建设大公司好我是做推广的怎么找客户
  • 昆明网站建设首选互维谷歌账号注册入口官网
  • 那有名网站是php做的营销外包
  • 如何看别人网站用什么做的合肥网站优化技术
  • 分销网站建站代写
  • 做360手机网站优化快长沙seo网站排名优化公司
  • 做外贸网站需要注意些什么手续官网seo优化
  • 做淘客网站用备案系统优化大师
  • 建行官网个人银行seo排名优化教学
  • 宁波网站优化方法电商培训机构需要什么资质
  • 网站商城微信支付接口百度百科合作模式
  • 做网站的公司叫什么问题岳阳网站界面设计
  • 做淘宝主要看哪些网站有哪些内容域名查询
  • 西安企业网站开发哪家好123网址之家
  • 宝塔建的数据库安装不了wordpressseo和sem哪个工资高
  • wordpress beginning南平seo
  • 网站开发合同.docseo 关键词优化
  • 路由器做内部网站服务器成都百度
  • 专门做餐厅设计的网站企业网站模板免费
  • 重庆做兼职哪个网站博客网
  • 怎么在网站上做下载指数函数图像及性质
  • centos6.9安装wordpress应用关键词优化
  • 牡丹江做网站的公司新媒体营销案例ppt
  • 团建智慧登陆官网seo优化公司排名
  • 深圳大型论坛网站建设百度推广深圳分公司
  • 和网站设计人员谈价要注意什么怎样联系百度客服
  • 湖南做网站 就问磐石网络专业网络营销案例ppt
  • 做曖网站深圳关键词推广
  • 华为云网站建设怎么设置选择项广告营销的经典案例