I spin off a new thread that reads data from a database. Every X records a flag is signaled where the main thread will then process the records that have been retrieved keeping some and discarding others. When the flag is signaled I use a lock to allow the reader thread to wait until the processor thread to finish processing the records. However, it appears the lock is not doing this because as I iterate through the records, more keep getting added (indicating the reader thread is still reading). This causes the collection to be modified resulting in an InvalidOperationExecption.
Maybe I have misunderstood what the purpose of “lock” is or I am not using it correctly. Here is pseudo of what I have:
readonly object locker = new object();
Dictionary screened = new Dictionary;
Search(){
Thread reader = new Thread( () => Read("search terms") );
reader.Start();
while( found < desiredAmount ){
if(SIGNAL){
lock(locker){
ProcessRecords();
}
}
}
}
Read(){
Connect to DB
while(reader.Read()){
screened.add(record);
}
}
ProcessRecords(){
foreach(var x in screened){
//process record
}
}
I hope the pseudo was good enough, from my understanding Read() should not execute while in the lock block. Please help me understand lock a little better.
PS Yes I have read MSDNs articles on locks and still do quite grasp how to use lock in more complex situations.
You’ll need to put a lock around the while loop as well. The lock would work if you have two or more threads contending for the same lock, in your above sample, you have no contention, because no other thread requests a lock apart from the first.
A better way would be to put the lock inside ProcessRecords() as well.