我坐在Beaglebone Black上并遇到pthread_join问题,这给了我Bus错误。 请参阅以下代码。这是直接从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的电脑上完美运行,尽管Valgrind显示了一些可能丢失的#34; 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
提前感谢所有提示。
答案 0 :(得分:0)
这里有一个错误(错误?错误原件?):
ret1 = pthread_create(&thread1, NULL, myfunc, (void *) msg1);
ret2 = pthread_create(&thread1, NULL, myfunc, (void *) msg2);
您将两个主题ID都存储在thread1
中,而thread2
保持未初始化状态。第二次调用pthread_create()
应使用&thread2
。