ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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
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.
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.
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?
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.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.