进程线程相关

default actions to signals

  1. almost all signals in POSIX. 1-1900, the default is to terminate without or with core dump.
  2. few signals are ignored like SIGCHLD
  3. SIGSTOP cause the program to suspend and SIGCONT to continue. common situation is use CTRL-Z in shell

c++11 threads

Eli Bendersky's website: C++11 threads, affinity and hyperthreading

c++11 thread_local

目前使用编译器的选项设置:windows:*__declspec(thread)* linux: *__thread*

使用recursive_mutex问题

task A wants to modify a shared data structure and take a lock on its recursive mutex. It then blocks on some OS call; the task gets taken off current thread, and task B start executing. task B take a lock on the same recursive mutex successfully because it executed on the same thread, then it will reads or modify the data which was in the middle of being modified by task A, then a disaster happen!

IPC方式

  1. 匿名管道(pipe) [进程间要有父子关系]
  2. 具名管道 (mkfifo)
  3. signal
  4. 消息队列
  5. 共享内存
  • 1,2 vs 4
    • [1,2 进程写消息时,管道必须已经打开来读, 否则会产生SIGPIPE信号]
    • 持续性不同, 管道是随进程的持续性,当管道最后一次关闭发生时,里面的数据就会丢失;而消息队列是随内核的持续性, 只要内核没有重新自举,消息队列不会被删除

epoll(linux), kqueue(freebsd), iocp(windows)

线程池大小的阻抗匹配原则

p:密集计算所占的时间比重(0 < p <= 1) c: cpu数目 线程池大小的经验公式: T = c/p (考虑到p值的估计不是很准,T的最佳值可以上下浮动50%)

多线程的相关原则: 一个文件描述符只在一个线程中操作

阻塞与非阻塞//同步与异步

  1. 同步与异步 同步和异步关注的是消息通信机制 (synchronous communication/ asynchronous communication)

所谓同步,就是在发出一个调用时,在没有得到结果之前,该调用就不返回。但是一旦调用返回,就得到返回值了。 换句话说,就是由调用者主动等待这个调用的结果。

而异步则是相反,调用 在发出之后,这个调用就直接返回了,所以没有返回结果。换句话说,当一个异步过程调用发出后,调用者不会立刻得到结果。而是在调用发出后,被调用者 通过状态、通知来通知调用者,或通过回调函数处理这个调用。

  1. 阻塞与非阻塞

阻塞和非阻塞关注的是程序在等待调用结果(消息,返回值)时的状态.

阻塞调用是指调用结果返回之前,当前线程会被挂起。调用线程只有在得到结果之后才会返回。 非阻塞调用指在不能立刻得到结果之前,该调用不会阻塞当前线程。

-------------本文结束感谢您的阅读-------------