相关疑难解决方法(0)

在Visual Studio中,与std :: async一起使用时未调用`thread_local`变量'析构函数,这是一个错误吗?

以下代码

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

std::mutex m;

struct Foo {
    Foo() {
        std::unique_lock<std::mutex> lock{m};
        std::cout <<"Foo Created in thread " <<std::this_thread::get_id() <<"\n";
    }

    ~Foo() {
        std::unique_lock<std::mutex> lock{m};
        std::cout <<"Foo Deleted in thread " <<std::this_thread::get_id() <<"\n";
    }

    void proveMyExistance() {
        std::unique_lock<std::mutex> lock{m};
        std::cout <<"Foo this = " << this <<"\n";
    }
};

int threadFunc() {
    static thread_local Foo some_thread_var;

    // Prove the variable initialized
    some_thread_var.proveMyExistance();

    // The thread runs for some time
    std::this_thread::sleep_for(std::chrono::milliseconds{100}); 

    return 1;
}

int …
Run Code Online (Sandbox Code Playgroud)

c++ multithreading memory-leaks visual-studio

22
推荐指数
1
解决办法
954
查看次数

为什么 C++ 标准库中没有 std::thread_pool ?

我觉得奇怪的是,尽管有大量的多线程构造,但该标准却缺少线程池类。什么原因可能会阻止委员会将其添加到标准中?

c++ multithreading std language-lawyer threadpool

7
推荐指数
1
解决办法
2070
查看次数