You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

sem_wait()触发SIGBUS异常问题求助

sem_wait()触发SIGBUS异常问题求助

各位大佬好,我最近在写一个Linux下的C++命令行程序,逻辑是主线程靠信号量阻塞,等后台线程完成任务后解锁信号量,主线程再退出。但现在碰到个棘手的问题:sem_wait()调用的时候触发了SIGBUS异常,折腾了好一阵都没找到原因,想请大家帮忙分析下!

先给大家贴下相关代码:

首先是类A里静态信号量的定义:

// Static definition of the semaphore object. This semaphore object is 
// declared as a static array of 32 bytes in class A
uint8_t  A::semaphore[sizeof(sem_t)];

然后是main()函数里的相关逻辑:

// In int main()
if(sem_init ((sem_t *) A::semaphore, 0, 0)) {
    // failed to initialize semaphore
}

printf("semaphore = %p\n", A::semaphore);  // Prints 0x129ba01

// Create another thread(s) to execute work

// Main thread gets blocked here... until thread(s) complete their work.
sem_wait ((sem_t *) A::semaphore);

// Once main thread gets unblocked, return from int main() with return value

我还自己加了自定义的异常处理器,但还是没搞懂为什么会触发SIGBUS。有没有大佬能帮我看看问题出在哪?

备注:内容来源于stack exchange,提问作者NightFuryLxD

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.04.15 14:59:51