Hi all,
I am trying to share a character array as shared data container between a pthread and the main program. Not really that experienced in C programming so not able to find out what is wrong with what I have done. Anyone able to have a quick look?
Bacically what I do is the following:
1) define the variable at the start of my main code:
Code:
char *DATA;
int READY=0;
2) start the thread with:
Code:
pthread_create(&threads[0], NULL, run_thread, 0);
3) which calls:
Code:
void *run_thread(void *threadid)
{
runThread(&READY,DATA);
}
4) and then the thread sets the DATA as below:
Code:
void runThread(int *ready,char *data)
{
...
data=(char *)malloc(length+1);
strncpy(data,buffer,length);
*ready=1;
...
{
5) Finally I wait for the thread in the main code and get the value as below:
Code:
while(READY==0){
sleep(1);
}
printf("VALUE:%s",DATA);
Now what happens with the above is that the value for DATA is NULL everytime. I have checked that is gets set in the thread, but somehow it does not carry over to the main code. A bit like the variable in the thread is a copy of the original variable.
Probably some C principle that I don't quite understand, so if someone would take a minute and point out that would be great
Thanks for your help in advance,
Olli