c++ - 为什么在使用 MinGW 添加 ltalloc 时会出现段错误

标签 c++ segmentation-fault pthreads mingw32 thread-local-storage

我尝试使用 ltalloc 构建我的应用程序.我用 MinGW32 4.9.1 和 MinGW64-32 4.9.2 试过了。 它可以很好地编译和链接,但是当我运行它时会发生段错误。调试将问题定位到以下代码:

#include <pthread.h>
#pragma weak pthread_once
#pragma weak pthread_key_create
#pragma weak pthread_setspecific
static pthread_key_t pthread_key;
static pthread_once_t init_once = PTHREAD_ONCE_INIT;
static void init_pthread_key() { pthread_key_create(&pthread_key, release_thread_cache); }
static thread_local int thread_initialized = 0;
static void init_pthread_destructor()//must be called only when some block placed into a thread cache's free list
{
    if (unlikely(!thread_initialized))
    {
        thread_initialized = 1;
        if (pthread_once)
        {
            pthread_once(&init_once, init_pthread_key);  // <--- THIS CAUSES THE SEGSEGV
            pthread_setspecific(pthread_key, (void*)1);//set nonzero value to force calling of release_thread_cache() on thread terminate
        }
    }
}

据我所知,这两个版本本身都支持线程本地存储。 ltalloc的wiki也写了如下:

Warning: in some builds of MinGW there is a problem with emutls and order of execution of thread destructor (all thread local variables destructed before it), and termination of any thread will lead to application crash.

不幸的是,这个警告没有告诉我任何信息。谷歌搜索也没有让我变得更聪明。

最佳答案

出乎意料,试试这个:

static void init_pthread_key(void) 
{ 
  if (pthread_key_create)
  {
    pthread_key_create(&pthread_key, release_thread_cache); 
  }
}

同时向 pthread_* 添加完整的错误检查可能不仅有助于调试。

关于c++ - 为什么在使用 MinGW 添加 ltalloc 时会出现段错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29389414/

相关文章:

c - 为什么这个程序会导致段错误?

c - 一种在 c 中获取处理单元(# cpu,cores)nr 的可移植方法?

c - 从线程返回 "string"

c++ - 如何在 C++ 中读取一堆字符串?

C++:迭代 STL 容器的正确方法

C++ : Arrays and memory locations

c++ - BOOST.Test 中的全局固定装置如何工作?

C 段错误字符指针

c++ - 如何完全绕过 `error: no match for ‘operator==’`?

c - pthread 将值返回到数组