dimanche 17 février 2019

C++11 demonstrating task parallelism using two threads

#include <iostream>
#include <thread>
#include <windows.h>

using namespace std;

const int arr[6] = {12, 7, 45, 56, 3, 21};
int minimum = arr[0];
int i;

void find_minimum()
{
   for(i=0;i<6;i++)
   {
      if(arr[i]<minimum)
      {
         minimum = arr[i];
      }
    }
      cout<<"The minimum element in the array is: ";
      cout<<minimum;
      cout<<endl;
    }

 void find_maximum()
  {
    for(i=0;i<6;i++)
    {
       if(arr[i]>minimum)
       {
          minimum = arr[i];
       }
    }
     cout<<"The maximum element in the array is: ";
     cout<<minimum;
     cout<<endl;
  }

  int main()
  {
    thread th1(find_minimum);
    Sleep(5000);
    thread th2(find_maximum);

    th1.join();
    th2.join();

    return 0;
  }

We were issued with an assignment to use two threads to implement task parallelism (using any programming language). I decided to use C++11 since that's the language I'm slightly familiar with but I'm not sure if I took the right approach. Does the above code demonstrate task parallelism? The code works perfectly fine.

Aucun commentaire:

Enregistrer un commentaire