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();
}

Edited 3 Years Ago by happygeek: fixed formatting

data = &flag;

that is incorrect. data is a pointer to a variable inside main(). If you want to change it, then you need to do something like this (typecast data to int pointer)

*(int*)data = flag;

That should fix the immediate problem, but then you need to add code that will syncronize access to the variable inside main() so that main() will not read a partially written variable when the read begins to change it. There is no guarentee that the operating system will not switch execution threads in the middle of read or write operations. You can implement system-wide variables called Semaphores.

> void main()
main returns int.

> while ( 0 == isOpen)
Cute trick.
Will you remember the == when you're comparing two variables?

The problem is, the compiler doesn't see your sneaky way of changing the variable, so it just assumes that isOpen is constant since you don't change it inside the loop. So it gets loaded ONCE when the loop enters for the first time (when it's probably still 0), and thereafter is is never read again.

To fix this, you need to indicate that the variable may change at any time, you do this with

volatile int isOpen  = 0;

> op = (open_parameters*) malloc( sizeof ( open_parameters ) );
> op = (open_parameters*) open_para;
This is a memory leak, there was no need to malloc anything at all.
Also, since you're writing in C, the cast is unnecessary, so it's simply
op = open_para;
But I guess you're compiling your C code with a C++ compiler anyway, so I'll save you the trouble of moaning about the cast being necessary and just tell you to figure out how to compile for the language you're writing in.

This article has been dead for over six months. Start a new discussion instead.