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,
A Mutex is a lock that we set before using a shared resource and release after using it. When the lock is set, no other thread can access the locked region of code.
Locks guard access to data Locks are used to guard a shared data variable, like the account balance shown here. If all accesses to a data variable are guarded (surrounded by a synchronized block) by the same lock object, then those accesses will be guaranteed to be atomic — uninterrupted by other threads.
The lock statement acquires the mutual-exclusion lock for a given object, executes a statement block, and then releases the lock. While a lock is held, the thread that holds the lock can again acquire and release the lock. Any other thread is blocked from acquiring the lock and waits until the lock is released.
Another way to classify locks is by what happens when the lock strategy prevents the progress of a thread. Most locking designs block the execution of the thread requesting the lock until it is allowed to access the locked resource. With a spinlock, the thread simply waits ("spins") until the lock becomes available.
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();
}
}
}
I'd advise you to take a look at Jon Skeet's Producer Consumer example. For more information about Producer Consumer, check out Wikipedia
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With