-3

Completely new to threading and concurrency but I'm trying to start a function as a new thread and I don't understand my errors. I receive an error along the lines of Candidate expects X arguments, 2 provided. This error repeats for 0 < X <= 9 (except 2). However, in every example I've seen it's as simple as just putting your function and it's arguments. My code looks like:

Trainer.cpp:

int time = 5; // for example

void Member::decrement(int seconds){
    while(seconds > 0){
        seconds--;
        Sleep(1000);
    }
    isBusy = false;
}

void Member::startDecrement(string state){
    if (state == "busy"){ // isBusy is a private boolean, hence this
        isBusy = true;
        thread myThread = thread(decrement, time); // Thread for method
        myThread.join(); 
    else {
        isBusy = false;
    }
}

Yet this doesn't work? Can somebody give me guidance on this please, what I'm trying to do is quite simple but I have found no way that works for me as of yet. Alternatives to thread are appreciated too, I've seen that std::async is an option but that doesn't seem to work with my compiler setup.

Setup info: -sdt=c++11, MinGW, Win64, GCC 4.7.2

EDIT:

Seeing as I've been nailed for errors, here is the entire error log.

I've also tried the code provided in the answer, no luck.

whitfin
  • 4,539
  • 6
  • 39
  • 67
  • 1
    You are likely to get better answers if you include a minimal but complete example and include the actual error message and not some approximation thereof. – NPE Mar 02 '13 at 16:53
  • Isn't it possible that your argument named `time` actually refers to the standard `time()` function? What if you rename your `time` variable? – Andy Prowl Mar 02 '13 at 16:56
  • @NPE That is a minimal but complete example, as that's what's causing the error. – whitfin Mar 02 '13 at 16:57
  • @Zackehh9lives: Minimal maybe, but far from complete. – NPE Mar 02 '13 at 16:57
  • @AndyProwl `time` is there for an example, the actual variable is a longer name of `transactionTime`. – whitfin Mar 02 '13 at 16:58
  • @NPE Complete in the sense that you have enough to find out what the error is, yes. But fine, I shall amend it. – whitfin Mar 02 '13 at 16:59
  • 2
    @Zackehh9lives: See your minimal example working [here](http://liveworkspace.org/code/3SpAkW$88) – Andy Prowl Mar 02 '13 at 17:02
  • @AndyProwl My code has been filled, although I see no reason why the other operations involved should affect anything. – whitfin Mar 02 '13 at 17:04
  • Bases on your error message, the part you should focus on is this: `cannot convert '((Trainer*)this)->Trainer::decrementTime' (type '') to type 'const attributes& {aka const boost::thread_attributes&}'` – JaredC Mar 02 '13 at 17:26
  • @JaredC That makes sense, except I see no issues in the methods above when comparing them to examples which would cause that error to occur? – whitfin Mar 02 '13 at 17:28
  • 1
    @Zackehh9lives Are the examples you're looking at for member functions as `decrementTime` is? The fact that you're having a problem indicates that you don't understand something (this is perfectly normal!!), but this is why we need a *complete* example, because sometimes we just aren't aware of what we are leaving out. – JaredC Mar 02 '13 at 17:30
  • @JaredC I believed so, although perhaps not. Do you have such an example? – whitfin Mar 02 '13 at 17:31

2 Answers2

2

Since your compile errors seem to indicate that decrementTime is a member function, you need to provide an object to invoke it on (e.g. this pointer):

thread myThread = thread(&Trainer::decrementTime, this, transactionTime);
JaredC
  • 5,150
  • 1
  • 20
  • 45
  • 1
    @Zackehh9lives No problem, I hope it makes sense why we needed a complete example (i.e. with `decrementTime` as a member function). Please read this: http://sscce.org/ – JaredC Mar 02 '13 at 17:37
  • It does now, yes. I had assumed there was no difference between the two (stupid, I know). – whitfin Mar 02 '13 at 17:38
0

This is a minimal and complete example:

#include <iostream>
#include <thread>

void decrement(int seconds) {
  std::cout << "seconds: " << seconds << std::endl;
}
int main(int arg, char * argv[]) {
  int time = 100;
  std::thread myThread(decrement, time);
  myThread.join();
  return 0;
}

Compile it like this: g++ thread.cpp -o thread -std=c++11 -pthread

Next time, give such an example and also the complete error message and make sure not to have typos in the technical details such as compiler flags.

DennisL
  • 466
  • 3
  • 6
  • This is what I have been doing (see updated question). Also note I'm using `boost::thread` not `std::thread`. – whitfin Mar 02 '13 at 17:08
  • (+1) for showing there's nothing wrong with the code, and for demonstrating how the question should have been asked in the first place. – NPE Mar 02 '13 at 17:11
  • @Zackehh9lives: Apologies if this sounds harsh, but you still haven't given us enough information to even reproduce the problem, let alone solve it. – NPE Mar 02 '13 at 17:13
  • @NPE I don't understand what you want? That's the exact code I'm running, the rest of the code is entirely separate from the issue. – whitfin Mar 02 '13 at 17:14
  • To switch to boost, you simply change two lines `#include ` and `boost::thread myThread(decrement, time);` – DennisL Mar 02 '13 at 17:15
  • @Zackehh9lives: Exact code? With a variable named `bool`?! Seriously, please stop wasting everyone's time by making implausible claims. – NPE Mar 02 '13 at 17:15
  • @NPE Well, aside from variable names :p And if changing to boost just requires changing the two lines, why doesn't mine work as it is now? – whitfin Mar 02 '13 at 17:16
  • Show a little more effort: Have you tried the sample code from above and what's the exact error message you are getting? – DennisL Mar 02 '13 at 17:18