Question: Please help, I need the threads to print in order of ID , along with preventing another thread from starting until the first is complete.

Please help, I need the threads to print in order of ID, along with preventing another thread from starting until the first is complete. I have it setup so that it will prevent another from starting, however they are not printing on numerical order from the ID. I added a photo of the instructions as well as my current output.
Here is part of my code:
case 3:
//Run Type 3- Same as run type 2, but threads must execute their work in order of thread ID.
// You are required to use a conditional variable for run type 3.
for (int i =0; i threadData->numberOfStringsToGenerate; i++)
{
//loop through thread count
for (int j =0; j threadCount; j++)
{
//if j is equal to thread data id
if (j == threadData->id)
{
std::unique_lock lock(mutexVar);
//freeze thread
std::this_thread::sleep_for(std::chrono::milliseconds(10));
//loop through thread data string length
for (int k =0; k threadData->sharedStringLength; k++)
{
//create string using thread data
threadData->sharedString[k]='A'+ threadData->id;
}
//print thread
printf("Thread %d: %s
", threadData->id, threadData->sharedString);
}
}
}
break;
}
Please help, I need the threads to print in order

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!