Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

segmentation fault on pthread_mutex_lock

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?

like image 984
Nefzen Avatar asked Jun 02 '09 14:06

Nefzen


2 Answers

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.

like image 82
Nefzen Avatar answered Nov 05 '22 19:11

Nefzen


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.

like image 20
Aiden Bell Avatar answered Nov 05 '22 20:11

Aiden Bell