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

EARLIER HERE IN CHEGG I MADE THIS QUESTION: I\'m using Visual Studio and I would

ID: 670455 • Letter: E

Question

EARLIER HERE IN CHEGG I MADE THIS QUESTION:

I'm using Visual Studio and I would like to know how to use thread to the thread runs a function explore() of a object called candidate. The function explore() expects a pointer to an attribute of a object, in this case, the object is maze and the attribute is another object, challenge.

if (candidate.directionX.positive)

{

thread Q(candidate.explore(&maze.challenge));

}

-----------------------------------------------------------------------------------------------------

ONE PERSON ANSWERED ME:

Worker workerObject = new Worker();
Thread workerThread = new Thread(candidate.explore(&maze.challenge));
if (candidate.directionX.positive)
{
workerThread.Start();
}

---------------------------------------------------------------------------------------------

I UNDERSTOOD THAT "Worker" IS THE TYPE OF THE OBJECT "candidate", AND "Thread" WAS THE "thread" COMMAND. SO, I DID THIS CODE BELOW, BECAUSE IT MAKES NO ERROS SINTAX WHEN I TIPED IN VISUAL STUDIO.

Worker *workerObject = new Worker();
thread * workerThread = new thread(candidate.explore(&maze.challenge));
if (candidate.directionX.positive)
{
workerThread; // OBS: workerThread doesn't have start() member.
}

--------------------------------------------------------------------------------------------------------

I HAD THIS ERROR WHEN I TRIED TO COMPILE.

Error (active) no instance of constructor "std::thread::thread" matches the argument list

----------------------------------------------------------------------------------------------------------------------------------

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

The function can be modified as follows:

# include <thread>

# include <iostream.h>

# include <conio.h>

void worker ( )

{

// write your code here

}

int main ( )

{

worker workerobject = new worker ( ); // create object of worker () method

Thread workerthread = new Thread ( ); // create object of Thread class

try

{

std :: thread workerthread ( workerobject.worker (std :: bind (&maze.challange))) ;

t.join ( );

}

catch (Exception e)

{

}

return 0;

}

Now in this method, t.join() method waits for thread to die. So here this method will stop execcution of first thread till the thread which calls pointer of &maze.challange completes its work.

std :: thread constructor can also be called with instances of classes that implement function call operator.

WE can pass arguments directly to the std :: thread constructor and they will be copied into the internal storage for the new thread and supplied to to the thread function using thread.bind() method.