我在試圖處理posix線程的奇怪問題上阻塞。 我的代碼開始:mutex_init()導致奇怪的段錯誤
#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
}
我第一pthread_mutex_init()
得到一個分段錯誤。 這裏的gdb的runlog和回溯:
[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
感謝您的幫助!互斥初始化
typedef struct {
pthread_mutex_t mutex;
} buffer_t;
,然後到: