LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 07-07-2016, 04:56 AM   #1
knobby67
Member
 
Registered: Mar 2006
Posts: 627

Rep: Reputation: 43
declaring a thread within a class


Hi All
trying to learn how to use c++ std::thread. I can get a basic one working. However for my code I'd like to declare the thread as part of a class. I've tried a few ways but get errors of various sorts. basically what I'd like to do is something like
Code:
class MyClass
{
a_function( std::string name, unsigned int x );

start_thread( std::string name, unsigned int x);

private:

std::thread mythread( function, std::string, unsigned int x ); <<<<ISUUE HERE I think..
 
}
then

Code:
MyClass::a_function( std::string name, unsigned int x )
{
stuff...
}

MyClass::start_thread( std::string name, unsigned int x )
{
  mythread( a_function, name, x ); /*start the thread*/
}
But I can't work out how to declare my thread in my class variables list, how to I show the function the thread starts?
Sorry if I've used wrong language to describe, but hope you can see what I mean from the pseudo code example
 
Old 07-07-2016, 06:23 AM   #2
knobby67
Member
 
Registered: Mar 2006
Posts: 627

Original Poster
Rep: Reputation: 43
Hi
I think the issue is with declaring the function as part of the class, which I need to do. If I test with it as not part of the class
Code:
void dynamic_run_thread( unsigned int lucky  )
{
	std::cout<<"C++11 MULTITHREADING new "<<lucky<<std::endl;


}
and call from
Code:
bool ClassA::run_dynamic( void )
{


std::thread run_thread( dynamic_run_thread, 100 );
run_thread.join();
it works. But if I make it part of the class


Code:
void ClassA::dynamic_run_thread( unsigned int lucky  )
{
	std::cout<<"C++11 MULTITHREADING new "<<lucky<<std::endl;


}
and call from
Code:
bool ClassA::run_dynamic( void )
{


std::thread run_thread( dynamic_run_thread, 100 );
run_thread.join();


I get "invalid use of non-static member function"

I've tried
Code:
std::thread run_thread( ClassA::dynamic_run_thread, 100 );
with no luck, can anyone tell me how I can call a function that's part of a class?
Thanks
 
Old 07-07-2016, 06:34 AM   #3
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,849

Rep: Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309
yes, definitely, the non-static member functions cannot be used that way. Just imagine, the existence of these functions depends on variables (instances of type ClassA).
From the other hand you can try to use static member function.
Also you may need to take into account, the non-static member functions have an additional, hidden parameter, the this pointer.
 
Old 07-07-2016, 07:40 AM   #4
sundialsvcs
LQ Guru
 
Registered: Feb 2004
Location: SE Tennessee, USA
Distribution: Gentoo, LFS
Posts: 10,659
Blog Entries: 4

Rep: Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941
As far as I know, the thread-function itself cannot be a class function, probably because of the issue of dealing with this. But you can simply write a short, private function, arrange it to receive a pointer to the desired object, and have that function invoke a (necessarily, public) method of the class-instance.

From a design standpoint, I suggest that, if the class uses a thread, the class-instance should own the thread and should completely control it. It should contain all the "voodoo" needed to launch the thread and to eventually tear it down, and to synchronize everything as required. Clients ought not to even know, so to speak, that a thread is involved.
 
Old 07-07-2016, 08:17 AM   #5
knobby67
Member
 
Registered: Mar 2006
Posts: 627

Original Poster
Rep: Reputation: 43
Thanks tired this std::thread run_thread( this->dyna... but no luck.
So if I'm understand what's being said to me I can't use a class function from a spawned thread?
 
Old 07-07-2016, 08:37 AM   #6
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,849

Rep: Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309
yes, in that context this has no value
 
Old 07-07-2016, 09:44 AM   #7
sundialsvcs
LQ Guru
 
Registered: Feb 2004
Location: SE Tennessee, USA
Distribution: Gentoo, LFS
Posts: 10,659
Blog Entries: 4

Rep: Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941
The thread main-process can be a simple "stub" that obtains the object-instance pointer by some suitable means, then calls a designated (public) method within that instance. Be sure to pass the pointer to the thread in a way that is not vulnerable to race conditions.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] Declaring class variables in headers, c++ retroCheck Programming 9 02-07-2015 04:17 PM
[SOLVED] Declaring a Vector inside a C++ Class Aquarius_Girl Programming 2 03-23-2010 02:17 AM
Base thread class in c++ using pthread true_atlantis Programming 6 10-03-2008 01:26 PM
Problem with passing a class address to a thread nalsrayatko Programming 2 11-25-2007 11:59 AM
Problem with a thread class in c++ crapodino Programming 1 09-26-2005 08:02 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 04:59 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration