107

Possible Duplicate:
Start thread with member function

I have a small class:

class Test
{
public:
  void runMultiThread();
private:
  int calculate(int from, int to);
}  

How its possible to run method calculate with two differents set of parametrs(for example calculate(0,10), calculate(11,20)) in two threads from method runMultiThread()?

PS Thanks I have forgotten that I need pass this, as parameter.

Community
  • 1
  • 1
kobra
  • 1,285
  • 2
  • 12
  • 15

1 Answers1

250

Not so hard:

#include <thread>

void Test::runMultiThread()
{
    std::thread t1(&Test::calculate, this,  0, 10);
    std::thread t2(&Test::calculate, this, 11, 20);
    t1.join();
    t2.join();
}

If the result of the computation is still needed, use a future instead:

#include <future>

void Test::runMultiThread()
{
     auto f1 = std::async(&Test::calculate, this,  0, 10);
     auto f2 = std::async(&Test::calculate, this, 11, 20);

     auto res1 = f1.get();
     auto res2 = f2.get();
}
Kerrek SB
  • 464,522
  • 92
  • 875
  • 1,084
  • All sorts of problems with this. Why the upvotes? The return value is discarded unchecked. Presumably the calculate method iterates over something - is it thread safe? – ravenspoint Jun 12 '12 at 14:35
  • 6
    @ravenspoint: Whether it's threadsafe is up to the OP, non? I agree that the return value should be recovered, though the OP doesn't indicate that that's intended (it could be like `printf`). `std::async` would be an alternative. – Kerrek SB Jun 12 '12 at 14:37
  • That's why I asked the OP what problem he found, instead of slapping down a half-answer that might will end up giving the OP even more grief. – ravenspoint Jun 12 '12 at 14:38
  • 2
    Why spawn two new threads and then block the current thread? Usually better to spawn one new thread and do the other calculation in the current thread. – Jonathan Wakely Jun 12 '12 at 14:44
  • 4
    @JonathanWakely Maybe because the OP asked so? Though we don't know if his approach is correct in the first place, but maybe he does other computations meanwhile. – Christian Rau Jun 12 '12 at 14:47
  • No, the OP said "in two threads" not "in two new threads". The current thread could be one of the two. – Jonathan Wakely Jun 12 '12 at 14:51
  • This answer has, so far, received 6 upvotes. It is impossible to know what OP's problem was, so how can all these people imagine the answer is useful? – ravenspoint Jun 12 '12 at 15:03
  • 17
    @ravenspoint: The question asked how to run a function in different threads with different arguments, and this answer clearly demonstrates how to do that. How is that not useful? – Mike Seymour Jun 12 '12 at 15:14
  • 1
    @ravenspoint: From OP's description of the problem, it is almost clear what he wants: he wants to run the function `calculate` in two different threads passing two different sets of arguments (also, it is likely that he wants to create two *new* different threads). – Nawaz Jun 12 '12 at 15:15
  • @Nawaz the original question is not at all clear. Does he need to check or store the return value from calculate(). Does the calculate() function have any side effects? Are all objects touched by calculate() thread-safe? There are always many 'gotchas' designing multithreaded applications. To say that it is 'not so hard' is very misleading. – ravenspoint Jun 12 '12 at 15:47
  • 9
    @ravenspoint: Those are *other* questions; and there can *more other* questions as well. – Nawaz Jun 12 '12 at 16:01
  • 4
    I didn't think that the number of threads would be a point of contention. I'm merely demonstrating the techniques; I'm confident the OP can arrange the code in any fashion that suits her ultimate concurrency needs. – Kerrek SB Jun 12 '12 at 16:24
  • This was helpful to me, but why does this need to be passed aside for the fact it is required to not have an error? :) – syntheticgio Jul 31 '15 at 01:35
  • My problem with this answer is that when I am trying the snippet, the object of Test class is being destroyed when std::thread starts. This is under GCC 5.3, not sure of other implementations. – ajeh Mar 11 '16 at 16:31