资讯

精准传达 • 有效沟通

从品牌网站建设到网络营销策划,从策略到执行的一站式服务

C++11中怎么使用std::thread实现并发

这篇文章给大家介绍C++11中怎么使用std::thread 实现并发,内容非常详细,感兴趣的小伙伴们可以参考借鉴,希望对大家能有所帮助。

在南票等地区,都构建了全面的区域性战略布局,加强发展的系统性、市场前瞻性、产品创新能力,以专注、极致的服务理念,为客户提供成都做网站、网站制作 网站设计制作按需开发网站,公司网站建设,企业网站建设,成都品牌网站建设,全网营销推广,成都外贸网站制作,南票网站建设费用合理。

std::thread 构造

default (1)
thread() noexcept;
 
initialization (2)
template 
explicit thread (Fn&& fn, Args&&... args);
 
copy [deleted] (3)
thread (const thread&) = delete;
 
move (4)
thread (thread&& x) noexcept;
 

(1). 默认构造函数,创建一个空的 thread 执行对象。
(2). 初始化构造函数,创建一个 thread对象,该 thread对象可被 joinable,新产生的线程会调用 fn 函数,该函数的参数由 args 给出。
(3). 拷贝构造函数(被禁用),意味着 thread 不可被拷贝构造。
(4). move 构造函数,move 构造函数,调用成功之后 x 不代表任何 thread 执行对象。

注意:可被 joinable 的 thread 对象必须在他们销毁之前被主线程 join 或者将其设置为 detached.

std::thread 各种构造函数例子如下(参考):

#include 
#include 
#include 
#include 
#include 
#include 
 
void f1(int n)
{
  for (int i = 0; i < 5; ++i) {
    std::cout << "Thread " << n << " executing\n";
    std::this_thread::sleep_for(std::chrono::milliseconds(10));
  }
}
 
void f2(int& n)
{
  for (int i = 0; i < 5; ++i) {
    std::cout << "Thread 2 executing\n";
    ++n;
    std::this_thread::sleep_for(std::chrono::milliseconds(10));
  }
}
 
int main()
{
  int n = 0;
  std::thread t1; // t1 is not a thread
  std::thread t2(f1, n + 1); // pass by value
  std::thread t3(f2, std::ref(n)); // pass by reference
  std::thread t4(std::move(t3)); // t4 is now running f2(). t3 is no longer a thread
  t2.join();
  t4.join();
  std::cout << "Final value of n is " << n << '\n';
}

move 赋值操作

move (1)
thread& operator= (thread&& rhs) noexcept;
 
copy [deleted] (2)
thread& operator= (const thread&) = delete;
 

(1). move 赋值操作,如果当前对象不可 joinable,需要传递一个右值引用(rhs)给 move 赋值操作;如果当前对象可被 joinable,则 terminate() 报错。
(2). 拷贝赋值操作被禁用,thread 对象不可被拷贝。

请看下面的例子:

#include 
#include 

#include   // std::chrono::seconds
#include  // std::cout
#include   // std::thread, std::this_thread::sleep_for

void thread_task(int n) {
  std::this_thread::sleep_for(std::chrono::seconds(n));
  std::cout << "hello thread "
    << std::this_thread::get_id()
    << " paused " << n << " seconds" << std::endl;
}

/*
 * === FUNCTION =========================================================
 *     Name: main
 * Description: program entry routine.
 * ========================================================================
 */
int main(int argc, const char *argv[])
{
  std::thread threads[5];
  std::cout << "Spawning 5 threads...\n";
  for (int i = 0; i < 5; i++) {
    threads[i] = std::thread(thread_task, i + 1);
  }
  std::cout << "Done spawning threads! Now wait for them to join\n";
  for (auto& t: threads) {
    t.join();
  }
  std::cout << "All threads joined.\n";

  return EXIT_SUCCESS;
} /* ---------- end of function main ---------- */

其他成员函数

get_id
获取线程 ID。

joinable
检查线程是否可被 join。

join
Join 线程。

detach
Detach 线程

swap
Swap 线程 。

native_handle
返回 native handle。

hardware_concurrency [static]
检测硬件并发特性。

关于C++11中怎么使用std::thread 实现并发就分享到这里了,希望以上内容可以对大家有一定的帮助,可以学到更多知识。如果觉得文章不错,可以把它分享出去让更多的人看到。


分享标题:C++11中怎么使用std::thread实现并发
文章分享:http://cdkjz.cn/article/gdiiej.html
多年建站经验

多一份参考,总有益处

联系快上网,免费获得专属《策划方案》及报价

咨询相关问题或预约面谈,可以通过以下方式与我们联系

业务热线:400-028-6601 / 大客户专线   成都:13518219792   座机:028-86922220