views:

142

answers:

2

Hi,
I have a program (C#) with a list of tests to do.
Also, I have two thread. one to add task into the list, and one to read and remove from it the performed tasks.
I'm using the 'lock' function each time one of the threads want to access to the list.
Another thing I want to do is, if the list is empty, the thread who need to read from the list will sleep. and wake up when the first thread add a task to the list. Here is the code I wrote:

...
List<String> myList = new List();
Thread writeThread, readThread;
writeThread = new Thread(write);
writeThread.Start();
readThraed = new Thread(read);
readThread.Start();
...
private void write()
{
   while(...)
   {
     ...
     lock(myList)
     {
        myList.Add(...);
     }
     ...
     if (!readThread.IsAlive)
     {
        readThraed = new Thread(read);
        readThread.Start();
     }
     ...
   }
   ...
}

private void read()
{
bool noMoreTasks = false;
   while (!noMoreTasks)
   {
      lock (MyList)//syncronize with the ADD func.
      {
                if (dataFromClientList.Count > 0)
                {
                    String task = myList.First();
                    myList.Remove(task);
                }
                else
                {
                    noMoreTasks = true;
                }      
      }
      ...
   }
   readThread.Abort();
}

Apparently I did it wrong, and it's not performed as expected (The readTread does't read from the list).
Does anyone know what is my problem, and how to make it right?
Many thanks,

+3  A: 

What you need is a blocking queue. It is like a normal queue except the Dequeue method blocks if nothing is queued. Here is one implementation. Once you get the blocking queue implemented then everything else is easy. Just be careful which implementation of the blocking queue you use. I have seen a lot of examples out there that have subtle threading problems. It might be best to stick with the link I provided.

public class Example
{
  private BlockingQueue<Task> m_Queue = new BlockingQueue<Task>();

  public void StartExample()
  {
    Thread producer = new Thread(() => Producer());
    Thread consumer = new Thread(() => Consumer());
    producer.Start();
    consumer.Start();
    producer.Join();
    consumer.Join();
  }

  private void Producer()
  {
    for (int i = 0; i < 10; i++)
    {
      m_Queue.Enqueue(new Task());
    }
  }

  private void Consumer()
  {
    while (true)
    {
      Task task = m_Queue.Dequeue();
    }
  }
}
Brian Gideon
Thanks for your fast answer, I want to try your answer, but I don't have the BlockingQueue class. Do I need to add some using?
menacheb
Take a deep breath and read the answer one more time; slowly this time. Brian provides a link to an implementation of the BlockingQueue class in his answer.
Seventh Element
A BlockingQueue implementation is included in .NET 4.0.
Brian Gideon
+2  A: 

I'd advise you to take a look at Jon Skeet's Producer Consumer example. For more information about Producer Consumer, check out Wikipedia

Jean Azzopardi
+1 Excellent resource.
Brian Gideon