安徽网站设计平台,怎样做销售网站,菠菜网站开发,网站开发经验总结与教训本文章记录c创建线程#xff0c;启动线程和结束线程的代码。
需要注意#xff0c;编译时需要添加-lpthread依赖。
代码#xff1a;
ThreadTest.h
#ifndef TEST_THREAD_TEST_H
#define TEST_THREAD_TEST_H#include thread
#include mutexclass ThreadTes…本文章记录c创建线程启动线程和结束线程的代码。
需要注意编译时需要添加-lpthread依赖。
代码
ThreadTest.h
#ifndef TEST_THREAD_TEST_H
#define TEST_THREAD_TEST_H#include thread
#include mutexclass 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_HThreadTest.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!