Open cosven opened 7 years ago
几个疑问:
这篇博客说的还是有道理,APUE 这本书里面也没有提到 父/子线程 的概念。第一个问题答案应该是否,然后问题本身应该也是有问题的。
看完这一章,对解决这两个问题好像没有直接的帮助哦 - .-。
概括这章的目标 同步机制是怎样防止多个线程访问共享资源不一致的。
PID 是一个 non-negative integer,thread ID 类型是 pthread_t
它可能是个指针,也可能是个 unsigned integer
各系统不一样。
With threads, it is cleaner to return the error code from the function, thereby restricting the scope of the error to the function that caused it, instead of relying on some global state that is changed as a side effect of the function. 感觉像是个设计原则。
A single thread can exit in three ways, thereby stopping its flow of control, without terminating the entire process.
However, when one thread can modify a variable that other threads can read or modify, we need to synchronize the threads to ensure that they don’t use an invalid value when accessing the variable’s memory contents.
一个例子:The increment operation is usually broken down into three steps.
Concepts: memery cycle
A mutex
is basically a lock that we set (lock) before accessing a shared resource and release (unlock) when we’re done.
carefully controlling the order
in which mutexes are locked.时间到了,但是还没有获得 mutex,就会有 Error.
Three states are possible with a reader–writer lock: locked in read mode, locked in write mode, and unlocked. Only one thread at a time can hold a reader–writer lock in write mode, but multiple threads can hold a reader–writer lock in read mode at the same time.
A spin lock is like a mutex, except that instead of blocking a process by sleeping, the process is blocked by busy-waiting (spinning) until the lock can be acquired. A spin lock could be used in situations where locks are held for short periods of times and threads don’t want to incur the cost of being descheduled.
A barrier allows each thread to wait until all cooperating threads have reached the same point, and then continue executing from there.