Debian pthread_join 总线错误

Debian pthread_join bus error

我坐在 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;

}

尽管 Valgrind 在 pthread_join 命令上显示了一些 "possibly lost" 字节,但此代码在我的 PC 运行 Ubuntu 14.04 上运行完美。因此,代码不是问题 - 我认为它一定是 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

提前感谢所有提示。

此处有错误(打字错误?原文有误?):

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

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