I want to change isOpen variable in main function from threadfun. But , the following code doesn't change the isOpen value in main fun . So, main stay forever in while loop even though the thread fun finished. Thanks a lot in advance.
Rgds,
perlsu
#include <windows.h>
#include <process.h> /* _beginthread, _endthread */
#include <stddef.h>
#include <stdio.h>
#include <stdlib.h>
typedef struct open_parameters_tag
{
int i;
void* userData;
}open_parameters;
void threadfun( int i, void* data)
{
int flag = 1;
puts("thread");
Sleep (1000);
puts("thread");
Sleep (1000);
puts("finished thread");
data = &flag;
}
void thread_open( void* open_para )
{
open_parameters* op;
op = (open_parameters*) malloc( sizeof ( open_parameters ) );
op = (open_parameters*) open_para;
threadfun(op ->i, op->userData);
}
void main()
{
static int [B]isOpen [/B] = 0;
static open_parameters open_para;
open_para.i = 5;
open_para.userData = &[B]isOpen[/B];
_beginthread( thread_open, 0, (void*)&open_para);
//isOpen = 1; //error if this line doesn't contain
while ( 0 == [B]isOpen[/B])
{
// Wait for a while between loops.
Sleep( 5 );
}
printf(" finished main");
getchar();
}