C++11 std::thread::detach and access to shared data

时间:2022-09-02 10:09:38

If you have shared variables between a std::thread and the main thread (or any other thread for that matter), can you still access those shared variables even if you execute the thread::detach() method immediately after creating the thread?

如果你在std :: thread和主线程(或任何其他线程)之间共享变量,即使在创建线程后立即执行thread :: detach()方法,你仍然可以访问这些共享变量吗?

2 个解决方案

#1


18  

Yes! Global, captured and passed-in variables are still accessible after calling detach().

是!调用detach()后仍可访问全局变量,捕获变量和传入变量。

However, if you are calling detach, it is likely that you want to return from the function that created the thread, allowing the thread object to go out of scope. If that is the case, you will have to take care that none of the locals of that function were passed to the thread either by reference or through a pointer.

但是,如果您正在调用detach,则可能需要从创建该线程的函数返回,从而允许该线程对象超出范围。如果是这种情况,则必须注意该函数的所有本地都不是通过引用或通过指针传递给线程的。

You can think of detach() as a declaration that the thread does not need anything local to the creating thread.

您可以将detach()视为一个声明,即线程不需要创建线程的任何本地内容。

In the following example, a thread keeps accessing an int on the stack of the starting thread after it has gone out of scope. This is undefined behaviour!

在下面的示例中,一个线程在超出范围后继续访问起始线程的堆栈上的int。这是未定义的行为!

void start_thread()
{
    int someInt = 5;
    std::thread t([&]() {
        while (true)
        {
            // Will print someInt (5) repeatedly until we return. Then,
            // undefined behavior!
            std::cout << someInt << std::endl;
        }
    });

    t.detach();
}

Here are some possible ways to keep the rug from being swept out from under your thread:

这里有一些可能的方法可以防止地毯从你的线程中被扫除:

  • Declare the int somewhere that will not go out of scope during the lifetime of any threads that need it (perhaps a global).
  • 在任何需要它的线程(可能是全局线程)的生命周期内声明int不会超出范围的int。
  • Declare shared data as a std::shared_ptr and pass that by value into the thread.
  • 将共享数据声明为std :: shared_ptr,并将其按值传递给线程。
  • Pass by value (performing a copy) into the thread.
  • 将值(执行副本)传递到线程中。
  • Pass by rvalue reference (performing a move) into the thread.
  • 通过rvalue引用(执行移动)传递到线程中。

#2


6  

Yes. Detaching a thread just means that it cleans up after itself when it is finished and you no longer need to, nor are you allowed to, join it.

是。分离线程只是意味着它在完成后会自行清理,你不再需要,也不允许你加入它。

#1


18  

Yes! Global, captured and passed-in variables are still accessible after calling detach().

是!调用detach()后仍可访问全局变量,捕获变量和传入变量。

However, if you are calling detach, it is likely that you want to return from the function that created the thread, allowing the thread object to go out of scope. If that is the case, you will have to take care that none of the locals of that function were passed to the thread either by reference or through a pointer.

但是,如果您正在调用detach,则可能需要从创建该线程的函数返回,从而允许该线程对象超出范围。如果是这种情况,则必须注意该函数的所有本地都不是通过引用或通过指针传递给线程的。

You can think of detach() as a declaration that the thread does not need anything local to the creating thread.

您可以将detach()视为一个声明,即线程不需要创建线程的任何本地内容。

In the following example, a thread keeps accessing an int on the stack of the starting thread after it has gone out of scope. This is undefined behaviour!

在下面的示例中,一个线程在超出范围后继续访问起始线程的堆栈上的int。这是未定义的行为!

void start_thread()
{
    int someInt = 5;
    std::thread t([&]() {
        while (true)
        {
            // Will print someInt (5) repeatedly until we return. Then,
            // undefined behavior!
            std::cout << someInt << std::endl;
        }
    });

    t.detach();
}

Here are some possible ways to keep the rug from being swept out from under your thread:

这里有一些可能的方法可以防止地毯从你的线程中被扫除:

  • Declare the int somewhere that will not go out of scope during the lifetime of any threads that need it (perhaps a global).
  • 在任何需要它的线程(可能是全局线程)的生命周期内声明int不会超出范围的int。
  • Declare shared data as a std::shared_ptr and pass that by value into the thread.
  • 将共享数据声明为std :: shared_ptr,并将其按值传递给线程。
  • Pass by value (performing a copy) into the thread.
  • 将值(执行副本)传递到线程中。
  • Pass by rvalue reference (performing a move) into the thread.
  • 通过rvalue引用(执行移动)传递到线程中。

#2


6  

Yes. Detaching a thread just means that it cleans up after itself when it is finished and you no longer need to, nor are you allowed to, join it.

是。分离线程只是意味着它在完成后会自行清理,你不再需要,也不允许你加入它。