English 中文(简体)
从C++的2个不同校对获取同一物体的不同数据成员
原标题:Accessing different data members belonging to the same object from 2 different thread in C++

我有几个目标需要在++的不同侧面上采取行动。 我知道,必须锁定可能同时使用不止一个透镜的任何变数,但如果每一读物能(书写)同一物体的不同数据成员? 例如,每一条镜头都使用不同的物体方法,而任何一种称为更改同一数据成员的方法都不使用。 只要我不接触同一数据成员,或我是否需要把整个物体锁上门,那是否安全?

我探讨了关于这一专题的解释和细节,但每一个例子似乎都侧重于单一变量或非成员的职能。

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?

问题回答

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.

是的,可以安全地通过不同距离接触一个物体的不同成员。

我认为你可以这样做。 但是,你最好能确信,这种方法在获得相同数据方面从未改变,或者电话程序并没有决定采用另一种方法,另一种方法已经使用。

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

确实,是,你可以这样做,但正如其他人指出的那样,你不应有。 IMHO, 获取数据成员的途径应当经过宽松/优良的方法,以便任何必要的相互交流/重要划分/绘制地图/点名均在物体内封。

<><>>>> 只要我不接触同一数据成员,或者我是否需要把整个物体锁上任何轨道?

答案完全取决于该类别的设计,但我仍要说,总是建议考虑100次,然后允许多面镜进入同一物体。 鉴于这一事实,如果你确信数据是 ,那么数据是独立的<>/strong>,就必须锁定整个物体。

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

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.





相关问题
Undefined reference

I m getting this linker error. I know a way around it, but it s bugging me because another part of the project s linking fine and it s designed almost identically. First, I have namespace LCD. Then I ...

C++ Equivalent of Tidy

Is there an equivalent to tidy for HTML code for C++? I have searched on the internet, but I find nothing but C++ wrappers for tidy, etc... I think the keyword tidy is what has me hung up. I am ...

Template Classes in C++ ... a required skill set?

I m new to C++ and am wondering how much time I should invest in learning how to implement template classes. Are they widely used in industry, or is this something I should move through quickly?

Print possible strings created from a Number

Given a 10 digit Telephone Number, we have to print all possible strings created from that. The mapping of the numbers is the one as exactly on a phone s keypad. i.e. for 1,0-> No Letter for 2->...

typedef ing STL wstring

Why is it when i do the following i get errors when relating to with wchar_t? namespace Foo { typedef std::wstring String; } Now i declare all my strings as Foo::String through out the program, ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

Window iconification status via Xlib

Is it possible to check with the means of pure X11/Xlib only whether the given window is iconified/minimized, and, if it is, how?