Ah, a nodeshell to claim as my own!
POSIX thread, for
multithreaded programming on a
UNIX-like
system. Here's an
incredibly basic example of how to use pthreads (you should really look into
manpages...)
/*
When compiling, link this to libpthread:
$ cc filename.c -o filename -lpthread
*/
#include <stdio.h>
#include <unistd.h>
#include <pthread.h>
void *my_first_thread( void *arg ) {
printf( "This is my first thread; looping infinitely\n" );
while(1);
}
void *my_second_thread( void *arg ) {
printf( "This is my second thread\n" );
sleep( 2 );
return NULL;
}
int main() {
pthread_t my_thread1, my_thread2;
int r;
void *return_value;
r = pthread_create( &my_thread1, NULL, *my_first_thread, NULL );
if( r != 0 ) {
fprintf( stderr, "failed to create thread\n" );
return 1;
}
r = pthread_create( &my_thread2, NULL, *my_second_thread, NULL );
if( r != 0 ) {
fprintf( stderr, "failed to create thread\n" );
return 1;
}
pthread_join( my_thread2, &return_value );
printf( "my_second_thread() ended returning %p, so now I shall quit.\n",
return_value );
return 0;
}