0
votes

I would store objects in my std list on thread-safe way. I don't want to block a thread, if it want to access an element that currently not in use. If I can't lock only one element, there is an other question: If I call a destructor on one of my object in the list, will it remove the element and invalidate it's iterator?

3
Could you briefly explain why you need this (what you're doing), how the lifetime of the objects in the list is and how the threads operate on the list (e.g. iterate over the whole or something else and how do they know which item to work on)?murrekatt

3 Answers

0
votes

Generally you need to protect the whole list container instance by a lock or mutex in multi threaded environment.

If you are storing new'ed objects in your container, then the destructor will be called only when you are deleting the pointer to the object.

Further answer to your question depends upon:

1. What are you storing in list ? Object itself or Pointer to object

2. "If I call a destructor on one of my object in the list"... Does this mean Deleting the list node or retrieving the pointer from list and then deleting it instead of deleting the list node [This is not good unless you point the pointer to NULL] .

If you are storing object itself the list.remove will call destructor for the object.

If you are storing pointer to the object .. list.remove will not call destructor and I think its a no-op as well [correct me if I am wrong here]

3. The standard gives a guaratee that list iterator will not be invalidated on addition and splicing ... but will become invalid if the iterator pointing to the removed element.

0
votes

Can I lock an element in a list to make it threadsafe, instead of locking the whole list?

Yes, if all threads use a lock whenever reading/writing/destructing the element, then that is safe.

For example, if the element was a string, and one thread took the lock, appended a character, then released the lock, then another thread can:

  • safely attempt to do the same, to the same element, contesting the same lock

  • append a different element to the list without any lock

  • NOT do a find() in the list; the find may attempt a read on the partially updated string

  • NOT sort() the list

  • NOT erase() the element from the list

I don't want to block a thread, if it want to access an element that currently not in use.

Well, you need some level of granularity. The finest possible is a lock per element, but that's potentially very wasteful of space (and wasted memory eventually impacts performance).

If I can't lock only one element, there is an other question: If I call a destructor on one of my object in the list, will it remove the element and invalidate it's iterator?

No. Calling a destructor doesn't normally affect the list (unless your elements themselves are aware of the list holding them and their destructors are coded to remove themselves). Normally, you instead erase the element from the list and that calls the destructor and releases the associated heap memory. If the element is itself a raw pointer, then you need to call delete on it yourself (smart_pointer's automated that for you). Iterators, pointer, references to the erased element are immediated invalidated.

0
votes

To manipulate The List, you should LOCK around The List. Once your object is extracted, you can release The List Lock, and LOCK around the object only.