Я сижу на Beaglebone Black и у меня проблема с pthread_join, которая выдает ошибку Bus. См. следующий код ниже. Это взято непосредственно из учебника Youtube по pthreads.
#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 показывает некоторые «возможно потерянные» байты в команде pthread_join. Следовательно, проблема не в коде - я полагаю, что это должно быть что-то в Debian, работающем на Beaglebone, что вызывает это. И да, я включаю библиотеку -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
Debian-версия, работающая на Beaglebone:
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
Спасибо за все советы заранее.
&thread1
обоим своимpthread_create()
.thread2
остается неинициализированным.pthread_join()
ing с неинициализированнымpthread_t
— поведение undefined. Вам просто повезло (или не повезло?), что это не зависло на вашем ПК. Более того, ваш компилятор с радостью предупредил бы вас об этом, если бы вы передали-Wuninitialized
,-Wall
или-Wextra
, последние два из которых должны быть обязательными. - person EOF   schedule 14.03.2016