+ 1

What is included in the atomic header of c++, in what case would i need it?

4th Oct 2017, 5:35 PM
Kai
Kai - avatar
8 Answers
+ 15
In this example with regard to your description (if I have gotten it right!), I have been created a hypothetical program to deal with arrays update in a regular way. Please add your expectation from that then I will try to find an answer for that (consider that I don't have any background on multithreading! But I will do my best to find a solution for that.) int main() { int size = 1000000; int a[size] = {0, 1, 2, ..., 999999}; int b[size] = {0, 1, 2, ..., 999999}; f_a(a, size); .... } // Just for array a void f_a(int *x, int s) { // Updating array a by new values for (int i= 0; i < size; ++i) { cout << x[i] << " + 10 = "; x[i] += 10; cout << x[i] << endl; } }
4th Oct 2017, 7:22 PM
Babak
Babak - avatar
+ 14
atomic library defines classes and template classes to use to create types that support atomic operations. [https://msdn.microsoft.com/en-us/library/hh874894.aspx] What's an atomic operation then? As a vague definition, it deals with multithreading and concurrency. For clear description see: [https://en.wikipedia.org/wiki/Linearizability]
4th Oct 2017, 5:53 PM
Babak
Babak - avatar
+ 13
Thanks Kai for your cooperation. I begin my research as soon as possible.
4th Oct 2017, 8:53 PM
Babak
Babak - avatar
+ 12
Hey Kai I've done that using thread library instead of atomic. It works concurrently fine. If you didn't satisfy with that tell me to search a bit more. Here is the code, but SL doesn't support that. The second link is a live version of the code in another online compiler which fully supports C++ 11 and above. [https://code.sololearn.com/c1xLIhQ7f3mK] [https://onlinegdb.com/SyGzIRQ2b] Another live version [http://cpp.sh/7vx2v]
5th Oct 2017, 4:24 PM
Babak
Babak - avatar
+ 2
Sorry for requestioning, but i am Not sure if i get the the describtion correct. could you maybe describe a situation in which you would use it and how...?
4th Oct 2017, 5:44 PM
Kai
Kai - avatar
+ 2
lets say i have two hugh arrays "a" and "b" which are each updated through some function f_a and f_b, but the update from each element, e.g. in a through f_a, is independent of every other entry in a, but not from b. Since they are hugh i want the function to run the update on multiple threads(e.g. processor kernels?). since multiple threads read out b for the update of a i want to use the atomic header? but if so, how?
4th Oct 2017, 6:05 PM
Kai
Kai - avatar
+ 1
almost. let me put it in a class... https://code.sololearn.com/c6gUcUxw5EwL/?ref=app as you can See, for the update of a' s entry there is only a dependency on b and on the very same entry itself. so i think it could run parallel, but is the atomic somewhere needed? and if (not), why
4th Oct 2017, 8:41 PM
Kai
Kai - avatar