_gcry_ath_mutex_lock: Assertion `*lock == ((ath_mutex_t) 0)' failed.

牛晨光發表於2012-03-19

如果你的多執行緒程式出現如題的錯誤,然後直接 Aborted (core dumped) 掉,那麼一定是由於你所使用的gnutls介面沒有進行鎖的初始化,

在程式初始化時加入以下程式碼即可:

#include <gnutls/gnutls.h>

static int mutex_init(void **mutex)
{
    *mutex = malloc(sizeof(pthread_mutex_t));
    pthread_mutex_init(*mutex, NULL);
    return 0;
}

static int mutex_lock(void **mutex)
{
    pthread_mutex_lock(*mutex);
    return 0;
}

static int mutex_unlock(void **mutex)
{
    pthread_mutex_unlock(*mutex);
    return 0;
}

static int mutex_deinit(void **mutex)
{
    pthread_mutex_destroy(*mutex);
    free(*mutex);
    return 0;
}

int main(int argc, char **argv)
{
    gnutls_global_set_mutex(mutex_init, mutex_deinit,
                            mutex_lock, mutex_unlock);
    gnutls_global_init();

    // your codes

    return 0;
}


相關文章