进程和线程的区别

进程更重量,线程更轻。
线程共享同一个进程的地址空间,可以通过共享内存通信。但多进程不可以,他们有独自的地址空间。
创建新的线程只会多线程栈 线程id pc指针之类的东西,不会增加文件描述符。

在父子关系方面。多次fork出来的是树,多次pthread_create出来的是一个主进程和一堆相同地位的线程。

相关函数

•Creating and reaping threads
•pthreadcreate()
•pthread_join()
•Determining your thread ID 返回当前线程id
•pthread_self()
•Terminating threads
•pthread_cancel()
•pthread_exit()
•exit() [terminates all threads]
•return [terminates current thread]
•Synchronizing access to shared variables
•pthread_mutex_init
pthread_mutex
[un]lock

多线程容易出错的原因

操作共享变量会竞争。多个线程之间共享的变量在C里有堆上的对象 全局变量 本地景泰变量等

条件变量

作用是避免忙等,和锁(二元信号量)配合使用。