【问题标题】:Debian pthread_join bus errorDebian pthread_join 总线错误
【发布时间】:2016-03-14 16:34:13
【问题描述】:

我坐在 Beaglebone Black 上,遇到 pthread_join 问题,这给了我总线错误。 请参见下面的代码。这直接取自 pthreads 上的 Youtube 教程。

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

void *myfunc (void *myvar);

int main(int argc, char* argv[])
{
    pthread_t thread1, thread2;
    char *msg1 = "First thread";
    char *msg2 = "Second thread";
    int ret1, ret2;

    ret1 = pthread_create(&thread1, NULL, myfunc, (void *) msg1);
    ret2 = pthread_create(&thread1, NULL, myfunc, (void *) msg2);

    printf("Main function after pthread_create\n");

    pthread_join(thread1, NULL);
    pthread_join(thread2, NULL);

    // Bus error on the above pthread_join
    printf("Here is OK\n"); 
    printf("First thread ret1 = %d\n", ret1);
    printf("Second thread ret2 = %d\n", ret2);

   return 0;
}

void *myfunc (void *myvar)
{
   char *msg;
   msg = (char *) myvar;

   int i;
   for(i=0; i < 10; i++)
   {
       printf("%s %d\n", msg, i);
       sleep(1);
   }

 return NULL;

}

此代码在我运行 Ubuntu 14.04 的 PC 上完美运行,尽管 Valgrind 在 pthread_join 命令上显示了一些“可能丢失”的字节。因此代码不是问题 - 我认为它一定是在 Beaglebone 上运行的 Debian 上的某些东西导致了它。是的,我确实包含了 -lpthread 库。

我在 Beaglebone 上得到的输出是:

Main function after pthread_create
Second thread 0
First thread 0
Second thread 1
First thread 1
Second thread 2
First thread 2
Second thread 3
First thread 3
Second thread 4
First thread 4
Second thread 5
First thread 5
Second thread 6
First thread 6
Second thread 7
First thread 7
Second thread 8
First thread 8
Second thread 9
First thread 9
Bus error

在 Beaglebone 上运行的 Debian 版本:

Distributor ID: Debian
Description:    Debian GNU/Linux 7.8 (wheezy)
Release:        7.8
Codename:       wheezy

编辑:打印以下调试信息:

Program received signal SIGBUS, Bus error.
0xb6fbdbd0 in pthread_join () from /lib/arm-linux-gnueabih/libpthread.so.0

提前感谢所有提示。

【问题讨论】:

  • 您将&amp;thread1 传递给您的两个pthread_create()s。 thread2 未初始化。 pthread_join()ing 带有未初始化的 pthread_t 是未定义的行为。您只是幸运(或不幸?)这没有在您的 PC 上崩溃。更重要的是,如果您通过了-Wuninitialized-Wall-Wextra,您的编译器会很高兴地警告您,最后两个应该是强制性的。

标签: pthreads debian beagleboneblack pthread-join


【解决方案1】:

这里有一个错误(错字?原文有错误?):

ret1 = pthread_create(&thread1, NULL, myfunc, (void *) msg1);
ret2 = pthread_create(&thread1, NULL, myfunc, (void *) msg2);

您将两个线程 ID 都存储在 thread1 中,而 thread2 未初始化。第二次调用pthread_create() 应该使用&amp;thread2

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-11-06
    • 1970-01-01
    • 2015-05-02
    • 2016-06-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多