Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Notify thread when data is added in queue

I have one thread which is adding data in the queue, now I want that other thread should get notified when the data is added so that it can start processing data from queue.

one option is thread will poll the queue continuously to see if count is more than zero but I think this is not good way, any other suggestion will be greatly appreciated

Any suggestion how I can achieve this, I am using .net framework 3.5.

and what if i have two thread one is doing q.Enqueue(data) and other is doing q.dequeue(), in this case do i need to manage the lock..?

like image 465
Real Master Avatar asked Mar 23 '23 08:03

Real Master


1 Answers

You can use ManualResetEvent to notify a thread.

ManualResetEvent e = new ManualResetEvent(false);

After each q.enqueue(); do e.Set() and in the processing thread, you wait for items with e.WaitOne().

If you do processing inside a loop, you should do e.Reset() right after e.WaitOne().

like image 102
Mehraban Avatar answered Apr 02 '23 08:04

Mehraban