【问题标题】:Initialising with std::call_once() in a multithreading environment [duplicate]在多线程环境中使用 std::call_once() 初始化 [重复]
【发布时间】:2019-05-12 12:15:12
【问题描述】:

我正在阅读C++ Concurrency in Action, 2nd Edition X 一书。本书包含一个示例,该示例使用std::call_once() 函数模板和std::once_flag 对象以线程安全的方式提供某种惰性初始化

这里是本书的简化摘录:

class X {
public:
   X(const connection_details& details): connection_details_{details}
   {}

   void send_data(const data_packet& data) {
      std::call_once(connection_init_, &X::open_connection, this);
      connection_.send(data); // connection_ is used
   }

   data_packet receive_data() {
      std::call_once(connection_init_, &X::open_connection, this);
      return connection_.recv(data); // connection_ is used
   }

private:
   void open_connection() {
      connection_.open(connection_details_); // connection_ is modified
   }

   connection_details connection_details_;
   connection_handle connection_;
   std::once_flag connection_init_;
};

上面的代码所做的是延迟连接的创建,直到客户端想要接收数据或有数据要发送。连接是由open_connection() 私有成员函数创建的,而不是由X 的构造函数创建的。构造函数仅保存连接详细信息以便以后能够创建连接。

上面的open_connection() 成员函数被调用一次,到目前为止一切正常。在单线程上下文中,这将按预期工作。但是,如果多个线程在同一个对象上调用send_data()receive_data() 成员函数怎么办?

显然,open_connection() 中的connection_ 数据成员的修改/更新与它在send_data()receive_data() 中的任何使用都不同步。

std::call_once() 是否会阻塞第二个线程,直到第一个线程从 std::call_once() 返回?


X3.3.1. 节:在初始化期间保护共享数据

【问题讨论】:

标签: c++ multithreading c++11 initialization std-call-once


【解决方案1】:

基于this post 我创建了这个答案。

我想看看std::call_once() 是否与同一std::once_flag 对象上对std::call_once() 的其他调用同步。下面的程序创建了几个线程来调用一个函数,该函数包含对std::call_once() 的调用,该调用使调用线程长时间休眠。

#include <mutex>

std::once_flag init_flag;
std::mutex mtx; 

init_flag 是与std::call_once() 调用一起使用的std::once_flag 对象。互斥体mtx 只是为了避免在将字符从不同线程流式传输到std::coutstd::cout 上的交错输出。

init() 函数是由std::call_once() 调用的函数。它显示文本initialising...,使调用线程休眠三秒钟,然后在返回之前显示文本done

#include <thread>
#include <chrono>
#include <iostream>

void init() {
   {
      std::lock_guard<std::mutex> lg(mtx);
      std::cout << "initialising...";
   }

   std::this_thread::sleep_for(std::chrono::seconds{3});  

   {
      std::lock_guard<std::mutex> lg(mtx);
      std::cout << "done" << '\n';
   }
}

这个函数的目的是休眠足够长的时间(本例中为三秒),以便剩余线程有足够的时间到达std::call_once() 调用。这样我们就可以看到它们是否阻塞,直到执行这个函数的线程从它返回。

函数do_work()main()中创建的所有线程调用:

void do_work() {
   std::call_once(init_flag, init);
   print_thread_id(); 
}

init() 只会被一个线程调用(即,它只会被调用一次)。所有线程都调用print_thread_id(),即对main()中创建的每个线程执行一次。

print_thread_id() 只显示当前线程 id:

void print_thread_id() {
   std::lock_guard<std::mutex> lg(mtx);
   std::cout << std::this_thread::get_id() << '\n';
}

main()中一共创建了16个线程,调用do_work()函数:

#include <vector>

int main() {
   std::vector<std::thread> threads(16);
   for (auto& th: threads)
      th = std::thread{do_work};

   for (auto& th: threads)
      th.join();
}

我在系统上得到的输出是:

initialising...done
0x7000054a9000
0x700005738000
0x7000056b5000
0x700005632000
0x700005426000
0x70000552c000
0x7000055af000
0x7000057bb000
0x70000583e000
0x7000058c1000
0x7000059c7000
0x700005a4a000
0x700005944000
0x700005acd000
0x700005b50000
0x700005bd3000

这个输出意味着没有线程执行print_thread_id(),直到第一个调用std::call_once()的线程从它返回。这意味着这些线程在std::call_once() 调用时被阻塞。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2022-07-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-05-20
    • 2015-09-04
    • 1970-01-01
    • 2014-05-06
    相关资源
    最近更新 更多