相关疑难解决方法(0)

通过引用将参数传递给std :: thread函数是否安全?

#include <thread>
#include <string>
#include <vector>
#include <chrono>

using namespace std;

void f(const vector<string>& coll)
{
    this_thread::sleep_for(1h);

    //
    // Is coll guaranteed to be valid before exiting this function?
    //
}

int main()
{
    {
        vector<string> coll(1024 * 1024 * 100);
        thread(f, coll).detach();
    }

    //
    // I know std::thread will copy arguments into itself by default, 
    // but I don't know whether these copied objects are still valid
    // after the std::thread object has been destroyed.
    //

    while (true);
} …
Run Code Online (Sandbox Code Playgroud)

c++ standards multithreading object-lifetime c++11

15
推荐指数
2
解决办法
1235
查看次数

标签 统计

c++ ×1

c++11 ×1

multithreading ×1

object-lifetime ×1

standards ×1