Just following pthread tutorial from CMU http://www.cs.cmu.edu/afs/cs/academic/class/15492-f07/www/pthreads.html