Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

725
Vistas
C/C++ arrays with threads - do I need to use mutexes or locks?

I am new to using threads and have read a lot about how data is shared and protected. But I have also not really got a good grasp of using 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 the 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.

My questions then:

  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?

  2. 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?

  3. Should I use atomic data types, and will there be any significant time overhead if I do?

  4. Many answers to this type of question seem 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?

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

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.

over 4 years ago · Santiago Trujillo
3 Respuestas
Responde la pregunta

0

  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?

No. Because the data is never modified, there cannot be synchronization problem.

  1. 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?

Depends.

  1. If any other thread is going to read the element, you need synchronization.
  2. If any thread may modify the size of the vector, you need synchronization.

In any case, take care of not writing into adjacent memory locations by different threads a lot. That could destroy the performance. See "false sharing". Considering, you probably don't have a lot of cores and therefore not a lot of threads and you say write is done only once, this is probably not going to be a significant problem though.

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

If you use locks (mutex), atomic variables are not necessary (and they do have overhead). If you need no synchronization, atomic variables are not necessary. If you need synchronization, then atomic variables can be used to avoid locks in some cases. In which cases can you use atomics instead of locks... is more complicated and beyond the scope of this question I think.

Given the description of your situation in the comments, it seems that no synchronization is required at all and therefore no atomics nor locks.

  1. ...Would my array elements in this example be aligned, or is there some way to ensure they are?

As pointed out by Arvid, you can request specific alignment using the alginas keyword which was introduced in c++11. Pre c++11, you may resort to compiler specific extensions: https://gcc.gnu.org/onlinedocs/gcc-5.1.0/gcc/Variable-Attributes.html

over 4 years ago · Santiago Trujillo Denunciar

0

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. No. Mutexes are not needed since threads are only reading the array.

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

  3. 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.

over 4 years ago · Santiago Trujillo Denunciar

0

The only time you need to use Locks is when data is modified on a shared resource. Eg if some threads where used to write data and some used to read data (in both cases from the same resource) then you only need a lock for when writing is done. This is to prevent whats known as "race".

There is good information of race on google for when you make programs that manipulate data on a shared resource.

over 4 years ago · Santiago Trujillo Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda