Guest User

Untitled

a guest
May 26th, 2018
111
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.64 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <pthread.h>
  4. #include <assert.h>
  5.  
  6. #define VECTOR_COL 10000000
  7. #define VECTOR_ROW 10
  8.  
  9. typedef struct {
  10. int* start;
  11. int* end;
  12. int count;
  13. } IncrementArgs;
  14.  
  15. void* increment_subsection(void* increment_args)
  16. {
  17. IncrementArgs* a = increment_args;
  18.  
  19. for (; a->start < a->end; a->start++)
  20. {
  21. *(a->start) = a->count++;
  22. }
  23.  
  24. return NULL;
  25. }
  26.  
  27. int main (void)
  28. {
  29. // Too big for stack, allocate elsewhere
  30. int* vector = (int*) calloc(VECTOR_COL * VECTOR_ROW, sizeof(int));
  31. int err = 0;
  32. IncrementArgs increment_args;
  33. IncrementArgs increment_args2;
  34. pthread_t set_lower_arr;
  35. pthread_t set_upper_arr;
  36.  
  37. increment_args.start = vector;
  38. increment_args.end = vector+(VECTOR_COL/2)*VECTOR_ROW;
  39. increment_args.count = 0;
  40. err = pthread_create(&set_lower_arr, NULL, increment_subsection,
  41. &increment_args);
  42. if (err)
  43. {
  44. printf("Error creating lower thread %d\n", err);
  45. return 1;
  46. }
  47. increment_args2.start = vector+(VECTOR_COL/2)*VECTOR_ROW;
  48. increment_args2.end = vector+(VECTOR_COL*VECTOR_ROW);
  49. increment_args2.count = VECTOR_COL*VECTOR_ROW / 2;
  50. err = pthread_create(&set_upper_arr, NULL, increment_subsection,
  51. &increment_args2);
  52. if (err)
  53. {
  54. printf("Error creating upper thread %d\n", err);
  55. return 1;
  56. }
  57.  
  58. if (pthread_join(set_upper_arr, NULL))
  59. {
  60. printf("Error joining thread\n");
  61. return 2;
  62. }
  63.  
  64. if (pthread_join(set_lower_arr, NULL))
  65. {
  66. printf("Error joining thread\n");
  67. return 2;
  68. }
  69.  
  70. free(vector);
  71.  
  72. return 0;
  73. }
Add Comment
Please, Sign In to add comment