contributed <nekoneko
>
Lock-Free Programming Techniques
不懂的詞: ABA Problem,
CAS, LockMUTEX
Atomic Read-Modify-Write
在多執行序的情況下,對資料的讀寫使用atomic操作,保證資料的一致性
Compare And Swap
atomic的實做有不同的方式,在恐龍本中提到test-and-set和compare-and-swap,兩種都是為了在多執行序的情況下,只讓一個執行序進入critical session。以下是恐龍本書中程式碼。
搭配看影片: Memory Ordering Intro - Georgia Tech jserv
- 有提到三種防止memory Ordering的方式。目前沒看懂之後再補。cheng hung lin
Different Processors Have Different Memory Models
如標題,各個cpu對於自家的亂序執行都有其不同的方式。
參考資料
atomic , read-modify-write, 恐龍本(OS) 6.4章, tundergod的筆記
asm volatile("lwsync" ::: "memory")
Win32 Interlocked
operation, except on Xbox 360load(std::memory_order_acquire)
pthread_mutex_lock
share state不懂,之後再補
有看沒有懂,之後在研究cheng hung lin
這篇的下一篇為FP in C的實做,Functional programming in C – Implementation
英文太差,中文表達不好,還是沒有佷讀透其中道理。
- Concurrency (並行) vs. Parallelism (平行)
cheng hung lin
Sequential-before
,happen-before
Cache Coherence
尾聲的部份跟主題沒關,但可以先看一下cheng hung lin
Coroutines are computer program components that generalize subroutines for --nonpreemptive multitasking, by allowing multiple entry points for suspending and resuming execution at certain locations.
直接看man setjmp
和man longjmp
,與參考瞿同學的筆記和程式碼
setjmp
:
save stack context for nonlocal goto
setjmp() and sigsetjmp() return 0 if returning directly, and nonzero when returning from longjmp(3) or siglongjmp(3) using the saved context.
longjmp
:
nonlocal jump to a saved stack context
longjmp() restores the environment saved by the last call of setjmp(3) with the corresponding env argument.
longjmp() cannot cause 0 to be returned. If longjmp() is invoked with a second argument of 0, 1 will be returned instead.
longjmp(jump_buff_*, val)
是跳回最後一次呼叫setjmp(jump_buffer_*)
的位置,所以說這也是為什麼會加if (r == 0) /* call other function */
,避免一直回覆呼叫同一個函式後又跳回原呼叫的setjmp(jump_buff_*)
,此外,longjmp
是禁止以0為參數的,若傳0進去,則會在setjmp回傳1。目前先跳過cheng hung lin
先略cheng hung lin
有些pthread函式的manual找不到的話,可能是沒有裝glibc的manual
Common thread modules:
Thread Safe
函式庫也要保證thread safe
Discussion on calling pthread_exit()
from main()
:
By having main() explicitly call pthread_exit() as the last thing it does, main() will block and be kept alive to support the threads it created until they are done.
man pthread_exit()
的Notes也有提到: To allow other threads to continue execution, the main thread should terminate by calling pthread_exit() rather than exit(3).
exit()
,連帶整個task都會結束(die)。若是呼叫pthread_exit()
,則是把main當作task裡的其中一個thread,只會把main thread關掉,整個task仍然存在,其他衍生的thread也得以繼續執行。pthread_exit()
void pthread_exit(void *retval)
stack management:
pthread_attr_setstack/pthread_attr_getstack
做stack調整,而非pthread_attr_setstackattr/pthread_attr_getstackattr
。man pthread_attr_setstackattr
裡提到,這函式的攜帶性(portable)不足。x
的waiting queue,當其他thread有符合x
這個condition時,喚醒x
waiting queue裡的thread。thread pool
?
LD_PRELOAD
libc.so
).man ld.so
打星號的部份,是編譯產生phonebook_*
會使用到的檔案
violate
register
first-class objects
nekoneko