Advertisement
Thiff

Threads2b

Nov 7th, 2017
173
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.52 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <time.h>
  4. #include <iostream>
  5. #include <sys/time.h>
  6. #include <sys/param.h>
  7. #include <pthread.h>
  8. #include <math.h>
  9. #define TYPE float
  10.  
  11. using namespace std;
  12.  
  13. const int count_Thread = 2;
  14. const int my_length = 10;
  15.  
  16.  
  17. struct thread_argument
  18. {
  19.     int id;
  20.     int from, to;
  21.     TYPE *data;
  22. };
  23.  
  24.  
  25.  
  26. float randomFloat(float minR,float maxR){
  27.     return (minR + 1) + (((float) rand()) / (float) RAND_MAX) * (maxR - (minR + 1));
  28.  
  29. }
  30.  
  31. int randomInt(int minR, int maxR){
  32.     return rand()%(maxR-minR + 1) + minR;
  33.  
  34. }
  35. void BubbleSort_dsc(TYPE *arr, int start, int end){
  36.     TYPE tmp;
  37.     for(int i = start; i < end; i++)
  38.     {
  39.         for(int j = start; j < end - 1; j++)
  40.         {
  41.             if(arr[j+1] > arr[j])
  42.             {
  43.                 tmp = arr[j + 1];
  44.                 arr[j + 1] = arr[j];
  45.                 arr[j] = tmp;
  46.             }
  47.         }
  48.     }
  49.  
  50. }
  51.  
  52. void BubbleSort_asc(TYPE *arr, int start, int end){
  53.     TYPE tmp;
  54.     for(int i = start; i < end; i++)
  55.     {
  56.         for(int j = start; j < end - 1; j++)
  57.         {
  58.             if(arr[j+1] < arr[j])
  59.             {
  60.                 tmp = arr[j + 1];
  61.                 arr[j + 1] = arr[j];
  62.                 arr[j] = tmp;
  63.             }
  64.         }
  65.     }
  66.  
  67. }
  68. void fill_array(TYPE *arr, int my_length, int minR, int maxR)
  69. {   TYPE temp;
  70.     for (int i = 0; i < my_length; i++)
  71.     {
  72.         temp = randomFloat(minR,maxR);
  73.         arr[i] = roundf(temp * 100) / 100.0;
  74.         //arr[i] = randomInt(minR, maxR);
  75.  
  76.     }
  77. }
  78.  
  79.  
  80. void *thread_sorting( void *void_arg )
  81. {
  82.     thread_argument *ptr_data = (thread_argument*) void_arg;
  83.     BubbleSort_asc(ptr_data->data, ptr_data->from, ptr_data->to);
  84.  
  85.     return NULL;
  86. }
  87.  
  88. int main()
  89. {   srand( ( int ) time( NULL ) );
  90.     TYPE *my_array = new TYPE [my_length];
  91.  
  92.     pthread_t pt[count_Thread];
  93.     thread_argument ta[count_Thread];
  94.     int minR = -100.00;
  95.     int maxR = 100.00;
  96.  
  97.     for (int i = 0; i < count_Thread; i++)
  98.     {
  99.         ta[i].id = i;
  100.         ta[i].from = i * (my_length/count_Thread);
  101.         ta[i].to = ta[i].from + (my_length/count_Thread);
  102.         ta[i].data = my_array;
  103.     }
  104.  
  105.     fill_array(my_array, my_length, minR, maxR);
  106.  
  107.     for (int i = 0; i < count_Thread; i++)
  108.     {
  109.         pthread_create( &pt[i], NULL, thread_sorting, &ta[i] );
  110.     }
  111.     for (int i = 0; i < count_Thread; i++)
  112.     {
  113.         pthread_join(pt[i], NULL);
  114.     }
  115.     for (int i = 0; i < my_length; i++)
  116.     {
  117.         cout << my_array[i] << endl;
  118.     }
  119. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement