Download c source code

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

void* run(void* data)
{
	size_t thread_num = (size_t)data;
//	if ( thread_num < 10 )
//		sleep(1);
	printf("Hello world from secondary thread %zu\n", thread_num);
	return NULL;
}

int main(int argc, char* argv[])
{
//	for ( int index = 0; index < argc; ++index )
//		fprintf(stderr, "%d[%s]\n", index, argv[index]);
	
	size_t thread_count = sysconf(_SC_NPROCESSORS_ONLN);
	if ( argc >= 2 )
		thread_count = strtoull(argv[1], NULL, 10);
		
	//pthread_t thread[thread_count];
	pthread_t* threads = (pthread_t*) malloc(thread_count * sizeof(pthread_t));

	for ( size_t index = 0; index < thread_count; ++index )
		pthread_create(&threads[index], NULL, run, (void*)index);

	printf("Hello world from main thread\n");

	for ( size_t index = 0; index < thread_count; ++index )
		pthread_join(threads[index], NULL);
		
	free(threads);
	return 0;
}