4

How to implement waitAny on more than 64 handles?

I have simple problem, i have many threads working until end of data, when thread is end of data then signalling to thread on what i running this waitAny for idle thread and give him next package of data.

Svisstack
  • 16,203
  • 6
  • 66
  • 100
  • 3
    Note that this kind of a problem may be a signal that your design could stand some rethinking. – Michael Burr Jan 31 '12 at 16:33
  • related question with WaitAll: http://stackoverflow.com/questions/2702545/workaround-for-the-waithandle-waitall-64-handle-limit – Marek Jan 31 '12 at 16:35

3 Answers3

5

You might want to consider implementing something like a queue for notification packets for the 'waitAny' thread to wait on. When one of your multitude of threads completes it's operation, it places a notification packet on the queue. Your waitAny turns into a wait on a single event that indicates something is in the queue.

Michael Burr
  • 333,147
  • 50
  • 533
  • 760
  • +1 the 'cascading waits' sounds very messy and would require more than one context-change. TBH, OP sounds like a P-C queue and a pool of threads would do. – Martin James Jan 31 '12 at 17:13
3

Implement cascading waits with additional threads to wait for pack of WaitHandles and signaling the other up on hierarchy.

Or test them in bulks without actual waits (without timeout set)

Oleg Dok
  • 21,109
  • 4
  • 45
  • 54
1

Effective wait is limited by API MAXIMUM_WAIT_OBJECTS which is 64. Apart from having your app redesigned to use fewer handles, you can either switch to poll mode (wait for each 64 separately with timeout of zero, and wait for small non-zero amount if all handles are not signaled), or split objects and waits between multiple threads of execution.

You might also want to revisit earlier discussions, e.g.

Community
  • 1
  • 1
Roman R.
  • 68,205
  • 6
  • 94
  • 158