Download c source code

#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <unistd.h>

/*
int pthread_create(pthread_t *thread, const pthread_attr_t *attr,
    void* (*start_routine)(void*), void *arg);
*/

typedef struct
{
	size_t thread_id;
	size_t thread_count;
	size_t* next_thread_to_print;
} thread_data_t;

void* run(void* data)
{
	thread_data_t* thread_data = (thread_data_t*)data;
	while ( *thread_data->next_thread_to_print < thread_data->thread_id )
		; // busy-wait
		
	printf("Hello from thread %zu of %zu\n", (*thread_data).thread_id, thread_data->thread_count);
	++*thread_data->next_thread_to_print;
	
	return NULL;
}

int main(int argc, char* argv[])
{
	size_t thread_count = sysconf(_SC_NPROCESSORS_ONLN);
	if ( argc >= 2 )
	{
		if ( sscanf(argv[1], "%zu", &thread_count) != 1 || thread_count == 0 )
			return (void)fprintf(stderr, "hello_w: error: invalid thread count: %s\n", argv[1]), 1;
	}
	
	pthread_t* threads = (pthread_t*)malloc(thread_count * sizeof(pthread_t));
	if ( threads == NULL )
		return (void)fprintf(stderr, "hello_w: error: could not allocate memory for: %zu threads\n", thread_count), 2;

	struct timespec start_time;
	clock_gettime(CLOCK_MONOTONIC, &start_time);

	thread_data_t* thread_data = (thread_data_t*) calloc(thread_count, sizeof(thread_data_t));
	size_t next_thread_to_print = 0;
	
	for ( size_t index = 0; index < thread_count; ++index )
	{
		thread_data[index].thread_id = index;
		thread_data[index].thread_count = thread_count;
		thread_data[index].next_thread_to_print = &next_thread_to_print;
		pthread_create(&threads[index], NULL, run, thread_data + index);
	}
	
	printf("Hello from main thread\n");
		
	for ( size_t index = 0; index < thread_count; ++index )
		pthread_join(threads[index], NULL);

	struct timespec finish_time;
	clock_gettime(CLOCK_MONOTONIC, &finish_time);
	
	double seconds = finish_time.tv_sec - start_time.tv_sec
		+ (finish_time.tv_nsec - start_time.tv_nsec) * 1e-9;

	printf("Thread creation and join time: %.9lfs\n", seconds);

	free(thread_data);
	free(threads);

	return 0;
}