This should do the job:<br><br><span style="font-family: courier new,monospace;">import std.stdio, std.perf, core.atomic;<br><br>// Cut and pasted from ParallelFuture.<br>void atomicIncUint(ref uint num) {<br> asm {<br>
mov EAX, num;<br> lock;<br> inc int ptr [EAX];<br> mov EAX, [EAX];<br> }<br>}<br><br>void main() {<br> auto pc = new PerformanceCounter;<br> pc.start;<br><br> shared uint num = 0;<br>
while(num < 100_000_000) {<br> atomicIncUint(num);<br> }<br><br> pc.stop;<br> writeln(pc.milliseconds); // 1772 ms<br><br> pc.start;<br><br> num = 0;<br> while(num < 100_000_000) {<br> atomicOp!"+="(num, 1U); // 2539 ms<br>
}<br><br> pc.stop;<br> writeln(pc.milliseconds);<br>}<br><br></span>So the difference is there, but it's not huge. In addition, the lack of anything like <font face="courier new,monospace">atomicOp!"++"(num); <font face="arial,helvetica,sans-serif">seems kind of weird.</font></font><br>
<br>Also, atomicOp shouldn't be so picky about types. For example, atomically adding an int to a uint should work.<br><br><div class="gmail_quote">On Fri, Aug 27, 2010 at 11:43 AM, Andrei Alexandrescu <span dir="ltr"><<a href="mailto:andrei@erdani.com">andrei@erdani.com</a>></span> wrote:<br>
<blockquote class="gmail_quote" style="margin: 0pt 0pt 0pt 0.8ex; border-left: 1px solid rgb(204, 204, 204); padding-left: 1ex;">This should be validated by benchmarking.<br>
<br>
Andrei<div class="im"><br>
<br>
On 8/27/10 7:45 PDT, David Simcha wrote:<br>
</div><blockquote class="gmail_quote" style="margin: 0pt 0pt 0pt 0.8ex; border-left: 1px solid rgb(204, 204, 204); padding-left: 1ex;"><div class="im">
IIRC (maybe this has changed recently) atomic increments in core.atomic<br>
are based on CAS instructions in a while loop, which is how more generic<br>
lock free primitives are made. Atomic increment should be special cased<br>
to directly use lock; inc [someRegister];.<br>
<br>
On Fri, Aug 27, 2010 at 10:40 AM, Sean Kelly <<a href="mailto:sean@invisibleduck.org" target="_blank">sean@invisibleduck.org</a><br></div><div class="im">
<mailto:<a href="mailto:sean@invisibleduck.org" target="_blank">sean@invisibleduck.org</a>>> wrote:<br>
<br>
On Aug 27, 2010, at 7:11 AM, David Simcha wrote:<br>
><br>
>><br>
>> I see you have some CAS instructions. Sean, I think it's a good<br>
time to collaborate with David to put them into druntime or<br>
std.concurrency.<br>
><br>
> Yeah, D needs a real atomics library. core.atomic is a good<br>
start, but I won't use it until it can efficiently do things like<br>
atomic increment.<br>
<br>
How could it be made more efficient?<br>
_______________________________________________<br>
phobos mailing list<br></div>
<a href="mailto:phobos@puremagic.com" target="_blank">phobos@puremagic.com</a> <mailto:<a href="mailto:phobos@puremagic.com" target="_blank">phobos@puremagic.com</a>><div class="im"><br>
<a href="http://lists.puremagic.com/mailman/listinfo/phobos" target="_blank">http://lists.puremagic.com/mailman/listinfo/phobos</a><br>
<br>
<br>
<br>
<br>
_______________________________________________<br>
phobos mailing list<br>
<a href="mailto:phobos@puremagic.com" target="_blank">phobos@puremagic.com</a><br>
<a href="http://lists.puremagic.com/mailman/listinfo/phobos" target="_blank">http://lists.puremagic.com/mailman/listinfo/phobos</a><br>
</div></blockquote><div><div></div><div class="h5">
_______________________________________________<br>
phobos mailing list<br>
<a href="mailto:phobos@puremagic.com" target="_blank">phobos@puremagic.com</a><br>
<a href="http://lists.puremagic.com/mailman/listinfo/phobos" target="_blank">http://lists.puremagic.com/mailman/listinfo/phobos</a><br>
</div></div></blockquote></div><br>