Linux 调用std::call_once崩溃问题

#include <iostream>
#include <mutex>

std::once_flag flag;

uint32_t count = 0;

void init()
{
    count = 1;
}

void call_once_func()
{
    std::call_once(flag, init);
}

int main(int argc, char **argv)
{
    call_once_func();
    call_once_func();
    call_once_func();

    printf("%u\n", count);
    return 0;
}

g++ 7.5.0
g++ test_std_call_once.cc
执行a.out会抛出异常
此时需要主动添加一个编译选项-pthread
g++ test_std_call_once.cc -pthread即可正常使用

BUG描述:https://gcc.gnu.org/bugzilla/show_bug.cgi?id=55394

没有链接编译时也不会报错,运行时异常描述也毫无作用,无法分析。

其实问题就出现在有没有链接pthread库,gcc使用的pthread_once函数执行,故需要链接pthread库
但是使用-lpthread会发现它链接不上,-l只影响链接阶段,让其在pthread找到使用的符号文件,如果没有找到,则忽略-lpthread。在gcc代码中使用static __typeof(pthread_key_create) __gthrw___pthread_key_create __attribute__ ((__weakref__("__pthread_key_create"))); 判断是否支持多线程,弱引用意味着如果在链接时找不到 __pthread_key_create,这个引用不会导致链接错误,而是会被替换为 NULL 或者其他默认行为。故__gthread_active_p返回为false,导致__gthread_once返回-1,最终抛出异常。当代码中存在pthread库的符号时,使用-lpthread将会正常调用call_once。

gcc -lpthread和gcc -pthread的区别
故以后代码中可以使用-pthread代替-lpthread
在这里插入图片描述

  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值