User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the C section within the Software Development category of DaniWeb, a massive community of 391,711 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 3,400 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our C advertiser:
Views: 1236 | Replies: 2
Reply
Join Date: Sep 2005
Posts: 17
Reputation: perlsu is an unknown quantity at this point 
Rep Power: 3
Solved Threads: 0
perlsu perlsu is offline Offline
Newbie Poster

thread-safe variable

  #1  
Feb 18th, 2006
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 isOpen = 0;
static open_parameters open_para;

open_para.i = 5;
open_para.userData = &isOpen;

_beginthread( thread_open, 0, (void*)&open_para);

//isOpen = 1; //error if this line doesn't contain

while ( 0 == isOpen)
{
// Wait for a while between loops.
Sleep( 5 );
}

printf(" finished main");
getchar();
}
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Aug 2005
Location: near St Louis, Missouri, USA
Posts: 10,548
Reputation: Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of 
Rep Power: 36
Solved Threads: 860
Moderator
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Most Valuable Poster

Re: thread-safe variable

  #2  
Feb 18th, 2006
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.
Reply With Quote  
Join Date: Dec 2005
Posts: 3,261
Reputation: Salem has much to be proud of Salem has much to be proud of Salem has much to be proud of Salem has much to be proud of Salem has much to be proud of Salem has much to be proud of Salem has much to be proud of Salem has much to be proud of Salem has much to be proud of 
Rep Power: 20
Solved Threads: 368
Colleague
Salem's Avatar
Salem Salem is offline Offline
void main'ers are DOOMed

Re: thread-safe variable

  #3  
Feb 18th, 2006
> 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.
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

DaniWeb C Marketplace
Thread Tools Display Modes

Similar Threads
Other Threads in the C Forum

All times are GMT -4. The time now is 3:08 am.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC