Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.programming.threads > #2173 > unrolled thread
| Started by | aminer <aminer@toto.net> |
|---|---|
| First post | 2014-04-14 20:12 -0700 |
| Last post | 2014-04-14 20:12 -0700 |
| Articles | 1 — 1 participant |
Back to article view | Back to comp.programming.threads
Weakness of lockfree loops ... aminer <aminer@toto.net> - 2014-04-14 20:12 -0700
| From | aminer <aminer@toto.net> |
|---|---|
| Date | 2014-04-14 20:12 -0700 |
| Subject | Weakness of lockfree loops ... |
| Message-ID | <lihth0$2bl$1@news.albasani.net> |
Hello,
We have to be smart more than that...
And we have to be carefull when using CAS based loops when
designing a Threadpool with a single concurrent FIFO Queue..
To understand why let's look at the following pop() function:
===
function TFIFOQUEUE_MPMC.pop(var obj:tNodeQueue):boolean;
var lastHead : long;
begin
repeat
lastHead:=head;
if tail<>head
then
begin
obj:=getObject(lastHead);
if CAS(head,lasthead,lasthead+1)
then
begin
result:=true;
exit;
end
else asm pause end;
end
else
begin
result:=false;
exit;
end;
until false;
end;
===
imagine that we have 4 threads under contention on a Quadcore processor
and the four threads are crossing at the same time the "lastHead:=head;"
so if the CAS has succeeded for one thread the other will fail and will
they will incur one more cache-line transfers for the "head" variable,
so this will make the threads that have rolled back the lockfree loop
loose there time in other cache-lines transfers, so this is not good for
the Threadpool cause the load has to be more balanced, so this can lower
the overall performance of the Threadpool under contention.
So i don't advice you to use lockfree CAS loops on the pop() side
of the queue when using them with Threadpools that uses a single
concurrent FIFO queue.
Hope you have understood this...
Thank you,
Amine Moulay Ramdane.
Back to top | Article view | comp.programming.threads
csiph-web