•
•
•
•
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
![]() |
•
•
Join Date: Sep 2005
Posts: 17
Reputation:
Rep Power: 3
Solved Threads: 0
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
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();
}
•
•
Join Date: Aug 2005
Location: near St Louis, Missouri, USA
Posts: 10,548
Reputation:
Rep Power: 36
Solved Threads: 860
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.
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
> 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.
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.
![]() |
•
•
•
•
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
•
•
•
•
•
•
•
•
DaniWeb C Marketplace
Similar Threads
- To thread or not to thread? (C++)
- Callback inside wrapper class dilemma (C++)
- Aurora, Nail, and others (Viruses, Spyware and other Nasties)
- Not enough space on hard drive to burn CD? (Windows NT / 2000 / XP / 2003)
- stripping digits (C)
- recursive findaverage function for Binary search tree (C)
- strtok_r() error on solaris 8 (C)
Other Threads in the C Forum
- Previous Thread: Another counting problem
- Next Thread: how to find execution time milliseconds



Linear Mode