Solved

Create a block / wait function?

Posted on 2004-08-17
7
229 Views
Last Modified: 2011-04-14
I am looking for a way to create a function that, like the sockets accept function,
blocks / waits untill a specific variable is set or a specific function is called.
At the moment I am using the very system expensive code:
while(!input);

I need a function that can be called inside a thread procedure to keep the thread from consuming all of the processors power.

Any ideas?



0
Comment
Question by:chrille_p
  • 4
  • 2
7 Comments
 
LVL 39

Expert Comment

by:itsmeandnobodyelse
ID: 11819148
On Windows platform use

    Sleep(1);    // wait 1 millisecond

in your loop.

Regards, Alex
0
 
LVL 39

Expert Comment

by:itsmeandnobodyelse
ID: 11819159
OS/2 you have to use DosSleep, and *NIX it is sleep (as far as i remember).

Regards, Alex
0
 
LVL 39

Accepted Solution

by:
itsmeandnobodyelse earned 250 total points
ID: 11819214
On Windows platform you also may wait on an event rather than on some time elapsed. You may either use WaitForSingleObject or WaitForMultipleObjects:

MSDN says:

The WaitForSingleObject function can wait for the following objects:

Change notification
Console input
Event
Job
Mutex
Process
Semaphore
Thread
Waitable timer

Regards, Alex

0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

Author Comment

by:chrille_p
ID: 11819311
Im not looking for a function that waits for a couple of seconds.
What I want is a thread-procedure (A) that waits untill function (B) is called...

I could use sleep() in my loop and check a flag every n second, but that would make
the reaction of my function slow. I want it to stop waiting and continue its work
directly when (B) is called.

/Chrille
0
 

Author Comment

by:chrille_p
ID: 11819344
WaitForSingleObject is perfect.

Thanks!

/Chrille
0
 
LVL 22

Expert Comment

by:grg99
ID: 11819346
Depending on the OS, there may be a Sleep() function that is calibrated in milliseconds.

Otherwise you could find some API, any API, that will do a task-switch for you.
On Windows that puts your thread to sleep for about 20-30 milliseconds, a nice ballpark sleep time.

0
 
LVL 39

Expert Comment

by:itsmeandnobodyelse
ID: 11819381
>> Im not looking for a function that waits for a couple of seconds.

Ok, but before using
 
    while(!input);

you should have

    while(!input) { sleep(1); }


>> What I want is a thread-procedure (A) that waits untill function (B) is called...

You could use a mutex that gets locked by your main thread, then your thread is waiting on the mutex to be unlocked, what is done at the beginning of function B .

What OS are you working on?

Regards, Alex




 

0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction This article is the first in a series of articles about the C/C++ Visual Studio Express debugger.  It provides a quick start guide in using the debugger. Part 2 focuses on additional topics in breakpoints.  Lastly, Part 3 focuses on th…
Introduction This article is a continuation of the C/C++ Visual Studio Express debugger series. Part 1 provided a quick start guide in using the debugger. Part 2 focused on additional topics in breakpoints. As your assignments become a little more …
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…

861 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now