A
andreas.zetterstrom
I'm implementing some different c++ classes which I want to be thread
safe. All these classes contain lists or arrays of some kind.
I'm using protected sections to make them thread safe.
The question then is: how do you in a nice safe way pick values out of
the list? The easiest way is to have a pair of Lock, Unlock functions,
but this also presents a lot of ways of doing a misstake.
Say the list class has 5 functions, one to get the number of items,
one to get items one by one and 3 other functions. The 3 functions
have their locks internally as per c++ design you don't want other
classes need to know anything about internal structure of another
class. The 2 other functions is the problem. I would have to have
external locks to lock them as the number of items might change while
going through the list otherwise. 3 problems arise with that solution:
you might forget (or don't know that it's needed) to lock the list
before using it, you might forget to unlock the list while done and
you might deadlock against other list functions that you didn't know
were blocking too (or because you thought external locks were needed
there as well).
How to solve this in a nice way? I've been thinking of perhaps
supplying a list to a special function in the list class that then
transfers all the list data in the class to the supplied list and thus
having a copy that won't risk changing. This will take double the
memory and will require an extra loop through the data though.
safe. All these classes contain lists or arrays of some kind.
I'm using protected sections to make them thread safe.
The question then is: how do you in a nice safe way pick values out of
the list? The easiest way is to have a pair of Lock, Unlock functions,
but this also presents a lot of ways of doing a misstake.
Say the list class has 5 functions, one to get the number of items,
one to get items one by one and 3 other functions. The 3 functions
have their locks internally as per c++ design you don't want other
classes need to know anything about internal structure of another
class. The 2 other functions is the problem. I would have to have
external locks to lock them as the number of items might change while
going through the list otherwise. 3 problems arise with that solution:
you might forget (or don't know that it's needed) to lock the list
before using it, you might forget to unlock the list while done and
you might deadlock against other list functions that you didn't know
were blocking too (or because you thought external locks were needed
there as well).
How to solve this in a nice way? I've been thinking of perhaps
supplying a list to a special function in the list class that then
transfers all the list data in the class to the supplied list and thus
having a copy that won't risk changing. This will take double the
memory and will require an extra loop through the data though.