c# - Synchronizing the performance of two algorithms to that of the slowest bottleneck -


i have 2 methods. 1 method retrieves data, , processes data. both algorithms operate @ different speed want synchronize performance of both algorithms run @ speed of bottleneck. thinking of using message queue of maximum size. until queue has reached it's maximum size, retrieval method carries on populating list whilst processing method removes items list on different thread long there items on list. if list reaches maximum size wait until queue no longer @ maximum size.

does sound logical approach?

i thinking of form of generic class along lines of

queue<t> _thequeue; private int _maxqueuesize;  func<t> _processor; func<t> _populator;  chasedqueue(func<t> processor, funct<t> populator, int maxqueuesize = 30) {     _thequeue = new queue<t>();     _maxqueuesize =     _processor = processor;     _populator = populator; }  public void start() {     new thread(() => startchaser()).start();     new thread(() => startpopulator()).start(); }  private void startchaser() {     while ((element = _documentqueue.poll) != null)      {         _processor(_documentqueue.dequeue());     }  }     private void startpopulator() {     foreach(var item in _populator)     {         while(_thequeue.count < _maxqueuesize)         {             _documentqueue.enqueue(item);         }        } } 

promoting comment fully-fledged answer, completeness:

reactiveextensions, namely zip, seems fit [example here]‌​. 2 algorithms expose streaming results observables zip...


Comments

Popular posts from this blog

How to provide Authorization & Authentication using Asp.net, C#? -

toolbar - How to add link to user registration inside toobar in admin joomla 3 custom component -

How to use Authorization & Authentication in Asp.net, C#? -