0

I created a function that run in while(1) and return an integer, I want to turn this function in background and recover their return.

who can help me please!

here is my function:

int my_fct() { while(1) { int result = 1; return result; } } 
9
  • 1
    For the sake of readability, you can simplify your function to: int my_fct() { return 1; } Commented Nov 11, 2013 at 11:38
  • You should read something about pthreads. Threads can't be explained in a post. Read: computing.llnl.gov/tutorials/pthreads Commented Nov 11, 2013 at 11:39
  • Can you include the function and the code where the function is being called from? And what exactly do you mean bu "turn this function in background"? Commented Nov 11, 2013 at 11:41
  • If you can use c++11, I'd recommend using the built-in threads. Otherwise, pthreads or boost::thread. Just create a thread to do what you need done in the background, and when you need the result, call join() on the thread before trying to use it. Commented Nov 11, 2013 at 11:44
  • @anbuselvan pthreads is for unix but my system Windows Commented Nov 11, 2013 at 11:51

2 Answers 2

3

How about std::async to compute it in a different thread:

int main() { auto r = std::async(std::launch::async, my_fct); int result = r.get(); } 

Requires C++11 enabled.

Sign up to request clarification or add additional context in comments.

4 Comments

Note that async might easily get deprecated in C++14: open-std.org/jtc1/sc22/wg21/docs/papers/2013/n3777.pdf
Interesting, does it have another alternative in C++14?
None that I know of. The issue is with ~future where it's unclear whether it should block or detach. See open-std.org/jtc1/sc22/wg21/docs/papers/2013/n3773.pdf.
It won't be deprecated in C++14. It's too late for such a change. N3777 wasn't even a formal motion in Chicago.
0

If you don't have access to C++11 and as you don't have access to pthreads as you are on Windows then you can use OpenMP. Most C++ compilers on both Unix-like systems and Windows support OpenMP. It is easier to use than pthreads. For example your problem can be coded like:

#include <omp.h> int my_fct() { while(1) { int result = 1; return result; } } int main() { #pragma omp sections { #pragma omp section { my_fct(); } #pragma omp section { //some other code in parallel to my_fct } } } 

This is one option, take a look at OpenMP tutorials and you may find some other solutions as well.

As suggested in a comment you need to include appropriate compiler flag in order to compile with OpenMP support. It is /openmp for MS Visual C++ compiler and -fopenmp for GNU C++ compiler. You can find the correct flags for other compilers in their usage manuals.

2 Comments

Notice that OpenMP requires a special compiler flag to be activated. Most compilers will still compile OpenMP code without this flag, but all OpenMP #pragmas will be ignored. This is an easy mistake to make, so be sure your setup is correct.
@ComicSansMS Indeed one has to use appropriate compiler flags. I updated the answer with this notice.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.