Linux std::thread and std::bind example

22 篇文章 0 订阅
4 篇文章 0 订阅

 

代码

#include <iostream>       // std::cout
#include <atomic>         // std::atomic
#include <thread>         // std::thread
#include <vector>         // std::vector
#include <functional>
#include <algorithm>


#include <mutex>
#include <functional>
#include <sstream>
#include <sys/types.h>
#include <unistd.h>
#include <sys/syscall.h>

class Task
{
    public:
    void executeThread(std::string command)
    {
            pid_t x = syscall(__NR_gettid);
                std::cout<<"execute thread id:"<< x <<"\n";

                std::stringstream ss;
                ss << std::this_thread::get_id();
                uint64_t threadid = std::stoull(ss.str());
                std::cout<<"execute thread id:"<< threadid <<"\n";
            std::cout<<"execute thread:" <<  std::this_thread::get_id() << "\n";
        for(int i = 0; i < 5; i++)
        {
            std::cout << command << " :: " << i << std::endl;
        }
    int i = 0;
    std::cout<<"execute thread input a number!";
    std::cin >> i;
    }

    void startThread(void);
    std::thread th2;
    std::thread th3;
};

void Task::startThread(void)
{
    #if 0
        std::thread th1(&Task::executeThread, this, "Thread1");
        th1.join(); // Without join(), thread will terminate
    #elif 0
        th2 = std::thread(&Task::executeThread, this, "Thread2");
        th2.join();
    #else
        th3 = std::thread( std::bind(&Task::executeThread, this, "Thread3") );
        th3.join();
    #endif
}

int main()
{
            pid_t x = syscall(__NR_gettid);
                std::cout<<"main thread id:"<< x <<"\n";
        std::cout<<"main:" <<  std::this_thread::get_id() << "\n";
                        std::stringstream ss;
                ss << std::this_thread::get_id();
                uint64_t threadid = std::stoull(ss.str());
                std::cout<<"main thread id:"<< threadid <<"\n";
    Task* taskPtr = new Task();
    std::cout << "\ntaskPtr: " << taskPtr << std::endl;

    taskPtr->startThread();
    int i = 0;
    std::cout<<"input a number!";
    std::cin >> i;

    delete taskPtr;
    return 0;
}

编译

g++ -g -Wall -std=c++11 -o task task.cpp -lpthread

结果

root@7f4dc6e8ef5e:/test# ./task
main thread id:6058
main:140158864688960
main thread id:140158864688960

taskPtr: 0x561c5978b280
start thread id:140158864688960
start thread:140158864688960
execute thread id:6059
execute thread id:140158846494464
execute thread:140158846494464
Thread3 :: 0
Thread3 :: 1
Thread3 :: 2
Thread3 :: 3
Thread3 :: 4

查看thread id

root@7f4dc6e8ef5e:/test# ps -Tp 6058
  PID  SPID TTY          TIME CMD
 6058  6058 pts/3    00:00:00 task
 6058  6059 pts/3    00:00:00 task
 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值