Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to create an atomic vector or array in C++?

I have some code which uses an array of int (int[]) in a thread which is activated every second.

I use lock() from std::mutex to lock this array in this thread.

However I wonder if there is a way to create an atomic array (or vector) to avoid using a mutex? I tried a couple of ways, but the compiler always complains somehow?

I know there is a way to create an array of atomics but this is not the same.

like image 517
rainbow Avatar asked Sep 06 '17 07:09

rainbow


People also ask

What are atomic vectors?

Atomic vectors are probably the most fundamental data structure in the R programming language. An atomic vector is different from a one-dimensional array: an array has a dim attribute of length one while a vector has no such attribute. An atomic vector is also different from a list.

Can an array be a vector?

An array is a list that is arranged in multiple dimensions. A two-dimensional array is a vector of vectors that are all of the same length. We see that A has three rows and four columns and that each row is one of the vectors in the list. An array can contain any of the IDL data types.

How many types of atomic vectors are present?

Atomic vectors, of which there are six types: logical, integer, double, character, complex, and raw. Integer and double vectors are collectively known as numeric vectors.

Why would you use an array instead of a vector?

Vector is better for frequent insertion and deletion, whereas Arrays are much better suited for frequent access of elements scenario. Vector occupies much more memory in exchange for managing storage and growing dynamically, whereas Arrays are a memory-efficient data structure.


1 Answers

In practice, at the CPU level, there are instructions which can atomically update an int, and a good compiler will use these for std::atomic<int>. In contrast, there are are no instructions which can atomically update a vector of ints (for any architecture I am aware of), so there has got to be a mutex of some sort somewhere. You might as well let it be your mutex.


For future readers who haven't yet written code with the mutex:

You can't create a std::atomic of int[10], because that leads to a function which returns an array - and you can't have those. What you can do, is have a std::atomic<std::array<int,10>>

int main() {   std::atomic<std::array<int,10>> myArray; } 

Note that the compiler/library will create a mutex under the hood to make this atomic. Note further that this doesn't do what you want. It allows you to set the value of the whole array atomically.

It doesn't allow you to read the whole array, update one element, and write the whole array back atomically.

The reads and the writes will be individually atomic, but another thread can get in between the read and the write.

You need the mutex!

like image 62
Martin Bonner supports Monica Avatar answered Sep 17 '22 15:09

Martin Bonner supports Monica