【问题标题】:Member mutex causes SegFault成员互斥锁导致 SegFault
【发布时间】:2015-07-11 22:27:43
【问题描述】:

我不明白为什么下面的代码会导致分段错误。

如果我删除对 pushLock.lock() 和 .unlock() 的调用,它运行良好。

#include <mutex>
#include <queue>

class FunctionQueue{
public:
    FunctionQueue();
    ~FunctionQueue();
    void pushInt(int);
private:
    std::mutex pushLock;
    int currentPushQueue;
    std::queue<int> instructionQueues[2];
};

FunctionQueue::FunctionQueue(){
    instructionQueues[0] = std::queue<int>();
    instructionQueues[1] = std::queue<int>();
    // pushLock.unlock();
}

FunctionQueue::~FunctionQueue(){}

void FunctionQueue::pushInt(int newArgument){
    pushLock.lock();
    instructionQueues[currentPushQueue].push(newArgument);
    pushLock.unlock();
}

int main(int argc, char* argv[]){
    FunctionQueue testQueue;
    testQueue.pushInt(10);
}

gdb BackTrace 的输出非常无用:

Program received signal SIGSEGV, Segmentation fault.
0x0000000000000000 in ?? ()
(gdb) bt
#0  0x0000000000000000 in ?? ()
#1  0x00007ffff347a291 in ?? () from /lib/x86_64-linux-gnu/libdl.so.2
#2  0x00007ffff347a6d7 in ?? () from /lib/x86_64-linux-gnu/libdl.so.2
#3  0x00007ffff347a198 in dlsym () from /lib/x86_64-linux-gnu/libdl.so.2
#4  0x00007ffff7904b3e in ?? () from /usr/lib/nvidia-331/libGL.so.1
#5  0x00007ffff78e8db4 in ?? () from /usr/lib/nvidia-331/libGL.so.1
#6  0x00007ffff7dea0fd in ?? () from /lib64/ld-linux-x86-64.so.2
#7  0x00007ffff7dea223 in ?? () from /lib64/ld-linux-x86-64.so.2
#8  0x00007ffff7ddb30a in ?? () from /lib64/ld-linux-x86-64.so.2
#9  0x0000000000000001 in ?? ()
#10 0x00007fffffffe8a6 in ?? ()
#11 0x0000000000000000 in ?? ()

您能提供的任何帮助都非常好。

提前致谢。

【问题讨论】:

  • currentPushQueue 在哪里初始化?
  • 你是用-pthread编译的吗?
  • 代码太疯狂了。大部分都是无意义的噪音。
  • @KerrekSB 你能解释一下它有什么问题吗?除了不初始化currentPushQueue?
  • @user1137058:没有错,只是没有意义。您编写的几乎每一行代码都与隐式默认行为相同,因此是多余的。

标签: c++ segmentation-fault mutex


【解决方案1】:

完全删除类的构造函数中注释掉的代码,因为你没有锁定任何东西,所以一开始就不应该存在这些代码。问题是:

1.您尚未初始化或将成员变量“currentPushQueue”分配给任何值,因此此代码:

 instructionQueues[currentPushQueue].push(newArgument);

除非分配了 currentPushQueue,否则完全错误。

2.您没有使用互斥锁,因为它们是使用提供的包装器(std::unique_lock/std::lock_guard)。

试试这个代码,请回复:

#include <mutex>
#include <queue>

class FunctionQueue
{
public:
    FunctionQueue();
    ~FunctionQueue();
    void pushInt(int);
private:
    std::mutex pushLock;
    int currentPushQueue = 0; // Set this variable somehow
    std::queue<int> instructionQueues[2];
};

FunctionQueue::FunctionQueue()
{
    instructionQueues[0] = std::queue<int>();
    instructionQueues[1] = std::queue<int>();
}

FunctionQueue::~FunctionQueue() {}

void FunctionQueue::pushInt(int newArgument)
{
    std::unique_lock<std::mutex> mutexLock(pushLock);
    instructionQueues[currentPushQueue].push(newArgument);
    // Unlocks automatically
}

int main(int argc, char* argv[])
{
    FunctionQueue testQueue;
    testQueue.pushInt(10);
}

【讨论】:

  • 仍然是 segFaults,具有几乎相同的回溯(此处发布太长)。这段代码会为你编译吗?
  • 经过大量谷歌搜索后,发现它实际上与 g++ 中的一个错误有关,导致我需要在 g++ 的 args 中包含 -Wl,--no-as-needed
猜你喜欢
  • 2021-07-19
  • 2011-03-03
  • 2011-08-22
  • 2014-05-24
  • 1970-01-01
  • 2018-05-23
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多