Sign in to follow this  
rldivide

pausing FunctionA while waiting FunctionB ?

Recommended Posts

Hello, From FunctionA, I call a function from an extern library. Then, this extern library call FunctionB with result. Problem: I need to pause FunctionA while waiting for FunctionB to be called with the result, so I can return result from FunctionA. How can I do that ? Thanks

Share this post


Link to post
Share on other sites
Have function B transmit the result to function A (for instance, by means of a variable shared between the two) then return. Function A's execution will resume eventually, once the library function is done, and it will have the result in the shared variable ready for returning.

Share this post


Link to post
Share on other sites
Quote:
Original post by ToohrVyk
Function A's execution will resume eventually, once the library function is done, and it will have the result in the shared variable ready for returning.


I'm not sure of that, because the extern function call some CUDA stuff, then when this stuff is done, CUDA call function B. So function A will more likely continue the process as return from extern function doesn't imply the result is ready yet.

Share this post


Link to post
Share on other sites
What ToohrVyk means is that you have a spinlock in FunctionA() to wait that FunctionB() sets the variable, e.g.

static bool is_ready=false;
void FunctionB()
{
is_ready=true;
}
void FunctionA()
{
is_ready=false;
/*call the external func*/
while(!is_ready)
Sleep(0);
}

Share this post


Link to post
Share on other sites
Note that you can also use a signal and wait on it, if you want to avoid spin-locking and eating 100% of a CPU on waiting. :)

Share this post


Link to post
Share on other sites
Quote:
Original post by NotAYakk
Note that you can also use a signal and wait on it, if you want to avoid spin-locking and eating 100% of a CPU on waiting. :)


Can you be more specific ?

Share this post


Link to post
Share on other sites
Many threading libraries (e.g. boost) provide a condition variable that will allow you to wait without running burning through CPU cycles.

The code would look something like:


bool done = false;
mutex mtx;
condition cond;

void funcB()
{
// ...

scoped_lock lk(mtx);
done = true;
cond.signal();
}

void funcA()
{
external_func();

scoped_lock lk(mtx);
while (!done)
cond.wait(lk); // non-busy wait

// funcB() has finished.
}


EDIT: but if external_func() is expected to finish very quickly, you might be better off with a spin lock. Experiment with both methods (assuming you're aiming for speed).

However, simply assigning a value to a bool variable is not a sufficient method for implementing a correct spin lock (in C++ at least). You will need to insert an instruction that propogates the change to all processors. Someone else will have to tell you about the specifics of doing this.

Share this post


Link to post
Share on other sites
Saying that the bool is volatile is a good place to start. In many C++ compiler environments, that will work. (volatile is intended for "strange memory". It isn't intended for inter-thread communication in C++. But much of the requirements of strange memory and inter-thread communication overlap.)

And yes -- a signal based approach makes the most sense if you expect to be waiting more than 1/30th of a second. Depending on the granularity of your waiting needs, and the degree of real time responsiveness, and where you are in the kernel/user space, different approaches are better than others to this problem.

Share this post


Link to post
Share on other sites
Quote:
Original post by NotAYakk
Saying that the bool is volatile is a good place to start. In many C++ compiler environments, that will work.


I couldn't recommend using volatile with a clear conscience it as won't work in equally many environments, probably more in fact. The semantics change depending merely on the version of the MS compiler you use, for example. It's an incredibly brittle approach.

At the very least, check your compiler docs for the guarantees given and I'd also recommend forcing compilation to fail for any compiler you haven't researched thoroughly.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this