I'm blocking on a weird issue trying to deal with posix threads. I'll start with the code:
#include <pthread.h>
#include <semaphore.h>
typedef struct {
pthread_mutex_t *mutex;
} buffer_t;
buffer_t *buffer_alloc(unsigned int maxSize) {
buffer_t *buffer = (buffer_t *) malloc(sizeof(buffer_t));
if(buffer == NULL)
return NULL;
pthread_mutex_init(buffer->mutex, NULL); // This line causes a crash
pthread_mutex_t *mutex;
pthread_mutex_init(mutex, NULL); // This one doesn't
}
I'm getting a segmentation fault on the first pthread_mutex_init()
.
Here's gdb's runlog and backtrace:
[Thread debugging using libthread_db enabled]
Using host libthread_db library "/lib/x86_64-linux-gnu/libthread_db.so.1".
Program received signal SIGSEGV, Segmentation fault.
__pthread_mutex_init (mutex=0x0, mutexattr=0x0) at pthread_mutex_init.c:83
83 pthread_mutex_init.c: No such file or directory.
(gdb) backtrace
#0 __pthread_mutex_init (mutex=0x0, mutexattr=0x0) at pthread_mutex_init.c:83
#1 0x00000000004015a8 in buffer_alloc (maxSize=10) at buffers.c:26
Thanks for your help !