Create a block / wait function?

Posted on 2004-08-17
Medium Priority
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:

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?

Question by:chrille_p
  • 4
  • 2
LVL 39

Expert Comment

ID: 11819148
On Windows platform use

    Sleep(1);    // wait 1 millisecond

in your loop.

Regards, Alex
LVL 39

Expert Comment

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

Regards, Alex
LVL 39

Accepted Solution

itsmeandnobodyelse earned 1000 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
Waitable timer

Regards, Alex

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!


Author Comment

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.


Author Comment

ID: 11819344
WaitForSingleObject is perfect.


LVL 22

Expert Comment

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.

LVL 39

Expert Comment

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

Ok, but before using

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



Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

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

What is C++ STL?: STL stands for Standard Template Library and is a part of standard C++ libraries. It contains many useful data structures (containers) and algorithms, which can spare you a lot of the time. Today we will look at the STL Vector. …
C++ Properties One feature missing from standard C++ that you will find in many other Object Oriented Programming languages is something called a Property (http://www.experts-exchange.com/Programming/Languages/CPP/A_3912-Object-Properties-in-C.ht…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.
Suggested Courses

840 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