Accessing different data members belonging to the same object from 2 different thread in C++
Asked Answered
H

6

8

I have a few objects I need to perform actions on from different threads in c++. I known it is necessary to lock any variable that may be used by more than one thread at the same time, but what if each thread is accessing (writing to) a different data member of the same object? For example, each thread is calling a different method of the object and none of the methods called modify the same data member. Is it safe as long as I don't access the same data member or do I need to lock the whole object anyway?

I've looked around for explanations and details on this topic but every example seems to focus on single variables or non-member functions.

To summarize: Can I safely access 2 different data members of the same object from 2 different thread without placing a lock on the whole object?

Hammertoe answered 26/4, 2012 at 13:51 Comment(2)
How can you be sure you will never access same member from two threads at a time?Winzler
can't you have a lock per struct variable instead of lock per struct. fine grained locking ??Encumber
B
5

It is effectively safe, but will strongly reduce the performance of your code if you do that often. Computers use things called "cache lines" and if two processors are working on the same cache line they'll have to pass it back & forth all the time, slowing your work down.

Buckling answered 26/4, 2012 at 13:54 Comment(1)
it may also be unsafe if the data members are not properly alignedIty
C
3

Yes, it is safe to access different members of one object by different thread.

Cnidoblast answered 26/4, 2012 at 13:54 Comment(0)
M
1

I think you can do that fine. But you better be sure that that the method internals never change to access the same data or the calling program doesn't decide to call another method that another thread is already using etc.

So possible, but potentially dangerous. But then it will also be quicker because you'll be avoiding calls to get mutexes. Pick your poison.

Mons answered 26/4, 2012 at 13:55 Comment(0)
A
1

Well, yes, OK you can do it but, as others have pointed out, you should not have to. IMHO, access to data members should be via getter/setter methods so that any necessary mutexing/criticalSectioning/semaphoring/whatever is encapsulated within the object.

Albert answered 26/4, 2012 at 14:4 Comment(0)
D
0

Is it safe as long as I don't access the same data member or do I need to lock the whole object anyway?

The answer totally depends upon the design of the class, However I would still say that it is always recommended to think 100 times before allowing multiple threads to access same object. Given the fact, If you are sure that the data is really independent their is NO need to lock the whole object.

Then a different question arises, "If variables are indeed independent Why they are in same class ?" Be careful threading kills if mistaken.

Decillion answered 26/4, 2012 at 13:58 Comment(0)
N
0

You might want to be careful. See for example http://gcc.gnu.org/ml/gcc/2012-02/msg00032.html Depending on how the fields are accessed, you might run across similar hard to find problems.

Neomineomycin answered 26/4, 2012 at 14:30 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.