Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C++
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 4-Feb-13 23:48pm
Comments
Marius Bancila at 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.
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

See the solutions to this question[^]. There are several useful links and examples
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

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 Smile | :)
  Permalink  

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

  Print Answers RSS
0 Sergey Alexandrovich Kryukov 6,745
1 OriginalGriff 6,696
2 CPallini 5,315
3 George Jonsson 3,599
4 Gihan Liyanage 2,650


Advertise | Privacy | Mobile
Web02 | 2.8.140922.1 | Last Updated 5 Feb 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100