Guest User

Untitled

a guest
Apr 26th, 2018
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.13 KB | None | 0 0
  1. typedef struct
  2. {
  3. double speed_in_kmh;
  4. double current_power_in_mega_watts;
  5. } DeLorean;
  6.  
  7. typedef struct
  8. {
  9. unsigned int* cap_indices;
  10. unsigned int array_length;
  11. } IndexContainer;
  12.  
  13. typedef struct {
  14. unsigned char* c_string;
  15. unsigned int value;
  16. } FluxCapacitor;
  17.  
  18. DeLorean* delorean__;
  19. FluxCapacitor** capacitors__;
  20.  
  21. pthread_mutex_t lock;
  22.  
  23. // Thread function.
  24. void* assembleDeLorean(void* indices)
  25. {
  26. // Get mutex to lock function and make sure
  27. // that only one thread at a time is using it.
  28. pthread_mutex_lock(&lock);
  29.  
  30.  
  31. // Declare "indices" parameter as IndexContainer.
  32. IndexContainer* iC = indices;
  33.  
  34. double sum = 0;
  35.  
  36. // Iterate through all given indices in iC
  37. // and add value of iC to sum, if index exists.
  38. for (int i = 0; i < 121; ++i)
  39. {
  40. for (int j = 0; j < iC->array_length; ++j)
  41. {
  42. if (i == iC->cap_indices[j])
  43. {
  44. sum += capacitors__[i]->value;
  45. break;
  46. }
  47. }
  48. }
  49.  
  50. // Assign computed sum to power of global delorean.
  51. delorean__->current_power_in_mega_watts = sum;
  52.  
  53. // Release mutex.
  54. pthread_mutex_unlock(&lock);
  55.  
  56. // Stop thread.
  57. pthread_exit(NULL);
  58. }
  59.  
  60. int main(void)
  61. {
  62. printf("Main thread "main()" was started.n");
  63.  
  64. capacitors__ = createFluxCapacitorArray(121);
  65. delorean__ = createDeLorean(0, 0);
  66.  
  67. IndexContainer* iC_1 = malloc(sizeof(*iC_1));
  68. iC_1->array_length = 21;
  69. iC_1->cap_indices = malloc(21 * sizeof(unsigned int));
  70.  
  71. IndexContainer* iC_2 = malloc(sizeof(*iC_2));
  72. iC_2->array_length = 100;
  73. iC_2->cap_indices = malloc(100 * sizeof(unsigned int));
  74.  
  75. // Fill iC_1.
  76. for (int i = 0; i < 21; ++i)
  77. {
  78. iC_1->cap_indices[i] = i;
  79. }
  80.  
  81. // Fill iC_2.
  82. int k = 0;
  83. for (int i = 21; i < 121; ++i)
  84. {
  85. iC_2->cap_indices[k] = i;
  86. ++k;
  87. }
  88.  
  89. // Declare threads.
  90. pthread_t thread1, thread2;
  91. int rT1, rT2;
  92.  
  93. // Initialize mutex protecting "assembleDeLorean" function.
  94. pthread_mutex_init(&lock, NULL);
  95.  
  96. // Create & run first thread.
  97. printf("Creating and running thread1.n");
  98. rT1 = pthread_create(&thread1, NULL, assembleDeLorean, &iC_1);
  99. if (rT1 != 0)
  100. {
  101. printf("Thread 1 could not be created.n");
  102. return EXIT_FAILURE;
  103. }
  104.  
  105. printf("Return value of creation of thread1: %dn", rT1);
  106.  
  107.  
  108. // Create & run second thread.
  109. printf("Creating and running thread2.n");
  110. rT2 = pthread_create(&thread2, NULL, assembleDeLorean, &iC_2);
  111. if (rT2 != 0)
  112. {
  113. printf("Thread 2 could not be created.n");
  114. return EXIT_FAILURE;
  115. }
  116.  
  117. printf("Return value of creation of thread2: %dn", rT2);
  118.  
  119. // Wait for threads to finish.
  120. printf("Waiting for thread1 to finish...n");
  121. if (pthread_join(thread1, NULL))
  122. {
  123. printf("An error occured while joining thread1.n");
  124. return EXIT_FAILURE;
  125. }
  126.  
  127. printf("Thread1 finished!");
  128.  
  129.  
  130. printf("Waiting for thread2 to finish...n");
  131. if (pthread_join(thread2, NULL))
  132. {
  133. printf("An error occured while joining thread2.n");
  134. return EXIT_FAILURE;
  135. }
  136. printf("Thread2 finished!");
  137.  
  138. return EXIT_SUCCESS;
  139. }
Add Comment
Please, Sign In to add comment