Getting bus 10 error with pthreads

My command line tool keeps throwing a message bus error: 10

. The Xcode debugger shows the message EXC_BAD_ACCESS

and highlights the function call that creates the thread. Manual debugging shows that the thread of execution is interrupted at random positions within the thread of the thread. I tried another compiler (gcc) but it turned out to be the same. Disconnecting pthread_mutex_lock()

and pthread_mutex_unlock()

doesn't help. I wrote this small example that reproduces the error.

#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>


typedef struct thread_args {
    pthread_mutex_t* mutex;
} thread_args;


void* test(void* t_args) {
    printf("Thread initiated\n");
    thread_args* args = (thread_args* )t_args;
    printf("Args casted\n");
    pthread_mutex_lock(args->mutex);
    printf("Mutex locked\n");
    pthread_mutex_unlock(args->mutex);
    printf("Mutex unlocked\n");
    pthread_exit(NULL);
}


int main() {
    pthread_mutex_t mutex1;
    pthread_mutex_init(&mutex1, NULL);

    thread_args args;
    args.mutex = &mutex1;

    pthread_t* thread;
    printf("Initiating a thread\n");
    pthread_create(thread, NULL, test, &args);
    return(0);
}

      

+3


source to share


3 answers


I think in your case

pthread_create(thread, NULL, test, &args);

      

in this call thread

is a pointer and not allocated memory. So essentially pthread_create()

trying to write to uninitialized memory, which creates undefined behavior .

Submitting a man page pthread_create()



Before returning, the successful call pthread_create()

stores the identifier of the new thread in the buffer pointed to by thread

; ....

Instead, you can do

 pthread_t thread;
 ...
 pthread_create(&thread, NULL, test, &args);

      

+2


source


You are using an uninitialized pointer to pthread_t

. The actual storage pthread_t

must be somewhere!

Try:



int main() {
   pthread_mutex_t mutex1;
   pthread_mutex_init(&mutex1, NULL);

   thread_args args;
   args.mutex = &mutex1;

   pthread_t thread;
   printf("Initiating a thread\n");
   pthread_create(&thread, NULL, test, &args);
   return(0);
}

      

+1


source


As other answers pointed out, you need to initialize your pointer thread

, which you can simply do with:

   pthread_t thread;
   pthread_create(&thread, NULL, test, &args);

      

Well then I will have to dynamically allocate memory because different threads are created inside many different functions, so I cannot use local variables because I am not going to join threads. Then how can I free the allocated memory without waiting for the thread to complete, that is, without calling the connection?

Not. You don't need to dynamically allocate just because you are going to create multiple threads. The stream ID is no longer needed after stream creation. That the local variable or malloc

ed is not important. This is only necessary when you need join

or change some characteristics of the stream for which you need an identifier. Otherwise, you can reuse the same thread to create multiple threads. For example,

   pthread_t thread;
   for( i = 0; i<8; i++)
     pthread_create(&thread, NULL, thread_func, NULL);

      

fine. A thread can always get its own identifier by calling pthread_self()

as needed. But you cannot pass a local variable mutex1

in the thread function, once the thread main

exits, mutex1

it doesn't exit anymore as the created thread continues to use it. So you either need malloc mutex1

or make it a global variable.

Another thing is that if you decide to allow the main thread to exit, you must call pthread_exit()

. Otherwise, when the main thread exits (either by calling exit

or simply return

), then the whole process will die, that is, all threads will die too.

+1


source







All Articles