user3462417
user3462417

Reputation: 11

Synchronize access and delete item in a list from different threads.

I have a list of objects. While one thread will add or remove objects in this list depending on some conditions. While other thread will access objects in this list. Ex:

List<node*> list


Thread1:

 {
   list.append(node);
   list.removeOne(index);
   ............

 }
Thread2
{

   Node* node = list.at(index);
   if(node)
     doSomething(node);
}

How to Thread2 avoid accessing null pointer when executing doSomething(node) but node is delete from thread1 ;

Upvotes: 1

Views: 61

Answers (1)

Vince
Vince

Reputation: 3577

While this can be solved using standard lock mechanism (see mutex), you may want to look into the Reader-Writer specific synchronization primitive. It will allow to have more than one reader, but only one writer can process the list at a time.

Upvotes: 2

Related Questions