MCP-S11-03 - LECTURE 3 - BASIC SYNCHRONIZATION NYU...

Info iconThis preview shows pages 1–8. Sign up to view the full content.

View Full Document Right Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: LECTURE 3 - BASIC SYNCHRONIZATION NYU Multicore Programming Class, Spring 2011 lerner@cs.nyu.edu 1 OS HW Task-oriented application (* Lecture 2) Concurrent Objects (*) Tasks (*) and synchronization primitives (today) application concurrency support Context Were exploring the layers of an application running atop of multicores hardware. 2 NYU Multicore Programming, Spring 2011 A simple mutual exclusion algorithm Semaphores and blocking mutexes Condition Variables and the wakeup problem M u t e x S e m a p h o r e C o n d V Todays agenda NYU Multicore Programming, Spring 2011 3 V a r M u t e x The literature on mutual exclusion algorithms is extensive In practice, mutual exclusion is implemented with some form of hardware and OS support (and well see them in Lecture 5) But well first develop our algorithmic and correctness evaluation skills by looking at some very elegant solutions that use only shared memory Mutual exclusion algorithms will provide two methods, lock() and unlock(), that allow us to mark the beginning and the end of critical sections in out code. lock() will block the caller until there is no other thread in the critical section unlock() would allow other thread in the critical section if theres one waiting 4 NYU Multicore Programming, Spring 2011 M u t e x lock() int me = my_tid % 2 int other = 1 me flag[me] = true victim = me while (flag[other] && victim == me) no-op unlock() int me = my_tid % 2 flag[me] = false int victim bool flag[2] = { false, false } Petersons algorithm Mutual exclusion solution in three lines of code. 5 NYU Multicore Programming, Spring 2011 lock() int me = my_tid % 2 int other = 1 me flag[me] = true victim = me while (flag[other] && victim == me) no-op int victim = 0 bool flag[2] = { true , false } Thread 0 unlock() int me = my_tid % 2 flag[me] = false M u t e x Uncontested case Solved in two simple tests and no wait. 6 NYU Multicore Programming, Spring 2011 lock() int me = my_tid % 2 int other = 1 me flag[me] = true victim = me while (flag[other] && victim == me) no-op int victim = 1 bool flag[2] = { true, true } Thread 1 unlock() int me = my_tid % 2 flag[me] = false loop...
View Full Document

Page1 / 21

MCP-S11-03 - LECTURE 3 - BASIC SYNCHRONIZATION NYU...

This preview shows document pages 1 - 8. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online