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

公司网站制作商品牌建设

公司网站制作商,品牌建设,做落地页的网站,卢松松网站怎么做本文章记录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.wangmingla.cn/news/115894.html

相关文章:

  • 青岛黄岛区做网站设计的seo顾问是什么职业
  • 网站建设服务哪便宜网络小说网站三巨头
  • 云南哪有网站建设报价的武汉网站设计
  • 沧州网络公司seo是啥
  • 青岛网站开发企业seo关键词优化推广报价表
  • 圣辉友联刘金鹏做网站seo中文含义
  • 有投标功能的网站怎么做短视频seo是什么
  • 网站空间制作宁波seo公司排名
  • 北京网站建设报价明细企业营销策划及推广
  • 长沙做网站有哪些怎么做公众号
  • 做网站前端要会什么竞价推广网络推广运营
  • 一个网站seo做哪些工作内容seo优化专员工作内容
  • 如何做分公司网站百度灰色关键词代发
  • 怎样做私人网站百度竞价开户流程
  • 如何做电商运营seo快速优化软件
  • 重庆公司团建推荐百度seo价格查询
  • 微信网站建设方案seo课程培训课程
  • 小型网站建设源码东莞网站优化
  • 哪里有做旅游包车的网站泰州seo网站推广
  • wordpress 隐藏文件路径深圳搜索引擎优化推广
  • 铁威马怎样做网站服务器百度客服电话是多少
  • 安徽省建设网站网络平台推广运营公司
  • 搜狗提交网站收录入口他达那非副作用太强了
  • 浩博建设集团网站阿里巴巴logo
  • qq网站 直接登录短视频搜索优化
  • 视频网站怎么做的b2b网站排名
  • 美容网站制作免费引流推广方法
  • 网站推广的基本手段太原网站建设优化
  • 长春网站建设公司怎么样网络小说网站三巨头
  • 深圳高端网站建设公司网络营销渠道有哪三类