Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <pthread.h>
- #include <assert.h>
- #define VECTOR_COL 10000000
- #define VECTOR_ROW 10
- typedef struct {
- int* start;
- int* end;
- int count;
- } IncrementArgs;
- void* increment_subsection(void* increment_args)
- {
- IncrementArgs* a = increment_args;
- for (; a->start < a->end; a->start++)
- {
- *(a->start) = a->count++;
- }
- return NULL;
- }
- int main (void)
- {
- // Too big for stack, allocate elsewhere
- int* vector = (int*) calloc(VECTOR_COL * VECTOR_ROW, sizeof(int));
- int err = 0;
- IncrementArgs increment_args;
- IncrementArgs increment_args2;
- pthread_t set_lower_arr;
- pthread_t set_upper_arr;
- increment_args.start = vector;
- increment_args.end = vector+(VECTOR_COL/2)*VECTOR_ROW;
- increment_args.count = 0;
- err = pthread_create(&set_lower_arr, NULL, increment_subsection,
- &increment_args);
- if (err)
- {
- printf("Error creating lower thread %d\n", err);
- return 1;
- }
- increment_args2.start = vector+(VECTOR_COL/2)*VECTOR_ROW;
- increment_args2.end = vector+(VECTOR_COL*VECTOR_ROW);
- increment_args2.count = VECTOR_COL*VECTOR_ROW / 2;
- err = pthread_create(&set_upper_arr, NULL, increment_subsection,
- &increment_args2);
- if (err)
- {
- printf("Error creating upper thread %d\n", err);
- return 1;
- }
- if (pthread_join(set_upper_arr, NULL))
- {
- printf("Error joining thread\n");
- return 2;
- }
- if (pthread_join(set_lower_arr, NULL))
- {
- printf("Error joining thread\n");
- return 2;
- }
- free(vector);
- return 0;
- }
Add Comment
Please, Sign In to add comment