Created
February 1, 2016 10:42
-
-
Save agalera/3947a49b733ba128b28f to your computer and use it in GitHub Desktop.
Test threads in C
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <pthread.h> | |
void* calculate() | |
{ | |
int i = 0; | |
int b = 0; | |
for(i=0; i<100000000; i++){ | |
b = i+b; | |
} | |
} | |
int main() | |
{ | |
int threads = 8; | |
pthread_t sum[threads]; | |
int i = 0; | |
for (i = 0; i<threads; i++) | |
{ | |
printf("thread started\n"); | |
pthread_create(&sum[i], NULL, calculate, NULL); | |
} | |
for (i = 0; i<threads; i++) | |
{ | |
pthread_join(sum[i], NULL); | |
printf("thread end\n"); | |
} | |
} | |
// gcc test.c -o program -lpthread; ./program |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment