Hello - thanks for taking the time to read this. I am developing a program in which work is queued into either a DB or an array, and multiple (about 100) threads perform the work by obtaining the next item in the list and processing it.

Obviosuly, there is a huge risk of threads deadheading or getting the same entry from the array or db when a second thread queries for the next item to process before the first thread has had an opportunity to remove the item from the queue.

My thoughts are to use a static class method and boolean to lock the queue:
public static List<string> dataQueue = new List<string>;
public static bool isQLocked = false;
public static void lockQ(){

Then, in the instance threads, each will first lock the queue by calling the static lockQ() method before processing. Once the method returns, it would be assumed that the calling instance had exclusive control, as all other instances would be stuck waiting for isQLocked to be made false again:

//no other instance of this method should be able to get here now -- this instance should have exclusive control
string nextInLine = dataQueue[0];
//done with getting the next item in the queue - release the lock to the next instance

Question is -- does this work? I have tried other methods and keep failing. It's hours and hours to clear out my garbage and work something like this, so I'm hoping for some insight and advice before I have to try it again.

Am I right in thinking that the first instance class calling the static method would get the lock, and then all other calling instances would be queued and released one by one? Or would it happen like other things I've tried where as soon as the first calling instance releases the lock, there is a massive jumble and the risk of releasing two of the same queue items still exists?

Thanks so much in advance for your help and any infromation you can provide as to how might be the best way to "feed" a single array to 100 threads such that they don't get duplicates.

I'm wondering if this concept