CIS 720 Waitfree synchronization Wait free synchronization An

  • Slides: 7
Download presentation
CIS 720 Wait-free synchronization

CIS 720 Wait-free synchronization

Wait free synchronization • An operation is wait-free if the thread executing the operation

Wait free synchronization • An operation is wait-free if the thread executing the operation does not have to wait for any other thread to take a step.

Reader-writers problem • Reader i T 1: sflag = wflag • Writer wflag =

Reader-writers problem • Reader i T 1: sflag = wflag • Writer wflag = true; Read buffer; write buffer if sflag or wflag then go to T 1 wflag = false;

Reader-writers problem • Reader i T 1: sflag = wflag sswitch = switch; Read

Reader-writers problem • Reader i T 1: sflag = wflag sswitch = switch; Read buffer; if sflag or wflag or switch != sswitch then go to T 1 • Writer wflag = true; write buffer switch = not switch; wflag = false;

Reader-writers problem • Reader i T 1: reading[i] = not writing[i] T 2: sflag

Reader-writers problem • Reader i T 1: reading[i] = not writing[i] T 2: sflag = wflag sswitch = switch; Read buffer; if sflag or wflag or switch != sswitch then go to T 2; if reading[i] = writing[i] then go to T 1; • Writer wflag = true; write buffer switch = not switch; wflag = false; for (j=0; j < N; j++) if (reading[j] != writing[j]) then writing[j] = reading[j]

Reader-writers problem • Reader i T 1: reading[i] = not writing[i] T 2: sflag

Reader-writers problem • Reader i T 1: reading[i] = not writing[i] T 2: sflag = wflag sswitch = switch; Read buffer; if sflag or wflag or switch != sswitch then go to T 2; if reading[i] = writing[i] then go to T 1; • Writer wflag = true; write buffer switch = not switch; for (j=0; j < N; j++) if (reading[j] != writing[j]) then writing[j] = reading[j] wflag = false;

Reader-writers problem • Reader i T 1: reading[i] = not writing[i] T 2: sflag

Reader-writers problem • Reader i T 1: reading[i] = not writing[i] T 2: sflag = wflag sswitch = switch; read buff 1; sflag 2 = wflag; sswitch 2 = switch; read buff 2; bad 1 = (sflag or sflag 2) or sswitch != sswitch 2 if reading[i] = writing[i] then return copybuff[i]; elseif bad 1 then return buff 2 else return buff 1; • Writer wflag = true; write buff 1 switch = not switch; for (j=0; j < N; j++) if (reading[j] != writing[j]) then { write copybuff[j]; writing[j] = reading[j]; } wflag = false write buff 2;