pthread_mutex_lock 上的分段错误
当我尝试这样做时,我遇到了分段错误
pthread_mutex_lock(&_mutex).
这真的很奇怪,我不确定是什么导致了它。 我已经在构造函数中初始化了 _mutex ,
pthread_mutex_init(&_mutex,NULL).
我能做什么?
I'm getting a segmentation fault when I try to do
pthread_mutex_lock(&_mutex).
This is really odd, I'm not sure what might have caused it. I have initialized _mutex in the constructor with
pthread_mutex_init(&_mutex,NULL).
anything I can do?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
解决了,我对此非常恼火。
我想将 Producer* 作为参数发送给 Pthread 运行的函数,因此我使用了 &(*iter),其中 iter 是在生产者向量上运行的迭代器。
我几乎没有注意到它(正确地)是一个向量< Producer* >,这意味着我一直在发送 Producer* * ,它产生了未定义的结果。 咕噜咕噜。 显然,我没有注意到这一点,因为 Pthreads 是纯 C 语言,因此使用 void*,因为它是接受任何类型参数的唯一方法。
solved it, and I am very annoyed at this.
I wanted to send a Producer* as an argument to the function the Pthread runs, so I used &(*iter), where iter is an iterator that runs on a producers vector.
little did I notice it was (rightfully) a vector< Producer* >, which meant I've been sending Producer* * which produced undefined results. grrrrr. Obviously, I didn't notice this because Pthreads is in pure C and therefor uses void* as it's only way of accepting any type of arguments.
连接调试器并找出确切导致段错误的原因。 某些指针可能只是指向随机性或未初始化的区域。
同时运行 valgrind 的 memcheck 并查看其内容。
编辑
作为对下面评论的回应,pthread API 的使用在某些地方听起来不正确。 我推荐 O'Reilly 的《PThread 编程》作为参考。 这就是我前进的动力:)我猜到这一点是因为 API 的使用正在将 pthread_mutex_t 结构内部的指针移动到危险的地方。 如果正确使用 API,则不会发生这种情况。
Attach a debugger and find out exactly what is causing the segfault. It is possible some pointer is just pointing into randomness or uninitialized area.
Also run valgrind's memcheck and see what that says.
edit
In response to comments below, the usage of the pthread API sound incorrect somewhere. I recommended "PThread Programming" by O'Reilly as a reference. It is what got me going :) I guessed this because the usage of the API is moving a pointer internal to the pthread_mutex_t struct somewhere dangerous. This should not happen with correct usage of the API.