user1590636
user1590636

Reputation: 1194

Queue and While loop

i have 2 threads one adds to a Enqueue and one does the Dequeue and do some processing,

now sometimes the Queue will become empty, and it might couple minutes to fill it with one elment, on the other hand it can have couple hundred elements if the data was local, so it has a mix of local data which is added fast and data that has to be first downloaded.

now if i do something like

While(queue.count > 0)
{  
    //lock denque and process
} 

it would exit the loop and the thread will end, on the other hand if i do something like

While(queue.count > 0 && DownloaderThreadisRunning)
    {  
        //lock denque and process
    } 

i will have another problem since some objects might take processing time to a point where the Enqueue thread has ended its job, so it wont get in the loop.

i thought of something like

While(queue.count > 0)
        {  
            //lock denque and process
         if(!DownloaderThreadisRunning && queue.Count==0)
         {
            break;
         }
        } 

but are thre any built in solutions to manage such things?

Upvotes: 1

Views: 4968

Answers (1)

TalentTuner
TalentTuner

Reputation: 17556

if you can use ConcurrentQueue than you do have lot more abstraction over creating your own Producer and Consumer.

Upvotes: 2

Related Questions