cosven / cosven.github.io

个人零碎笔记,博客草稿,阅读笔记
10 stars 0 forks source link

读《Advanced Programming in the UNIX Environment 3rd Edition》 #45

Open cosven opened 7 years ago

cosven commented 7 years ago
cosven commented 5 years ago

第 11 章 - Threads

几个疑问:

这篇博客说的还是有道理,APUE 这本书里面也没有提到 父/子线程 的概念。第一个问题答案应该是否,然后问题本身应该也是有问题的。

看完这一章,对解决这两个问题好像没有直接的帮助哦 - .-。

读《Advanced Programming in the UNIX Environment 3rd Edition》Chapter 11. Threads

Introduction

概括这章的目标 同步机制是怎样防止多个线程访问共享资源不一致的。

Thread Concepts

一个进程跑多线程的好处
  1. 简化处理异步事件的代码
  2. 多进程访问同一个资源麻烦
  3. 一些问题可以细分,从而提高整个程序吞吐量
    A thread consists of the information necessary to represent an execution context within a process. This includes
    • a thread ID that identifies the thread within a process
    • a set of register values
    • a stack
    • a scheduling priority and policy
    • a signal mask
    • an errno variable (recall Section 1.7)
    • thread-specific data (Section 12.6).

      Thread Identification

PID 是一个 non-negative integer,thread ID 类型是 pthread_t 它可能是个指针,也可能是个 unsigned integer 各系统不一样。

Thread Creation

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. 感觉像是个设计原则。

Thread Termination

A single thread can exit in three ways, thereby stopping its flow of control, without terminating the entire process.

  1. The thread can simply return from the start routine. The return value is the thread’s exit code.
  2. The thread can be canceled by another thread in the same process.
  3. The thread can call pthread_exit. 内容较多诶

    Thread Synchronization

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.

  1. Read the memory location into a register.
  2. Increment the value in the register.
  3. Write the new value back to the memory location.

Concepts: memery cycle

Mutexes

A mutex is basically a lock that we set (lock) before accessing a shared resource and release (unlock) when we’re done.

Deadlock Avoidance

时间到了,但是还没有获得 mutex,就会有 Error.

Reader-Writer Locks

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.

Reader–Writer Locking with Timeouts
Conditions Variables (貌似没怎么看到过)
Spin Locks

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.

Barriers (少见,没懂)

A barrier allows each thread to wait until all cooperating threads have reached the same point, and then continue executing from there.