Hi,
does anyone have an idea why this code
prints
on FreeBSD and Solaris
and
on linux
so it works as expected (at least as I would expect) on FreeBSD and Solaris but not on linux
is it possible that the "k" variable is destroyed before pthread_join() finishes?
Yes, I realize I'm asking about linux in the FreeBSD forum
I'm just trying to understand what is the problem? Is it incorrect to try to pass a reference to a stack variable from one thread to another using pthread_exit/pthread_join ?
10x
does anyone have an idea why this code
Code:
#include <stdio.h>
#include <pthread.h>
void *do_some_work(void *arg)
{
int i, j;
for(i = 1; i < 100; i++)
{
for(j = 1; j < 100; j++)
{
i / j;
}
}
printf("1\n");
int k = 5;
pthread_exit((void *) &k);
}
int main()
{
pthread_t joinable_thread;
pthread_attr_t attr;
pthread_attr_init(&attr);
pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE);
int status = pthread_create(&joinable_thread, &attr, do_some_work, NULL);
if(status)
{
printf("pthread_create() error\n");
return -1;
}
int *l;
status = pthread_join(joinable_thread, (void *) &l);
if(status)
{
printf("pthread_join() error\n");
return -1;
}
printf("2\n");
printf("%d\n", *l);
pthread_exit(NULL);
}
Code:
1
2
5
and
Code:
1
2
0
so it works as expected (at least as I would expect) on FreeBSD and Solaris but not on linux
is it possible that the "k" variable is destroyed before pthread_join() finishes?
Yes, I realize I'm asking about linux in the FreeBSD forum
I'm just trying to understand what is the problem? Is it incorrect to try to pass a reference to a stack variable from one thread to another using pthread_exit/pthread_join ?
10x