<div dir="ltr">Hi folks,<br><br>I am encountering segfaults when calling librdf_new_uri from multiple threads in C. My program code:<br><br><div style="margin-left:40px">#include <stdio.h><br>#include <stdlib.h><br>#include <string.h><br>#include <stdarg.h><br>#include <pthread.h><br>#include <librdf.h><br>#include <raptor2.h><br>/* pthread_mutex_t mutex; */<br><br>struct thread_struct {<br>    librdf_world *world;<br>    char *uri;<br>};<br><br>void *create_uri_instances(void *args) {<br>    char *original_uri;<br>    char *uri_copy;<br>    librdf_uri *uri;<br>    struct thread_struct *arg_struct = (struct thread_struct *) args;<br>    const int iterations = 1000;<br><br>    librdf_world_open(arg_struct->world);<br><br>    int i;<br>    for(i = 0; i < iterations; i++) {<br>        uri_copy = (char *) malloc(strlen(arg_struct->uri) + 1);<br>        strcpy(uri_copy, arg_struct->uri);<br><br>        /* pthread_mutex_lock(&mutex); */<br>        uri = librdf_new_uri(arg_struct->world, uri_copy);<br>        librdf_free_uri(uri);<br>        fprintf(stdout, "new/free of librdf_uri: %s\n", uri_copy);<br>        free(uri_copy);<br>        /* pthread_mutex_unlock(&mutex); */<br>    }<br>}<br><br>int main(int argc, char *argv[]) {<br>    librdf_world* world;<br>    pthread_t create_uri_instances_thread_1;<br>    pthread_t create_uri_instances_thread_2;<br><br>    // open world<br>    world = librdf_new_world();<br>    librdf_world_open(world);<br><br>    /* pthread_mutex_init(&mutex, NULL); */<br><br>    /* start thread 1 */<br>    struct thread_struct args1;<br>    args1.world = world;<br>    args1.uri = "<a href="http://host.org/1">http://host.org/1</a>";<br>    pthread_create(&create_uri_instances_thread_1, NULL, create_uri_instances, (void *) &args1);<br><br>    /* start thread 2 */<br>    struct thread_struct args2;<br>    args2.world = world;<br>    args2.uri = "<a href="http://host.org/2">http://host.org/2</a>";<br>    pthread_create(&create_uri_instances_thread_2, NULL, create_uri_instances, (void *) &args2);<br><br>    /* join on threads */<br>    pthread_join(create_uri_instances_thread_1, NULL);<br>    pthread_join(create_uri_instances_thread_2, NULL);<br><br>    librdf_free_world(world);<br>    pthread_mutex_destroy(&mutex);<br>    return 0;<br>}<br><br></div>Without the pthread mutex I encounter different segfaults originating from raptor_avltree.c. The gdb backtraces are attached.  When I synchronize around librdf_new_uri / librdf_free_uri with the pthread mutext I do not encounter segfaults.<br><br>Is this expected behaviour? Does anyone have advice or examples on using threads with librdf?<br><br>Thanks for your time,<br><br>Anthony Bargnesi<br><br></div>