Skip to content

Instantly share code, notes, and snippets.

@ademar111190
Last active August 29, 2015 14:22
Show Gist options
  • Save ademar111190/1ede9544c1d5b4570623 to your computer and use it in GitHub Desktop.
Save ademar111190/1ede9544c1d5b4570623 to your computer and use it in GitHub Desktop.
A little example of how manipulate matrix with threads in C

#A little example of how manipulate matrix with threads in C

  • compile:

$ gcc -pthread squareMatrixItemsWithThreads.c -o squareMatrixItemsWithThreads

  • run

$ ./squareMatrixItemsWithThreads

#include <stdio.h>
#include <pthread.h>
void* square(void* arg) {
int* item = (int*) arg;
*item *= *item;
}
int main() {
int i, max = 11, numbers[max];
pthread_t threads[max];
for(i = 0; i < max; i++) {
numbers[i] = i;
pthread_create(&(threads[i]), NULL, square, &numbers[i]);
}
for(i = 0; i < max; i++) {
pthread_join(threads[i], NULL);
printf("Number at position %d has value %d\n", i, numbers[i]);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment