小编Ste*_*oll的帖子

使用thread_local时gcc 4.8.1中的内存泄漏?

Valgrind在以下代码中报告泄漏的块,显然是每个线程一个:

#include <iostream>
#include <thread>
#include <mutex>
#include <list>
#include <chrono>

std::mutex cout_mutex;

struct Foo
{
    Foo() 
    { 
        std::lock_guard<std::mutex> lock( cout_mutex );
        std::cout << __PRETTY_FUNCTION__ << '\n'; 
    }

    ~Foo() 
    { 
        std::lock_guard<std::mutex> lock( cout_mutex );
        std::cout << __PRETTY_FUNCTION__ << '\n'; 
    }

    void 
    hello_world() 
    { 
        std::lock_guard<std::mutex> lock( cout_mutex );
        std::cout << __PRETTY_FUNCTION__ << '\n'; 
    }
};

void
hello_world_thread()
{
    thread_local Foo foo;

    // must access, or the thread local variable may not be instantiated
    foo.hello_world();

    // keep the thread around momentarily
    std::this_thread::sleep_for( …
Run Code Online (Sandbox Code Playgroud)

thread-local-storage c++11 gcc4.8

13
推荐指数
1
解决办法
1699
查看次数

标签 统计

c++11 ×1

gcc4.8 ×1

thread-local-storage ×1