I\'m a beginner,and I always see thread creation like this: thread nameThread(fu
ID: 670456 • Letter: I
Question
I'm a beginner,and I always see thread creation like this:
thread nameThread(functionName);
The doubt is how to create thread that use a function member of a object, and this function needs a pointer to another object. Something like this:
thread nameThread( object.function( &anotherObject.attribut ) );
I tried to create a thread with a test empty function that I made out of an object, only to test the thread command, everything is compiled without errors and the program runs. But of course, without the functionalities I need.
Explanation / Answer
#include <iostream>
#include <string>
#include "Thread.h"
class HelloWorld
{
public:
DWORD print ()
{
std::cout << "Hello World!" << std::endl;
return 0;
}
};
int main(void)
{
// Random object with DWORD method (void)
HelloWorld world;
// thread should call print method of world.
Thread<HelloWorld> thread(&world, &HelloWorld::print);
if (thread.start())
std::cout << "Thread start()" << std::endl;
thread.join(); // wait for thread
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.