|
发表于 2009-6-17 13:13:13
|
显示全部楼层
查了下书。确认ACE_WIN32_Proactor
1)是可以多线程跑event loop的
第8.5节:
Concurrency considerations. Multiple threads can execute the event loop of an ACE_WIN32_Proactor simultaneously. Since all event registration and dispatching is handled by the I/O completion port mechanism, and not by the ACE_WIN32_Proactor itself, there's no need to synchronize access to registration-related data structures, as in the ACE_WFMO_Reactor implementation (page 106).
但是后面也提到timer event要注意只能在某一个固定线程中处理。
2)也可以单线程。
8.5节的sidebar说的就是在一个线程里跑reactor+proactor
下面说下我个人的理解:
除了不适合多线程的情况外(如上面说的reactor+proactor,因为多线程reactor要考虑同步),是否多线程,用多少个线程,是出于性能考虑。
仔细看了下windows网络编程。其中提到CreateIoCompletionPort function的第四个NumberOfConcurrentThreads(就是执行event loop的线程)其值一般对应主机的cpu数目。
书中提到了worker thread,就是我们设定的跑event loop的线程。这个数目和NumberOfConcurrentThreads的区别在于,NumberOfConcurrentThreads指物理并发数,worker thread指逻辑并发数。worker thread数目常常大于NumberOfConcurrentThreads。但是如果确定worker thread不会“calls a function—such as Sleep or WaitForSingleObject—and becomes suspended”,这两个数目可以相等。
详文:
It's important to note the distinction between number of concurrent threads to specify when calling CreateIoCompletionPort versus the number of worker threads to create; they do not represent the same thing. We recommended previously that you should have the CreateIoCompletionPort function specify one thread per processor to avoid thread context switching. The NumberOfConcurrentThreads parameter of CreateIoCompletionPort explicitly tells the system to allow only n threads to operate at a time on the completion port. If you create more than n worker threads on the completion port, only n threads will be allowed to operate at a time. (Actually, the system might exceed this value for a short amount of time, but the system will quickly bring it down to the value you specify in CreateIoCompletionPort.) You might be wondering why you would create more worker threads than the number specified by the CreateIoCompletionPort call. As we mentioned previously, this depends on the overall design of your application. If one of your worker threads calls a function—such as Sleep or WaitForSingleObject—and becomes suspended, another thread will be allowed to operate in its place. In other words, you always want to have as many threads available for execution as the number of threads you allow to execute in the CreateIoCompletionPort call. Thus, if you expect your worker thread to ever become blocked, it is reasonable to create more worker threads than the value specified in CreateIoCompletionPort's NumberOfConcurrentThreads parameter. |
|