C++中的std::thread怎么使用

发布时间:2023-04-20 16:54:43 作者:iii
来源:亿速云 阅读:110

这篇文章主要讲解了“C++中的std::thread怎么使用”,文中的讲解内容简单清晰,易于学习与理解,下面请大家跟着小编的思路慢慢深入,一起来研究和学习“C++中的std::thread怎么使用”吧!

std::thread简介

C++11之前,window和linux平台分别有各自的多线程标准,使用C++编写的多线程往往是依赖于特定平台的。

在C++11新标准中,可以简单通过使用thread库,来管理多线程。thread库可以看做对不同平台多线程API的一层包装;因此使用新标准提供的线程库编写的程序是跨平台的。

一、C++11 线程创建

#include <thread>

二、std::thread 的构造函数中接收什么参数?

std::thread thObj(<CALLBACK>);
#include <thread>

void thread_function() {
    for(int i = 0; i < 10000; i++);
        std::cout<<"thread function Executing"<<std::endl;
}

int main() {
    std::thread threadObj(thread_function);
    for(int i = 0; i < 10000; i++);
        std::cout<<"Display From MainThread"<<std::endl;
    threadObj.join();    
    std::cout<<"Exit of Main function"<<std::endl;
    return 0;
}
#include <iostream>
#include <thread>
class DisplayThread {
public:
    void operator()() {
        for(int i = 0; i < 10000; i++)
            std::cout<<"Display Thread Executing"<<std::endl;
    }
};

int main() {
    std::thread threadObj( (DisplayThread()) );
    for(int i = 0; i < 10000; i++)
        std::cout<<"Display From Main Thread "<<std::endl;
    std::cout<<"Waiting For Thread to complete"<<std::endl;
    threadObj.join();
    std::cout<<"Exiting from Main Thread"<<std::endl;
    return 0;
}
#include <iostream>
#include <thread>
int main() {
    int x = 9;
    std::thread threadObj([]{
            for(int i = 0; i < 10000; i++)
                std::cout<<"Display Thread Executing"<<std::endl;
            });

    for(int i = 0; i < 10000; i++)
        std::cout<<"Display From Main Thread"<<std::endl;

    threadObj.join();
    std::cout<<"Exiting from Main Thread"<<std::endl;
    return 0;
}
std::thread::get_id()

获取当前线程的 ID:

std::this_thread::get_id()
#include <iostream>
#include <thread>
void thread_function() {
    std::cout<<"Inside Thread :: ID  = "<<std::this_thread::get_id()<<std::endl;    
}
int main() {
    std::thread threadObj1(thread_function);
    std::thread threadObj2(thread_function);

    if(threadObj1.get_id() != threadObj2.get_id())
        std::cout<<"Both Threads have different IDs"<<std::endl;

    std::cout<<"From Main Thread :: ID of Thread 1 = "<<threadObj1.get_id()<<std::endl;    
    std::cout<<"From Main Thread :: ID of Thread 2 = "<<threadObj2.get_id()<<std::endl;    

    threadObj1.join();    
    threadObj2.join();    
    return 0;
}

三、std::thread 的搭配用法

① std::promise

#include <iostream>
#include <future>
#include <thread>

using namespace std;

int main() {
  promise<int> a_promise;
  auto a_future = a_promise.get_future();
  a_promise.set_value(10);
  cout << a_future.get() << endl;
  cout << "after get()" << endl;
  return 0;
}

输出结构是:

10
after get()

② std::packaged_task

C++11 很贴心地提供 packaged_task 类型,可以不用直接使用 std::thread 和 std::promise,直接就能够生成线程,派遣任务:

#include <iostream>
#include <future>

using namespace std;

int f() {
  string hi = "hello, world!";
  cout << hi << endl;
  return hi.size();
}

int main() {
  packaged_task<int ()> task(f);
  auto result = task.get_future();

  task();

  cout << result.get() << endl;

  return 0;
}

运行结果为:

hello, world!
13

③ std::async

#include <iostream>
#include <vector>
#include <algorithm>
#include <numeric>
#include <future> 

template <typename RandomIt>

int parallel_sum(RandomIt beg, RandomIt end) {
    auto len = end - beg;
    if (len < 1000) 
        return std::accumulate(beg, end, 0);

    RandomIt mid = beg + len/2;
    auto handle = std::async(std::launch::async,
                             parallel_sum<RandomIt>, mid, end);
    int sum = parallel_sum(beg, mid);
    return sum + handle.get();
}

int main() {
    std::vector<int> v(10000, 1);
    std::cout << "The sum is " << parallel_sum(v.begin(), v.end()) << '\n';
}

运行结果:

The sum is 10000

④ std::this_thread

#include <iostream>
#include <future>
#include <thread>
using namespace std;

int f(promise<int> my_promise) {
  string hi = "hello, world!";

  my_promise.set_value(hi.size());

  this_thread::sleep_for(0.1s);
  cout << hi << endl;
}

int main() {
  promise<int> f_promise;

  auto result = f_promise.get_future();

  thread f_thread(f, move(f_promise));
  cout << result.get() << endl;

  f_thread.join();

  return 0;
}

感谢各位的阅读,以上就是“C++中的std::thread怎么使用”的内容了,经过本文的学习后,相信大家对C++中的std::thread怎么使用这一问题有了更深刻的体会,具体使用情况还需要大家实践验证。这里是亿速云,小编将为大家推送更多相关知识点的文章,欢迎关注!

推荐阅读:
  1. microsoft visual c++可不可以卸载
  2. C++如何建立链式二叉树

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

c++ std::thread

上一篇:java常用API之包装类的作用是什么

下一篇:flutter图片组件核心类源码分析

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》