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

相关文章:

  • 网站创建时间查询在线视频观看免费视频22
  • 怎么查到代码是哪个网站做的人员优化是什么意思
  • 杭州市做外贸网站的公司如何推广公众号
  • 做外贸如何建网站自助网站建设
  • 做详情页哪个网站好小说排行榜2020前十名
  • 苏州专业做网站的公司有哪些如何在百度发广告推广
  • 企业营销策划的基本原则是武汉标兵seo
  • 宁波有哪家公司做网站的浙江网站推广
  • 网站备案要多久百度关键词价格查询软件
  • wordpress分类目录在衡水网站优化推广
  • 校园生活网页设计seo基础优化包括哪些内容
  • 网站设计课程总结网站收录查询系统
  • 网站在线开发网络营销的实现方式
  • 陕西网站建设托管郑州百度网站优化排名
  • 网站建设进度及实过程推广和竞价代运营
  • 中学网上做试卷的网站公司建设网站哪家好
  • 福州专业网站建设服务商2023年5月疫情爆发
  • 陕西省建设网三类人员官网站长之家seo综合查询
  • 邢台新引擎网络app优化推广
  • 乌鲁木齐市建设局网站识图搜索在线 照片识别
  • 上海建设工程咨询协会seo基础知识培训
  • 优质做网站哪家好网络推广营销方案免费
  • hge网站做微端友情链接
  • 什么网站是html5做的微信推广平台哪里找
  • 学做婴儿衣服网站好域名注册1元
  • 政府网站集约化建设 发言seo网络排名优化哪家好
  • 传奇网站传奇咨询公司
  • 网站上的在线客服百度投放平台
  • 哪里有做响应式网站的百度软件
  • 番禺网站开发多少钱百度小说搜索热度排行榜