I am sitting on a Beaglebone Black and having problem with pthread_join, which gives me Bus error. See the following code below. This is taken directly from a Youtube-tutorial on 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;
}
This code works perfectly on my PC running Ubuntu 14.04, although Valgrind shows some "possibly lost" bytes on the pthread_join command. Hence the code is not the problem - I figure it must be something on the Debian running on Beaglebone that's causing it. And yes, I do include the -lpthread library.
The output I get on the Beaglebone is:
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
The Debian-version running on Beaglebone:
Distributor ID: Debian
Description: Debian GNU/Linux 7.8 (wheezy)
Release: 7.8
Codename: wheezy
EDIT: the following debug info is printed:
Program received signal SIGBUS, Bus error.
0xb6fbdbd0 in pthread_join () from /lib/arm-linux-gnueabih/libpthread.so.0
Thanks for all the tips in advance.
There's a mistake (typo? error in the original?) here:
ret1 = pthread_create(&thread1, NULL, myfunc, (void *) msg1);
ret2 = pthread_create(&thread1, NULL, myfunc, (void *) msg2);
You store both thread IDs in thread1
, leaving thread2
uninitialized. The second call to pthread_create()
should use &thread2
.