Thread synchronization and thread types in C++ using visual studiosPlease provide comments of what you did so that I can learn

1 answer below »
Thread synchronization and thread types in C++ using visual studiosPlease provide comments of what you did so that I can learn
Answered Same DayDec 08, 2021

Answer To: Thread synchronization and thread types in C++ using visual studiosPlease provide comments of what...

Arun Shankar answered on Dec 10 2021
140 Votes
#include
#include
#include
#include
#include
#include
#include
#include
// Include file and line nu
mbers for memory leak detection for visual studio in debug mode
#if defined _MSC_VER && defined _DEBUG
    #include
    #define new new(_NORMAL_BLOCK, __FILE__, __LINE__)
    #define ENABLE_LEAK_DETECTION() _CrtSetDbgFlag(_CRTDBG_ALLOC_MEM_DF | _CRTDBG_LEAK_CHECK_DF)
#else
    #define ENABLE_LEAK_DETECTION()
#endif
pthread_mutex_t mutex1;
int* status;
int* stringsStatus;
int count;
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
int runType; // run type
char *sharedString;
    // Shared string that will be generate in each thread. This memory is shared among all threads
};
// Prompts the user to press enter and waits for user input
void Pause()
{
    printf("Press enter to continue\n");
    getchar();
}
// Entry point for worker threads.
// Arguments:
// threadData - Pointer to per-thread data for this thread.
void ThreadEntryPoint(ThreadStruct *threadData)
{
bool waiting;
    switch(threadData->runType)
{
case 0: // runType = 0
for(int i = 0; i < threadData->numberOfStringsToGenerate; i++)
{
pthread_mutex_lock(&mutex1);
std::cout<<"\nThread "<id<<": ";
for(int j = 0; j < threadData->sharedStringLength; j++)
{
usleep(1);
threadData->sharedString[j] = 'A' + threadData->id;
}
printf("%s", threadData->sharedString);
pthread_mutex_unlock(&mutex1);
usleep(10);
}
break;
case 1: // runType = 1
for(int i = 0; i < threadData->numberOfStringsToGenerate; i++)
{
...
SOLUTION.PDF

Answer To This Question Is Available To Download

Related Questions & Answers

More Questions »

Submit New Assignment

Copy and Paste Your Assignment Here