#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]); | |
| } | |
| } |