Use pthread_mutex_t for all locks
Add pthread header to chan.h
Fix deadlock issue
Fix Makefile