Question
HELLO, I only need help/answer for the First bold part and the last two I did it but just need checking if correct or what
HELLO, I only need help/answer for the First bold part and the last two I did it but just need checking if correct or what I am missing. Thanks
struct ThreadStruct
{
int id; // ID of the thread
int sharedStringLength; // Length of the shared string
int numberOfStringsToGenerate; // Number of strings a single thread will generate
char *sharedString; // Shared string that will be generate in each thread. This memory is shared among all threads.
///////////////////////////////////////////////////// // TODO: Add any extra variables needed by the threads here //////////////////////////////////// };
///////////////////////////////////////////////////// // Prompts the user to press enter and waits for user input //////////////////////////////////////////
void Pause() {
printf("Press enter to continue ");
getchar();
}
///////////////////////////////////////////// //
Entry point for worker threads. //
// Arguments: // threadData - Pointer to per-thread data for this thread. /////////////////////////////////////////////
void ThreadEntryPoint(ThreadStruct *threadData) {
/////////////////////////////////////// // TODO: Add code to this function to make it run according to the run type.
// However do NOT duplicate the following code. /////////////////////////////////////////////
for(int i = 0; i < threadData->numberOfStringsToGenerate; i++,
std::this_thread::sleep_for(std::chrono::milliseconds(10)))
{ for(int j = 0; j < threadData->sharedStringLength; j++)
{
std::this_thread::sleep_for(std::chrono::milliseconds(1));
threadData->sharedString[j] = 'A' + threadData->id; }
printf("Thread %d: %s ", threadData->id, threadData->sharedString);
}
///////////////////////////////////////////////////////////////////////////////////
}
int main(int argc, char** argv)
{
ENABLE_LEAK_DETECTION();
int threadCount = 0;
int sharedStringLength = 0;
int numberOfStringsToGenerate = 0;
int runType = 0;
char *sharedString = nullptr;
ThreadStruct *perThreadData = nullptr;
/////////////////////////////////////////////////////////////////////////////////// // TODO:: Handle the runType command line argument.
// // The following code already handles the first 4 arguments by ignoring
// the first argument (which is just the name of the program) and reading
// in the next 3 (threadCount, sharedStringLength, and numberOfStringsToGenerate). //
// You will need to add code to this section to read in the final command line
// argument (the runType).
///////////////////////////////////
if (argc != 4) { fprintf(stderr, "Error: missing or incorrect command line arguments "); fprintf(stderr, "Usage: RaceCondition threadCount sharedStringLength numberOfStringsToGenerate runType "); fprintf(stderr, "Arguments: "); fprintf(stderr, " threadCount Number of threads to create. "); fprintf(stderr, " sharedStringLength Length of string to generate. "); fprintf(stderr, " numberOfStringsToGenerate Number of strings to generate per thread. "); fprintf(stderr, " runType The run type. "); Pause(); return 1; }
threadCount = atoi(argv[1]); sharedStringLength = atoi(argv[2]); numberOfStringsToGenerate = atoi(argv[3]); runType = atoi(argv[4]);
if (threadCount < 0 || sharedStringLength < 0 || numberOfStringsToGenerate < 0 || runType < 0) { fprintf(stderr, "Error: All arguments must be positive integer values. "); Pause(); return 1; }
printf("%d thread(s), string sharedStringLength %d, %d iterations, %d runType ", threadCount, sharedStringLength, numberOfStringsToGenerate, runType);
sharedString = new char[sharedStringLength + 1]; memset(sharedString, 0, sharedStringLength + 1); perThreadData = new ThreadStruct[threadCount];
///////////////////////////////////////////// // TODO:: You will need a container to store the thread class objects. It is up to you // to decide how you want to store the threads. /////////////////////////////////////////////////////////////////////////////////// // NOTE: Do NOT change this for loop header
for (int i = threadCount - 1; i >= 0; i--) { perThreadData[i].id = i; perThreadData[i].sharedStringLength = sharedStringLength; perThreadData[i].numberOfStringsToGenerate = numberOfStringsToGenerate; perThreadData[i].sharedString = sharedString;
/////////////////////////////////////////////////////////////////////////////////// // TODO:: Setup any additional variables in perThreadData and start the threads. // MUST be done in this for loop. /////////////////////////////////////////////////////////////////////////////////// perThreadData[i].mtx = &mtx; perThreadData[i].currentThread = ¤tThread; threads.push_back(std::thread(ThreadEntryPoint, &perThreadData[i])); }
/////////////////////////////////////////////////////////////////////////////////// // TODO:: Wait for all of the threads to finish. Since we are using // Joinable threads we must Join each one. Joining a thread will cause // the calling thread (main in this case) to block until the thread being // joined has completed executing. /////////////////////////////////////////////////////////////////////////////////// for (auto& thread : threads) { thread.join(); } Pause();
/////////////////////////////////////////////////////////////////////////////////// // TODO: Clean up /////////////////////////////////////////////////////////////////////////////////// delete[] perThreadData; return 0;
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started