threading in classes instead of main

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • CodeNoob117
    New Member
    • Jan 2013
    • 19

    threading in classes instead of main

    I want to make a thread outside an int main() method; and this code below gives an error of (paraphrasing) 'no constructor found for thread for type void()'

    Code:
    #include <thread>;
    class Board(){
    
    //Lines Later
    
    void getPlayerInput(){//Code NotShown}
    
    void playGame(){
    
    std::thread t1(getPlayerInput);//thread to get userInput 
    
    }
    
    
    //Lines Later
    
    }
    Is there any way to accomplish threading outside the main and in a class?
    Thanks,
    CodeNoob117
  • weaknessforcats
    Recognized Expert Expert
    • Mar 2007
    • 9214

    #2
    Multithreading is operating system dependent.

    Usually you:
    1) write a class method to launch your thread. What you do here is call a class static method with the info needed to create the thread.
    2) write a class static method that uses its arguments to create the thread. Here you need the name of the class method that is the thread itself. After the thread is created, use a retinterpret_ca st to cycle the thread back to a class member function.
    3) use the class member function.

    You will also need to set up an event so that you can stop your thread.

    If you are using Windows I recommend the book "Windows via C/C++" by Jeffrey Richeter. He had code examples on how to set this up.

    Comment

    Working...