473,402 Members | 2,064 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,402 software developers and data experts.

A Question about C++11 relaxed atomics

152 100+
Hello there...

Let's say i have some threads that want to give each other some non-zero values. Repeatedly in turns; that is, thread A gives thread B some non-zero value and then A responds with some non-zero value as well and this continues indefenitely. (but with more threads in chain). I use a variant of the static tree barrier for this purpose, but instead of a boolean sense value (where thread spin waiting for it to change), i use an atomic<int> initialized to zero and the thread waits until it gets a nonzero value. I use a fan-in and fan-out of 2. In particular i use the barrier to perform a min-reduction (the values exchanged are the minimum so far). Here is some code:

Expand|Select|Wrap|Line Numbers
  1. struct node{
  2.     atomic<int> sense{0};
  3.     atomic<int> left_child_sense{0};
  4.     atomic<int> right_child_sense{0};
  5. };
  6.  
  7. node nodes[1...n];
  8.  
  9. void barrier_like_stuff(int my_value){
  10.     node& my_node = nodes[my_id()];
  11.  
  12.     // wait until i get the values from my 2 children
  13.     int t1, t2;
  14.  
  15.     while ((t1 = my_node.left_child_sense.load(relaxed)) == 0){}
  16.     while ((t2 = my_node.right_child_sense.load(relaxed)) == 0){}
  17.  
  18.     // so the minimum is:
  19.     int local_minimum = min(my_value, t1, t2);
  20.  
  21.     // and re-initialize for the next time
  22.     my_node.left_child_sense.store(0, relaxed);
  23.     my_node.right_child_sense.store(0, relaxed);
  24.  
  25.     // notify parent (if we have) (WHICH is either left or right)
  26.     nodes[my_parent()].WHICH_child_sense.store(local_minimum, relaxed);
  27.  
  28.     // wait until my parent (if we have) passes me the final minimum value
  29.     while ((local_minimum = my_node.sense.load(relaxed)) == 0){}
  30.  
  31.     // re-initialize for next time
  32.     my_node.sense.store(0, relaxed);
  33.  
  34.     // and pass this minimum value to my children
  35.     nodes[my_left_child].sense.store(local_minimum, relaxed);
  36.     nodes[my_right_child].sense.store(local_minimum, relaxed);
  37. }
  38.  
This code is correct with relaxed atomics right? Because when i read a non-zero value with load(relaxed) i do not have another requirement for memory visibility from the other thread that passes my that value... (or i am wrong?). Another way to see it is that the modification order for each sense value individual does the right thing. Am i correct? Thanks for your time!
May 16 '14 #1
0 1035

Sign in to post your reply or Sign up for a free account.

Similar topics

8
by: Tim Rentsch | last post by:
Here's another question related to 'volatile'. Consider the following: int x; void foo(){ int y; y = (volatile int) x;
15
by: luke.yolanda | last post by:
hi everyone I have a question. First, I have implemented a huge matrix in a program, like 'unsigned char matrix;', when I executed this program, the error occured. Then I modified the ...
2
by: Ghada Al-Mashaqbeh via DotNetMonster.com | last post by:
Hi all, I am facing a problem in dynamic code generation at run time, the problem occurs when the dynmaic code use global data exist within the original application. Lets say that my...
90
by: John Salerno | last post by:
I'm a little confused. Why doesn't s evaluate to True in the first part, but it does in the second? Is the first statement something different? False print 'hi' hi Thanks.
10
by: chrisben | last post by:
Hi, Here is the scenario. I have a list of IDs and there are multiple threads trying to add/remove/read from this list. I can do in C# 1. create Hashtable hList = Hashtable.Synchronized(new...
2
by: Andrew Meador - ASCPA, MCSE, MCP+I, Network+, A+ | last post by:
I have Visual Studio 2003 Enterprise Architect Edition. I have been away from the programming area for a while - since before Studio 2005 came out. I am financially tight and am wondering if going...
32
by: =?gb2312?B?zfWzrLey?= | last post by:
Union un { int I; char c; } main() { union un x; x.c=10; x.c=1;
1
by: Morgan Cheng | last post by:
Asynchronous method model generally has a callback delegate as argument. The delegate will be called when some WaitHandle is set or time out. And, one more argument is used as state object to...
75
by: Masood | last post by:
Hi all, I've been reading this group new for a few weeks and it's quite intriguing the way it's so dysfunctional as a "society". Actually I was telling my brother about it - he's a sociology...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.