Skip to content

Instantly share code, notes, and snippets.

@agalera
Created February 1, 2016 10:42
Show Gist options
  • Save agalera/3947a49b733ba128b28f to your computer and use it in GitHub Desktop.
Save agalera/3947a49b733ba128b28f to your computer and use it in GitHub Desktop.
Test threads in C
#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