473,385 Members | 1,531 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

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()'

Expand|Select|Wrap|Line Numbers
  1. #include <thread>;
  2. class Board(){
  3.  
  4. //Lines Later
  5.  
  6. void getPlayerInput(){//Code NotShown}
  7.  
  8. void playGame(){
  9.  
  10. std::thread t1(getPlayerInput);//thread to get userInput 
  11.  
  12. }
  13.  
  14.  
  15. //Lines Later
  16.  
  17. }
  18.  
Is there any way to accomplish threading outside the main and in a class?
Thanks,
CodeNoob117
Nov 1 '13 #1

✓ answered by weaknessforcats

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_cast 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.

1 1335
weaknessforcats
9,208 Expert Mod 8TB
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_cast 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.
Nov 5 '13 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

45
by: Steven T. Hatton | last post by:
This is a purely *hypothetical* question. That means, it's /pretend/, CP. ;-) If you were forced at gunpoint to put all your code in classes, rather than in namespace scope (obviously classes...
8
by: Lothar Behrens | last post by:
Hi, I am thinking about using classes to encapsulate threads for my application. My requirements are the following: The thread implementation sould not know what has to be implemented in...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.