【发布时间】:2022-12-18 11:40:42
【问题描述】:
已解决 07/12另一个文件是这里的罪魁祸首!
问题这个非常短的 Mutex 代码似乎在 .见下图:
下面提供了复制此问题的代码:
struct test_struct {
pthread_mutex_t test_lock;
} *test_ptr;
void test(){
test_ptr = malloc(sizeof(struct test_struct));
pthread_mutex_init(&test_ptr->test_lock, NULL);
pthread_mutex_lock(&test_ptr->test_lock);
fprintf(stderr, "Reached here\n");
return;
}
int main(int argc, char *argv[])
{
test();
return 0;
}
我还尝试了什么?
- 我已将互斥锁设为全局变量(无结构)
- 我已将互斥量作为测试函数的一部分 ()
【问题讨论】:
-
无法重现,它工作正常并打印
Reached here,问题一定出在其他地方。 -
您是否包括了所有必需的标头?使用不带标头的
malloc可能会导致奇怪的事情发生。 -
已解决:框架中有另一个文件定义了 pthread_mutex_init 和 pthread_mutex_lock。所以,以上是行不通的。我很抱歉不必要地占用大家的时间:(
-
您应该检查
malloc的返回值是否为NULL。
标签: c memory-management segmentation-fault mutex