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;
    }
}
yasso
  • 303
  • 1
  • 5
  • 11
  • 1
    For the sake of readability, you can simplify your function to: `int my_fct() { return 1; }` – Daniel Daranas Nov 11 '13 at 11:38
  • You should read something about pthreads. Threads can't be explained in a post. Read: https://computing.llnl.gov/tutorials/pthreads/ – anbu selvan Nov 11 '13 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"? – Harshil Sharma Nov 11 '13 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. – dgel Nov 11 '13 at 11:44
  • @anbuselvan pthreads is for unix but my system Windows – yasso Nov 11 '13 at 11:51
  • @HarshilSharma exactly my function checks a field in the database and return 1 if my condition is verified else return 0 and I call this function in the main – yasso Nov 11 '13 at 11:54
  • then you can use std::async and std::future as said down and its method "wait_for" to check until your result from database is 1 – anbu selvan Nov 11 '13 at 11:59
  • but your function performs a single action..i dont understand the use of while loop – kunal Nov 11 '13 at 12:22
  • @kunal he painted a picture of his dream code. Think async `yield` – sehe Nov 11 '13 at 12:22

2 Answers2

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.

masoud
  • 55,379
  • 16
  • 141
  • 208
  • Note that `async` might easily get deprecated in C++14: http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2013/n3777.pdf – avakar Nov 11 '13 at 11:46
  • Interesting, does it have another alternative in C++14? – masoud Nov 11 '13 at 11:46
  • None that I know of. The issue is with `~future` where it's unclear whether it should block or detach. See http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2013/n3773.pdf. – avakar Nov 11 '13 at 11:51
  • 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. – Puppy Nov 11 '13 at 12:29
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.

Igor Popov
  • 2,588
  • 17
  • 20
  • Notice that OpenMP requires a special compiler flag to be activated. Most compilers will still compile OpenMP code without this flag, but all OpenMP `#pragma`s will be ignored. This is an easy mistake to make, so be sure your setup is correct. – ComicSansMS Nov 11 '13 at 12:06
  • @ComicSansMS Indeed one has to use appropriate compiler flags. I updated the answer with this notice. – Igor Popov Nov 11 '13 at 12:12