Skip to content

Instantly share code, notes, and snippets.

@atomictom
Created April 22, 2014 00:54
Show Gist options
  • Save atomictom/11161792 to your computer and use it in GitHub Desktop.
Save atomictom/11161792 to your computer and use it in GitHub Desktop.
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <pthread.h>
#include <unistd.h>
#include <sys/types.h>
#define NUM_THREADS 8
#define PRINT_TIME
struct thread_data_t{
int thread_id;
};
void * say_hello(void * args){
struct thread_data_t * data = (struct thread_data_t *) args;
int thread_id = data->thread_id;
printf("Thread %d says hello!\n", thread_id);
return NULL;
}
int main(void){
pthread_t threads[NUM_THREADS];
struct thread_data_t thread_data[NUM_THREADS];
/* ---------- Start timing ---------- */
time_t start = time(NULL);
/* ---------- Create and run threads ---------- */
int i;
for(i = 0; i < NUM_THREADS; i++){
thread_data[i].thread_id = i;
pthread_create(&threads[i], NULL, &say_hello, &thread_data[i]);
}
/* ---------- Wait for threads to finish ---------- */
for(i = 0; i < NUM_THREADS; i++){
pthread_join(threads[i], NULL);
}
/* ---------- Finish timing ---------- */
time_t end = time(NULL);
/* ---------- Print time ---------- */
#ifdef PRINT_TIME
printf("\nTime: %ld\n", end - start);
#endif
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment