c++ - responsively checking two queues without pegging CPU -


i have thread pool system uses message passing organize events, , using windows api bit of message passing. need use functions check presence of messages without blocking. if block (if use getmessage think block) while checking either queue, may miss incoming messages on other queue.

the first solution know of sleep couple of miliseconds somewhere during loop of peeking on both queues.

another way can think of have additional thread, have 1 each loop listening to. make not responsible doing other running windows message loop, use process , forward events own message queue event handled. won't work if windows sends messages i'm interested in original thread.

are there other solutions?

normally, thread pool not involved in windows message loop, , blocking indefinitely when there no work not allowable worker thread, desirable.

the elegant way of implementing thread pool can receive messages via kind of queue, automatically keeps cpu cores busy, , bonus efficient, using completion port.

createiocompletionport null handle create completion port , return handle. passing 0 numberofconcurrentthreads tells operating system keep many threads running there cores available.

create number of worker threads (a few more have cores) , createiocompletionport with handle returned first call. bind workers completion port. call getqueuedcompletionstatus infinite timeout on every worker, block them indefinitively.

make struct has overlapped first member, plus data want hand task (some pointers data, or anything).

for every task, set 1 of message structs, , postqueuedcompletionstatus completion port handle. @ application exit, post null. can use dwnumberofbytestransferred field (and completion key) pass additional info.

now windows wake 1 thread every message posted, in last-in-first-out order, number of cores available. if 1 of workers blocks on io, windows wake 1 task (keeping cpu busy long there work do).

after finishing task, go getqueuedcompletionstatus.

a way gracefully terminate workers pass "zero bytes transferred" , have worker re-post event, , exit if encounters that.


Comments

Popular posts from this blog

c# - SharpSVN - How to get the previous revision? -

c++ - Is it possible to compile a VST on linux? -

url - Querystring manipulation of email Address in PHP -