For school I am working on a project that has 2 reading threads running and 1 writing thread that work around a shared buffer. This shared buffer is some sort of pointer based list that we programmed ourselves. To make it thread-safe I used to pthread_rw_locks and also some pthread_barriers. When i tried to run my code it crashed almost instantly and it gave me a segmentation fault. When using the gdb debugger it gave me the following message:
program received signal SIGSEGV, Segmentation fault.
__pthread_barrier_init (barrier=0x0, attr=0x0, count=2) at pthread_barrier_init.c:47
47 pthread_barrier_init.c: No such file or directory.
When compiling I included the -lpthread flag and I also made sure to include the pthread.h in every file where it was used. Any idea why my program can't find this c file?
EDIT
This is a snippet of the code that I use. (This is barely all code but it goes wrong in this part)
This is my code for the main loop
int main(int argc, char*argv[])
{
sbuffer_t* buffer;
sbuffer_init(&buffer);
return 0;
}
This is my code for the buffer
/**
* basic node for the buffer, these nodes are linked together to create the buffer
*/
typedef struct sbuffer_node {
struct sbuffer_node *next; /**< a pointer to the next node*/
sensor_data_t data; /**< a structure containing the data */
} sbuffer_node_t;
/**
* a structure to keep track of the buffer
*/
struct sbuffer {
sbuffer_node_t *head; /**< a pointer to the first node in the buffer */
sbuffer_node_t *tail; /**< a pointer to the last node in the buffer */
pthread_rwlock_t* lock;
pthread_barrier_t* barrierRead; //Barrier to indicate that both reader threads have succesfully read the sensor reading
pthread_barrier_t* barrierWrite; //Barrier to indicate that a sensor reading has been removed
pthread_mutex_t* FIFOlock;
int finished;
};
int sbuffer_init(sbuffer_t **buffer) {
(*buffer) = malloc(sizeof(sbuffer_t));
(*buffer)->lock=malloc(sizeof(pthread_rwlock_t));
if (*buffer == NULL) return SBUFFER_FAILURE;
pthread_rwlock_init((*buffer)->lock,NULL);
pthread_rwlock_wrlock((*buffer)->lock);
pthread_barrier_init((*buffer)->barrierRead, NULL, READER_THREADS);
pthread_barrier_init((*buffer)->barrierWrite, NULL, READER_THREADS);
pthread_mutex_init((*buffer)->FIFOlock, NULL);
(*buffer)->head = NULL;
(*buffer)->tail = NULL;
(*buffer)->finished = CONNMGR_NOT_FINISHED;
pthread_rwlock_unlock((*buffer)->lock);
return SBUFFER_SUCCESS;
}
The error is not an error, just a warning, and it’s not issued by your program, but by the debugger. The debugger tried to help you by displaying the source file where the crash happened. Alas, that source file is not a part of your program, but a part of pthreads library. Since it’s not available, the debugger informs you of this fact, since otherwise you’d expect to see the source line where the problem manifested. gdb has a “show source line” function that gets invoked after a signal/exception is raised, and that function will always print something: either the source line, or an warning message.