c++线程thread示例

本文章记录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!

相关推荐

  1. c++线thread示例

    2024-01-29 15:10:02       47 阅读
  2. C#多线之(Thread)详解与示例

    2024-01-29 15:10:02       29 阅读
  3. 02_c++多线_this_thread

    2024-01-29 15:10:02       54 阅读
  4. C++之多线(multi-thread

    2024-01-29 15:10:02       71 阅读
  5. C++11 Thead线线

    2024-01-29 15:10:02       43 阅读
  6. C++】学习记录--Thread线库的使用

    2024-01-29 15:10:02       45 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-01-29 15:10:02       94 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-01-29 15:10:02       100 阅读
  3. 在Django里面运行非项目文件

    2024-01-29 15:10:02       82 阅读
  4. Python语言-面向对象

    2024-01-29 15:10:02       91 阅读

热门阅读

  1. FPGA硬件架构

    2024-01-29 15:10:02       60 阅读
  2. linux ping 某台服务的端口

    2024-01-29 15:10:02       47 阅读
  3. 【面试】MySQL的几种查询方式

    2024-01-29 15:10:02       62 阅读
  4. flink源码分析 - jar包中提取主类和第三方依赖

    2024-01-29 15:10:02       45 阅读
  5. iOS 闭包和Block的区别

    2024-01-29 15:10:02       60 阅读
  6. Nginx加固安全策略,简单实用

    2024-01-29 15:10:02       57 阅读