Add locking to ensure thread-safety. Fix #44
Merge branch '#44_thread_safety' into onitu