万字长文,慎入。
PDF 版下载: http://files.cppblog.com/Solstice/multithreaded_server.pdf
目录
1 进程与线程
2 典型的单线程服务器编程模型
3 典型的多线程服务器的线程模型
One loop per thread
线程池
归纳
4 进程间通信与线程间通信
5 进程间通信
6 线程间同步
互斥器(mutex)
跑题:非递归的mutex
条件变量
读写锁与其他
封装MutexLock、MutexLockGuard 和Condition
线程安全的Singleton 实现
归纳
7 总结
后文预览:Sleep 反模式
On Feb 12, 10:51 pm, 机械唯物主义 : linjunhalida <linjunhal...@gmail.com>
wrote:
> 其实我更关注如何根据需求,来考虑到底是用:
> 单线程阻塞,
> 单线程非阻塞(是否有点类似while loop forever+中断?)
> 多线程(消息传递,共享内存,线程存在的意义,是否是作为一个资源的管理者?)
> 大量线程(线程池)
> 多进程,
> 大量进程(进程池)
> 还有再上面的集群。。。
>
> 以及不同状况下的设计方法和调试方法。
>
> 2010/2/12 机械唯物主义 : linjunhalida <linjunhal...@gmail.com>
>
> > 感觉这篇文章还是深层次的交流性质的文章,
> > 如果在c++和boost上面加点注释方便非c++程序员阅读就好了。
> > 不过,好像非C++程序员也很少有机会接触到造轮子的状况,一般直接用封装好的库。。
>
> > 2010/2/12 机械唯物主义 : linjunhalida <linjunhal...@gmail.com>
>
> > 看起看起。0bug事件没过多久就出来叫阵好文,这打脸太狠了吧?
>
> >> 2010/2/12 Tinyfool <tinyf...@gmail.com>
>
> >> C++实在是不在行,好好的看了一遍,还是不太懂
>
> >>> 2010/2/12 Shuo Chen <giantc...@gmail.com>
2010/2/12 机械唯物主义 : linjunhalida <linjun...@gmail.com>:
On Feb 12, 11:09 pm, SpitFire <spitfi...@gmail.com> wrote:
> 正好想问一下,跨机器的message queue有哪些轻量高效的实现?苦苦寻找中
>
> 2010/2/12 Shuo Chen <giantc...@gmail.com>
刚才大概看了看你的文档, 你关于Semaphore的文字大概可以用这本书的章节作为答案:
http://docs.sun.com/app/docs/doc/816-5137/sync-11157?a=view
The two basic sorts of semaphores are binary semaphores and counting
semaphores. Binary semaphores never take on values other than zero or
one, and counting semaphores take on arbitrary nonnegative values. A
binary semaphore is logically just like a mutex.
However, although not always enforced, mutexes should be unlocked only
by the thread that holds the lock. Because no notion exists of “the
thread that holds the semaphore,” any thread can perform a V or
sem_post (3RT) operation.
Counting semaphores are nearly as powerful as conditional variables
when used in conjunction with mutexes. In many cases, the code might
be simpler when implemented with counting semaphores rather than with
condition variables, as shown in Example 4–14, Example 4–15, and
Example 4–16.
另外这本书还介绍了另外一种同步机制就是Barrier,其实就是基于Memory Barrier实现的。
多线程编程在用户层是可选的,在内核里却是必须的。Pthread的线程同步机制在内核里都有对应实现,所以说,精通多线程编程的人,写内核程序应该会顺手很多了。
在 2010年2月12日 下午9:51,Shuo Chen <gian...@gmail.com> 写道:
--
Cheers,
Oliver Yang
Twitter: http://twitter.com/yangoliver
Blog: http://blog.csdn.net/yayong
--------------------------------------------------------------------
An OpenSolaris Developer