带线程的C/C ++数组-我需要使用互斥锁或锁吗? [英] C/C++ arrays with threads - do I need to use mutexes or locks?

查看:180
本文介绍了带线程的C/C ++数组-我需要使用互斥锁或锁吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是使用线程的新手,并且已经阅读了很多有关如何共享数据和保护数据的知识.但是我还没有真正了解何时需要使用互斥锁和锁来保护数据.

I am new to using threads and have read a lot about how data is shared and protecting data. But I have also not really got a good grasp of when I need to use mutexes and locks to protect data.

以下是我将要解决的问题的描述.需要注意的重要一点是,这将是时间紧迫的,因此我需要尽可能减少开销.

Below is a description of the problem I will be working on. The important thing to note is that it will be time critical so I need to reduce overheads as much as possible.

我有两个固定大小的双精度数组.

I have two fixed size double arrays.

  • 第一个数组将为后续计算提供数据. 线程将从中读取值,但永远不会对其进行修改.一个 元素可能会在任何时候被任何线程读取.

  • The first array will provide data for subsequent calculations. Threads will read values from it but it will never be modified. An element may be read at some time by any of the threads.

第二个数组将用于存储计算结果
由线程执行.该数组的元素将永远是 由一个线程更新,并且仅在结果值
时更新一次 被写入其中.

The second array will be used to store results of the calculations
performed by the threads. An element of this array will only ever be updated by one thread, and probably only once when the result value
is written to it.

那我有什么问题吗?

  1. 每次从只读数组访问数据时,是否真的需要在线程中使用互斥锁?如果可以的话,您能解释为什么吗?

  1. Do I really need to use a mutex in a thread each time I access the data from the read-only array? If so could you explain why?

即使它是有史以来唯一写入该元素的线程,在写入结果数组时我是否需要在线程中使用互斥锁?

Do I need to use a mutex in a thread when it writes to the result array even though this will be the only thread that ever writes to this element?

我应该使用原子数据类型吗?如果这样做,会不会有任何重要的时间花在上面?

Should I use atomic data types and will there be any significant time over head if I do?

对于这种类型的问题,很多答案似乎都是-不,如果您的变量对齐,则不需要互斥量.在此示例中,我的数组元素会对齐还是有某种方法可以确保它们对齐?

A lot of answers to this type of question seems to be - no, you don't need the mutex if your variables are aligned. Would my array elements in this example be aligned, or is there some way to ensure they are?

该代码将在64位linux上实现.我打算使用Boost库进行多线程处理.

The code will be implemented on 64bit linux. I am planning on using Boost libraries for multithreading.

感谢所有回复和评论 我一直在考虑这个问题,并在网上浏览了好几天,一旦发布了答案,清楚的解释很快就回来了.有一个可接受的答案",但是所有答案和评论都同样有用.再次感谢

thanks for all the responses and comments I have been mulling this over and looking all over the web for days, and once posted the answer and clear explanations came back in literally seconds. There is an "accepted answer", but all the answers and comments were equally helpful. Thanks again

推荐答案

在给出的两个条件下,不需要互斥体.请记住,每次使用互斥锁(或任何同步结构)都是性能开销.因此,您希望尽可能避免使用它们(当然,在不影响正确代码的情况下).

Under the two conditions given, there's no need for mutexes. Remember every use of a mutex (or any synchronization construct) is a performance overhead. So you want to avoid them as much as possible (without compromising correct code, of course).

  1. 否.不需要互斥对象,因为线程仅读取数组.

  1. No. Mutexes are not needed since threads are only reading the array.

否.由于每个线程只写一个不同的内存位置,所以没有竞争条件.

No. Since each thread only writes to a distinct memory location, no race condition is possible.

否.这里不需要原子访问对象.实际上,使用原子对象可能会对性能产生负面影响,因为它阻止了诸如重新排序操作之类的优化可能性.

No. There's no need for atomic access to objects here. In fact, using atomic objects could affect the performance negatively as it prevents the optimization possibilities such as re-ordering operations.

这篇关于带线程的C/C ++数组-我需要使用互斥锁或锁吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆