Hi Yufeng,
Refrain from using pthread_cancel and pthread_exit. Those are Big Hammers and Sharp Knives.
Instead, set a variable that is monitored by the thread and set by the owner-thread / main thread which signals the child thread to terminate gracefully.
Mutex / condvar yada yada yada like normal apply.
HTH, --Eljay