-2

I am trying to call some of my member functions using threads. Suppose I have this

class myclass
{
public:
    myclass();
    double function1();
    void function2();
};

myclass::myclass()
{}


double myclass::function1()
{
    ...
    return a double;
}


void myclass::function2()
{
    //use a  thread to call function 1
    std::thread t(function1);//doesnt work!-wont compile
    std::thread t2(myclass::function1);//doesnt work either -wont compile
    std::thread t3(&myclass::function1);//doesnt work here either - wont compile
}

How can I call a member function by a thread inside another member function in C++? I am using Visual Studio 2013 Preview by the way.

UPDATE 2:

I did as i was told , some sections of code now compiles just fine and some others just don't!
this is the new sample code which generates the error :

class xGramManipulator
{
public:
    xGramManipulator();

    void ReadMonoGram();
    void ReadBiGram();

    void ReadMonoGram(double &);
    void ReadBiGram(double &);

    void CreateMonoGramAsync();
    void CreateBiGramAsync();
};

xGramManipulator::xGramManipulator()
{
}

void xGramManipulator::CreateMonoGramAsync()
{
    thread t(&xGramManipulator::ReadMonoGram, this);
}

void xGramManipulator::CreateBiGramAsync()
{
    thread t = thread(&xGramManipulator::ReadBiGram, this);
}

The above code(those two Async member functions) generates the following errors :
Error Message:

error C2661: 'std::thread::thread' : no overloaded function takes 2 arguments

Hossein
  • 24,202
  • 35
  • 119
  • 224
  • 3
    What prevented you from formatting your question? You've been an active SO member for almost two years. Also, "doesnt work!" is hardly a useful description of your technical problem. Please do better. – Lightness Races in Orbit Jul 26 '13 at 11:13
  • @LightnessRacesinOrbit: The reason is simple ,I just accidentally hit Enter and the question was submitted, before i even try to format it.Though secods after submission it was kindly formatted by "R. Martinho Fernandes ". – Hossein Jul 26 '13 at 14:22
  • Okay, that's a good reason. Thanks. – Lightness Races in Orbit Jul 26 '13 at 14:24
  • The edited code example compiles fine for me. Make sure you `#include ` and use `std::thread`, though. Also, please don't overwrite your original question and change it. You can *append* to it, but now you've made all the answers nonsensical. – Kerrek SB Jul 26 '13 at 15:01
  • Ok, its included! then why doest it not compile in Visual Studio 2013 preview? Where did you compile it? – Hossein Jul 26 '13 at 15:08
  • @KerrekSB: The question is now updated – Hossein Jul 27 '13 at 03:12

2 Answers2

4

Say std::thread(&myclass::function1, this).

If you need to disambiguate overloads, you have to cast the function pointer explicitly:

std::thread(static_cast<void (xGramManipulator::*)()>(&xGramManipulator::ReadMonoGram), this)
Kerrek SB
  • 464,522
  • 92
  • 875
  • 1,084
  • 1
    Please stop answering blatant dupes! OMG! – Lightness Races in Orbit Jul 26 '13 at 11:14
  • @LightnessRacesinOrbit: Whoops, sorry. Next time! – Kerrek SB Jul 26 '13 at 11:15
  • 2
    @LightnessRacesinOrbit: I was young and needed the rep... And I'm not always sober enough to know whether there are dupes :-S I usually only spot dupes in particular areas I feel stronly about, like `eof()`, where the answer is much longer to type out than it takes to close. – Kerrek SB Jul 26 '13 at 11:16
  • 3
    @KerrekSB Nevermind, there isn't anything wrong with answering a question anyway. It's not your fault if it turns out to be a duplicate (and an answer doesn't ever hurt anybody). – Christian Rau Jul 26 '13 at 11:19
  • Thanks @KerrekSB :) i was going to format the code but suddenly i hit enter and it got posted , thanks for formatting and also for the answer ;) – Hossein Jul 26 '13 at 11:22
  • by the way is this wrong? thread t(&xGramManipulator::ReadMonoGram, this); because i get compilation error saying : "error C2661: 'std::thread::thread' : no overloaded function takes 2 arguments" – Hossein Jul 26 '13 at 11:33
  • @Hossein: You have to bind *all* the arguments. Non-static member functions take one instance argument plus explicit arguments, but you have to provide all the other arguments as well. – Kerrek SB Jul 26 '13 at 12:29
  • @ChristianRau: Yes, an answer can hurt the community, just in the same way that giving someone money can hurt them (if it prevents them from bothering to get their own job). Responsible community members shall not encourage dupes and [help vampire](http://slash7.com/2006/12/22/vampires/) questions by answering them! – Lightness Races in Orbit Jul 26 '13 at 12:51
  • @kerrekSB:I know that , the problem is that that function takes no argument at all! – Hossein Jul 26 '13 at 13:28
  • @Hossein: Please post representative code for your actual problem... – Kerrek SB Jul 26 '13 at 14:10
  • @Hossein: Well, my comments apply exactly. You need to specify *all* the arguments, like `std::thread t(&xGramManipulator::ReadMonoGram, this, 0.5);`. – Kerrek SB Jul 27 '13 at 09:00
  • I dont want to use the parametrized overload here, I want to run the parameterless version in my thread! – Hossein Jul 27 '13 at 23:48
  • 1
    @Hossein: Right, I got it - if you have multiple overloads, you have to disambiguate the function pointer explicitly. I added that. – Kerrek SB Jul 28 '13 at 10:54
  • Thanks, But whats that crazy syntax? would you add abit more explanation to your answer so that i can understand whats going on? – Hossein Jul 28 '13 at 11:01
  • 1
    @Hossein: It's just a cast to the type of the function you want. Look up member function types for details. The type of the other overload is `void (xGramManipulator::*)(double)`. – Kerrek SB Jul 28 '13 at 11:14
0

Try using boost::bind as described here to bind the implicit "this" parameter of a member function:

How to use boost bind with a member function

This will make it a function without parameters and you can use it to start a thread.

Community
  • 1
  • 1
Tobias Langner
  • 10,634
  • 6
  • 46
  • 76