Sleep

T

Tomas Wilhelmsson

Ok, got a problem here, is it posible to make a function sleep until a
variable == a value without hoging 100% CPU? ...

while(wait) { }

would hog as much as it could get its hands on but i cant realy allow that
and it has to react as soon as the wait flag goes false.
 
J

John Carson

Tomas Wilhelmsson said:
Ok, got a problem here, is it posible to make a function sleep until a
variable == a value without hoging 100% CPU? ...

while(wait) { }

would hog as much as it could get its hands on but i cant realy allow
that and it has to react as soon as the wait flag goes false.


You don't say how the value of the variable is being set. If the variable is
being set by another thread, then there are certainly ways to wait without
hogging CPU time, but they are not part of standard C++ and will typically
be platform specific. If that is the problem you are dealing with, I suggest
you post this question on a newsgroup for your platform of choice.
 
T

Tomas Wilhelmsson

You don't say how the value of the variable is being set. If the variable is
being set by another thread, then there are certainly ways to wait without
hogging CPU time, but they are not part of standard C++ and will typically
be platform specific. If that is the problem you are dealing with, I suggest
you post this question on a newsgroup for your platform of choice.

It's set by another thread yes.. so .. if you could mention one way or a
good direction to look i would be very happy :> .. For the record, im on a
linux system.
 
P

Peter Koch Larsen

Tomas Wilhelmsson said:
It's set by another thread yes.. so .. if you could mention one way or a
good direction to look i would be very happy :> .. For the record, im on a
linux system.

Look in comp.programming.threads.

Kind regards
Peter Koch Larsen
 
U

Unforgiven

Tomas said:
It's set by another thread yes.. so .. if you could mention one way
or a good direction to look i would be very happy :> .. For the
record, im on a linux system.

I would suggest using the POSIX thread library. It has a little something
called condition variables, which does what you want. Check 'man
pthread_cond_init' or for more info check 'info libc' and browse to 'POSIX
Threads'
 

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

Members online

No members online now.

Forum statistics

Threads
474,147
Messages
2,570,835
Members
47,382
Latest member
MichaleStr

Latest Threads

Top