简体   繁体   English

使用pthreads获取总线错误10

[英]Getting bus error 10 with pthreads

My command line tool keeps throwing the bus error: 10 message. 我的命令行工具不断抛出bus error: 10消息。 Xcode debugger shows EXC_BAD_ACCESS message and highlights the function call that creates the thread. Xcode调试器显示EXC_BAD_ACCESS消息并突出显示创建线程的函数调用。 Manual debugging shows that the execution flow breaks at random positions inside the thread flow. 手动调试显示执行流程在线程流内的随机位置中断。 I tried another compiler (gcc), but it ended up the same. 我尝试了另一个编译器(gcc),但最终结果相同。 Disabling pthread_mutex_lock() and pthread_mutex_unlock() doesn't help. 禁用pthread_mutex_lock()pthread_mutex_unlock()没有帮助。 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);
}

I think, in your case, 我认为,在你的情况下,

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

at this call, thread is a pointer and not allocated memory. 在此调用中, thread是指针而没有分配内存。 So, essentially pthread_create() tries to write into uninitialized memory, which creates undefined behavior . 因此,基本上pthread_create()尝试写入未初始化的内存,从而创建未定义的行为

Referring the man page of pthread_create() 参考pthread_create()的手册页

Before returning, a successful call to pthread_create() stores the ID of the new thread in the buffer pointed to by thread ;.... 回国之前,在成功调用pthread_create()存储在缓冲区中的新线程的ID被指向thread ; ....

Instead, you can do 相反,你可以做到

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

You're using an uninitialized pointer to your pthread_t . 你正在使用一个未初始化的指向你的pthread_t指针。 The actual storage of the pthread_t needs to be somewhere! pthread_t的实际存储需要在某个地方!

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);
}

As other answers pointed out, you need to initialize your pointer thread which you can simply do with: 正如其他答案所指出的那样,您需要初始化指针thread ,您可以使用它来执行以下操作:

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

Well, then I'll have to allocate memory dynamically, because different threads are spawned inside many different functions, hence I can't use local variables, because I'm not going to join the threads. 那么,我将不得不动态分配内存,因为不同的线程在许多不同的函数中产生,因此我不能使用局部变量,因为我不会加入线程。 Then, how can I free the allocated memory without waiting for the thread to finish, ie without calling join? 那么,如何在不等待线程完成的情况下释放分配的内存,即不调用join?

No. You don't need to dynamically allocate just because you are going to spawn multiple threads. 不。您不需要动态分配只是因为您要生成多个线程。 The thread identifier is no longer needed once a thread has been created So whether it's a local variable or malloc ed is not important. 一旦创建了一个线程,就不再需要线程标识符了解它是局部变量还是malloc ed并不重要。 It's only needed when you need to join or change some characteristics of the thread -- for which you need the ID. 只有当您需要join或更改线程的某些特征时才需要它 - 您需要ID。 Otherwise, you can even reuse the same thread for creating multiple threads. 否则,您甚至可以重用相同的线程来创建多个线程。 For example, 例如,

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

is perfectly fine. 很好。 A thread can always get its own ID by calling pthread_self() if needed. 如果需要,线程总是可以通过调用pthread_self()来获取自己的ID。 But you can't pass a local variable mutex1 to thread functions as once main thread exits, the mutex1 no longer exits as thread created continues to use it. 但是一旦main线程退出,你就无法将局部变量mutex1给线程函数,因为创建的线程继续使用它, mutex1不再退出。 So you either need malloc mutex1 or make it a global variable. 所以你要么需要malloc mutex1要么让它成为一个全局变量。

Another thing to do is that if you decide to let the main thread exit then you should call pthread_exit() . 另一件事是,如果您决定让主线程退出,那么您应该调用pthread_exit() Otherwise, when the main thread exits (either by calling exit or simply return ) then the whole process will die, meaning, all the threads will die too. 否则,当线程退出时(通过调用exit或简单地return ),整个过程将会死亡,这意味着所有线程也将死亡。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM