By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,402 Members | 897 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,402 IT Pros & Developers. It's quick & easy.

Blocking functions

P: n/a
Koo
How do you create your own blocking function?

Koo
Jul 22 '05 #1
Share this Question
Share on Google+
9 Replies


P: n/a
Koo wrote:
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

Jul 22 '05 #2

P: n/a
On Sat, 20 Dec 2003 09:42:57 -0500, Jeff Schwab <je******@comcast.net>
wrote in comp.lang.c++:
Koo wrote:
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


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
Jul 22 '05 #3

P: n/a
"Koo" <io********@hotmail.com> wrote in message
news:32**************************@posting.google.c om...
How do you create your own blocking function?


Here, use mine.

void block()
{
for(;;)
}

-Mike
Jul 22 '05 #4

P: n/a
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
--
http://www.EventHelix.com/EventStudio
EventStudio 2.0 - Go Beyond UML Use Case and Sequence Diagrams
Jul 22 '05 #5

P: n/a
Koo <io********@hotmail.com> wrote in message
news:32**************************@posting.google.c om...
How do you create your own blocking function?

Koo


Here's the best

void blocking()
{
char ch;
cin>>ch;
}
Jul 22 '05 #6

P: n/a
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!
Jul 22 '05 #7

P: n/a
Koo wrote:
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
Jul 22 '05 #8

P: n/a
Koo wrote:
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

Jul 22 '05 #9

P: n/a
Anders Hybertz <an****@hybertz.dk> wrote in message news:<A3*********************@news000.worldonline. dk>...
Koo wrote:
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.


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

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

Jul 22 '05 #10

This discussion thread is closed

Replies have been disabled for this discussion.