Click here to Skip to main content
15,881,687 members
Please Sign up or sign in to vote.
3.00/5 (1 vote)
See more:
Hi ,
I m working on an application which gets stuck while making some function calls.So I thought of using an asyncronous functions and for that i thought I will call those functions in a separate thread with some timeout.So if the functions get hung, the thread will get terminated after the time out happens.But I dont know whether this is a good idea to spawn a separate thread for this.

So please suggest me how this can be done or how to implement an asyncronous function in general.


Thanks,
Dev.
Posted
Comments
Marius Bancila 5-Feb-13 6:06am    
You're too vague. You need to tell us more about the problem. What kind of application, calls, what platform, are you using C++11, etc.

See the solutions to this question[^]. There are several useful links and examples
 
Share this answer
 
Is it a good idea? Yes, these days multiple threading is considered normal and expected and the overhead of starting and cleaning up a thread is no longer considered overly expensive in general outside of some embedded applications.
The question then becomes how do you do it and that very much depends on what framework, libraries and compiler you're using.
Current theory says you should use C++11 threads from a C++11 standard library with a C++11 conformant compiler and you're done. However there are no C++11 conformant compilers, C++11 threads are still more of a specification than a reality in many parts and good luck finding a standard library that both implements them correctly and documents how to use them.
Failing that then you're left with Win32 Threading API, or Qt Thread classes or Intel threading primitives or POSIX threads or one of many other threading helpers wrappers and abstraction layers which assuming you're on Windows all map down in the end to Win32 API functions like CreateThread, SuspendThread, GetThreadId.
Then of course you could bend your specification of C++ and look at .NET or WinRT threading APIs about which I know little.
Once you've picked your threading technology stick to it, don't try to mix them unless you want a serious headache, and look for examples/tutorials related to that threading tech here on CP or elsewhere. Beware if you're just starting with threads they are the hardest part of programming, even many example programs will have subtle flaws. It's worth the effort though to be able to do two things at once :-)
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900