Thread safe array

  • Thread starter andreas.zetterstrom
  • Start date
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.
 
I

Ian Collins

I'm implementing some different c++ classes which I want to be thread
safe.

comp.programming.threads or c.l.c++ would be more appropriate places to ask.
 
A

andreas.zetterstrom

comp.programming.threads or c.l.c++ would be more appropriate places to ask.

Yeah I noticed, sorry about that, I have posted it on c++ instead.
 
C

CBFalconer

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.

If you apply the same care to your code that you did to selecting a
newsgroup on which to post this, I can guarantee it won't work.
Neither will your post. This is c.l.c, and C++ is off-topic.
 
K

Kenny McCormack

Ian Collins said:
That was a particularly arseholy post, considering I'd already
redirected him to the appropriate groups and he'd apologised for posting
here.

You expected anything less (else) from CBF?
 
C

CBFalconer

Ian said:
That was a particularly arseholy post, considering I'd already
redirected him to the appropriate groups and he'd apologised for
posting here.

Bear in mind that the order of deliverance depends on how the
newsreader orders the thread. Mine presents earlier messages
first. I hadn't even seen the responses you reference.
 
C

Chris M. Thomasson

CBFalconer said:
Bear in mind that the order of deliverance depends on how the
newsreader orders the thread. Mine presents earlier messages
first. I hadn't even seen the responses you reference.

Wow, your newsreader "really" supports top-posting!


;^)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,228
Members
46,818
Latest member
SapanaCarpetStudio

Latest Threads

Top