Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

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;
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote