Blocking functions

J

Jeff Schwab

Koo said:
How do you create your own blocking function?

That's a system call, not part of the language.

The function often looks like this:

#include <unistd.h>

unsigned int sleep(unsigned int seconds);

-Jeff
 
J

Jack Klein

That's a system call, not part of the language.

The function often looks like this:

#include <unistd.h>

unsigned int sleep(unsigned int seconds);

-Jeff

Nonsense, you can write a blocking function in perfectly standard C++.

void blocking_function(void)
{
while(1);
}

....blocks extremely well.

Unblocking is another issue.

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++ ftp://snurse-l.org/pub/acllc-c++/faq
 
E

EventHelix.com

Any function that invokes a blocking OS primitive is a blocking
function.

If you wish to write a function that will not return until
a specific condition is met, use blocking semaphore calls to
implement such a function.

Sandeep
 
K

Koo

Well, all of you examples work but i need something that doesn't use
99% of my cup time. I need something that stop the program and wait
for something to happen. And it needs to be fast!
 
A

Anders Hybertz

Koo said:
Well, all of you examples work but i need something that doesn't use
99% of my cup time. I need something that stop the program and wait
for something to happen. And it needs to be fast!

Have a look at www.boost.org and the condition class in the threading
library. It's a non-busy-wait blocking pattern that works very well.

If you are on Windows there are things like WaitForSingleObject that you
could use directly, but that is not platform portable - so I recommend
boost http://www.boost.org/libs/thread/doc/condition.html.

o<|:) /Anders
 
J

Jeff Schwab

Koo said:
Well, all of you examples work but i need something that doesn't use
99% of my cup time. I need something that stop the program and wait
for something to happen. And it needs to be fast!


Deming's suggestion doesn't use 99% of your CPU time:

void blocking()
{
char ch;
cin>>ch;
}

If you happen to be blocking to wait for input, this is probably the way
to go. If you're blocking for something else, you probably need a
system call. In that case, check in a newsgroup specific to your
platform, and they probably will be able to help you right away.

-Jeff
 
A

Andy

Anders Hybertz said:
Have a look at www.boost.org and the condition class in the threading
library. It's a non-busy-wait blocking pattern that works very well.

That was a useful tip. Not as clever as while(1), for(;;) and cin>>ch; though.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Members online

Forum statistics

Threads
474,156
Messages
2,570,878
Members
47,408
Latest member
AlenaRay88

Latest Threads

Top