1

Assume I have some array. All items are initially null Thread1 writes to array. Thread2 blocks until all items in array are set (and then process).

I was doing it like that: I've created one another array with AutoResetEvent. In Thread1 every time i update array item i call Set to corresponding AutoResetEvent and in Thread2 I just WaitHandle.WaitAll(events);

But now I do think this is not efficient. I think that I should probably use one event. Because I can count how many items are updated yet, i can just raise event on last update.

This is simplified exmple, in real life things are a little bit harder, but probably you can suggest something better?

Also should I use Volatile.Read in Thread1? (I have double-CPU machine).

Oleg Vazhnev
  • 23,239
  • 54
  • 171
  • 305

1 Answers1

0

What not take a look at a BlockingCollection so you can add your items in a thread safe manner?

You could look at using a thread safe ObserveableCollection to get your notifications too.

Community
  • 1
  • 1
Jon
  • 38,814
  • 81
  • 233
  • 382