Shows basic usage of the pthread library
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
struct computation
{
int argument;
unsigned long result;
};
unsigned long fib(int n)
{
if(n < 2) return 1;
else return fib(n-2) + fib(n-1);
}
void* calc_fib(void* raw_ptr)
{
computation* c = (computation*)raw_ptr;
c->result = fib(c->argument);
return (void*)0;
}
int main(int argc, const char* argv[])
{
int arg = (argc>1)?atoi(argv[1]):42;
if(arg <= 0) arg = 42;
computation comp = {arg, 0};
pthread_t worker;
pthread_create(&worker, (pthread_attr_t*)0, calc_fib, (void*)&comp);
pthread_join(worker, (void**)0);
fprintf(stdout, "fib(%i) = %lu\n", comp.argument, comp.result);
return 0;
}