Handmade Hero»Episode Guide
Memory Barriers and Semaphores
?
?

Keyboard Navigation

Global Keys

[, < / ], > Jump to previous / next episode
W, K, P / S, J, N Jump to previous / next marker
t / T Toggle theatre / SUPERtheatre mode
V Revert filter to original state Y Select link (requires manual Ctrl-c)

Menu toggling

q Quotes r References f Filter y Link c Credits

In-Menu Movement

a
w
s
d
h j k l


Quotes and References Menus

Enter Jump to timecode

Quotes, References and Credits Menus

o Open URL (in new tab)

Filter Menu

x, Space Toggle category and focus next
X, ShiftSpace Toggle category and focus previous
v Invert topics / media as per focus

Filter and Link Menus

z Toggle filter / linking mode

Credits Menu

Enter Open URL (in new tab)
0:00:35Recap/Review
0:00:35Recap/Review
0:00:35Recap/Review
0:02:45Yesterday's TODOs
0:02:45Yesterday's TODOs
0:02:45Yesterday's TODOs
0:04:00TODO 1: Ordering of writes
0:04:00TODO 1: Ordering of writes
0:04:00TODO 1: Ordering of writes
0:06:33CompletePastWritesBeforeFutureWrites
0:06:33CompletePastWritesBeforeFutureWrites
0:06:33CompletePastWritesBeforeFutureWrites
0:10:22Looking up memory fences
0:10:22Looking up memory fences
0:10:22Looking up memory fences
0:12:10Venturing into Visual Studio's include folder
0:12:10Venturing into Visual Studio's include folder
0:12:10Venturing into Visual Studio's include folder
0:13:20Putting in an actual CPU barrier
0:13:20Putting in an actual CPU barrier
0:13:20Putting in an actual CPU barrier
0:14:32Where do we go from here?
0:14:32Where do we go from here?
0:14:32Where do we go from here?
0:15:00Volatile introduction
0:15:00Volatile introduction
0:15:00Volatile introduction
0:18:43TODO 2: Interlocked writes
0:18:43TODO 2: Interlocked writes
0:18:43TODO 2: Interlocked writes
0:20:10Looking up InterlockedIncrement
0:20:10Looking up InterlockedIncrement
0:20:10Looking up InterlockedIncrement
0:21:37TODO 3: Already taken care of by volatile
0:21:37TODO 3: Already taken care of by volatile
0:21:37TODO 3: Already taken care of by volatile
0:21:54TODO 4: Ordering of reads
0:21:54TODO 4: Ordering of reads
0:21:54TODO 4: Ordering of reads
0:23:24Checking our work
0:23:24Checking our work
0:23:24Checking our work
0:24:20NB: Don't use all 16 hyperthreads!
0:24:20NB: Don't use all 16 hyperthreads!
0:24:20NB: Don't use all 16 hyperthreads!
0:25:45Job completion
0:25:45Job completion
0:25:45Job completion
0:28:40Waiting for all the threads to complete
0:28:40Waiting for all the threads to complete
0:28:40Waiting for all the threads to complete
0:30:50Suspending and resuming threads
0:30:50Suspending and resuming threads
0:30:50Suspending and resuming threads
0:34:51Semaphores
0:34:51Semaphores
0:34:51Semaphores
0:37:28WaitForSingleObject for suspending
0:37:28WaitForSingleObject for suspending
0:37:28WaitForSingleObject for suspending
0:40:35Creating the semaphore
0:40:35Creating the semaphore
0:40:35Creating the semaphore
0:44:24Problem: Threads never wake up!
0:44:24Problem: Threads never wake up!
0:44:24Problem: Threads never wake up!
0:46:50Waking up by releasing the semaphore
0:46:50Waking up by releasing the semaphore
0:46:50Waking up by releasing the semaphore
0:53:27How we'll use the semaphore
0:53:27How we'll use the semaphore
0:53:27How we'll use the semaphore
0:54:26Testing the semaphore
0:54:26Testing the semaphore
0:54:26Testing the semaphore
0:56:50Work for tomorrow
0:56:50Work for tomorrow
0:56:50Work for tomorrow
0:57:13Q&A
🗩
0:57:13Q&A
🗩
0:57:13Q&A
🗩
0:58:24d7samurai In the visual studio output window you can right click and deselect some of the stuff
🗪
0:58:24d7samurai In the visual studio output window you can right click and deselect some of the stuff
🗪
0:58:24d7samurai In the visual studio output window you can right click and deselect some of the stuff
🗪
0:58:56cubercaleb Why does this have to be so complicated??
🗪
0:58:56cubercaleb Why does this have to be so complicated??
🗪
0:58:56cubercaleb Why does this have to be so complicated??
🗪
0:59:37rc1290 Why did you put the memory barrier in a macro when it's platform-specific code?
🗪
0:59:37rc1290 Why did you put the memory barrier in a macro when it's platform-specific code?
🗪
0:59:37rc1290 Why did you put the memory barrier in a macro when it's platform-specific code?
🗪
1:00:07robrobby After the sleep, some threads still pushed several strings, leaving out some of the other threads
🗪
1:00:07robrobby After the sleep, some threads still pushed several strings, leaving out some of the other threads
🗪
1:00:07robrobby After the sleep, some threads still pushed several strings, leaving out some of the other threads
🗪
1:00:56plain_flavored Would Sleep(0) in your spin-lock help anything?
🗪
1:00:56plain_flavored Would Sleep(0) in your spin-lock help anything?
🗪
1:00:56plain_flavored Would Sleep(0) in your spin-lock help anything?
🗪
1:02:35abnercoimbre IMGUI has been trending. Sorry if you get asked this more than you like to.
🗪
1:02:35abnercoimbre IMGUI has been trending. Sorry if you get asked this more than you like to.
🗪
1:02:35abnercoimbre IMGUI has been trending. Sorry if you get asked this more than you like to.
🗪
1:03:07goodjerm When you initially started this project, what were the first 5 things you coded and why?
🗪
1:03:07goodjerm When you initially started this project, what were the first 5 things you coded and why?
🗪
1:03:07goodjerm When you initially started this project, what were the first 5 things you coded and why?
🗪
1:03:22ifingerbangedurcat I missed most of tonight, what does the volatile keyword mean?
🗪
1:03:22ifingerbangedurcat I missed most of tonight, what does the volatile keyword mean?
🗪
1:03:22ifingerbangedurcat I missed most of tonight, what does the volatile keyword mean?
🗪
1:03:47noxy_key How do you plan to maintain cache line coherency between processors? Can physical CPUs share a cache line?
🗪
1:03:47noxy_key How do you plan to maintain cache line coherency between processors? Can physical CPUs share a cache line?
🗪
1:03:47noxy_key How do you plan to maintain cache line coherency between processors? Can physical CPUs share a cache line?
🗪
1:05:03Blackboard: MESI and Cache Coherency
1:05:03Blackboard: MESI and Cache Coherency
1:05:03Blackboard: MESI and Cache Coherency
1:14:15gasto5 Was volatile added in C99?
🗪
1:14:15gasto5 Was volatile added in C99?
🗪
1:14:15gasto5 Was volatile added in C99?
🗪
1:14:37abnercoimbre Wait, so transactional memory wants to simplify concurrent programming by allowing a chunk of load/store instructions to execute in atomically. Have you messed with this?
🗪
1:14:37abnercoimbre Wait, so transactional memory wants to simplify concurrent programming by allowing a chunk of load/store instructions to execute in atomically. Have you messed with this?
🗪
1:14:37abnercoimbre Wait, so transactional memory wants to simplify concurrent programming by allowing a chunk of load/store instructions to execute in atomically. Have you messed with this?
🗪
1:15:05Blackboard: Transactional Memory
1:15:05Blackboard: Transactional Memory
1:15:05Blackboard: Transactional Memory
1:18:58abnercoimbre Transactional memory is often advocated as an easier-to-use replacement for locks that avoids any possibility of a deadlock, so I wanted your thoughts.
🗪
1:18:58abnercoimbre Transactional memory is often advocated as an easier-to-use replacement for locks that avoids any possibility of a deadlock, so I wanted your thoughts.
🗪
1:18:58abnercoimbre Transactional memory is often advocated as an easier-to-use replacement for locks that avoids any possibility of a deadlock, so I wanted your thoughts.
🗪
1:21:04plainflavored Why are we building a generic work distribution when the tiled renderer is designed to cleanly split up the work anyway?
🗪
1:21:04plainflavored Why are we building a generic work distribution when the tiled renderer is designed to cleanly split up the work anyway?
🗪
1:21:04plainflavored Why are we building a generic work distribution when the tiled renderer is designed to cleanly split up the work anyway?
🗪
1:22:45Wrapping things up
🗩
1:22:45Wrapping things up
🗩
1:22:45Wrapping things up
🗩

Memory Barriers and Semaphores

Semaphores:

A semaphore is essentially a number that the operating system keeps track of, that can be incremented and decremented. When you wait for a semaphore, you're essentially telling the OS to let you know when the semaphore number becomes greater than zero. Once it does, then the Wait() call will return and the thread can do something. Calling ReleaseSemaphore(), maybe a little counterintuitively, increments the semaphore, allowing any threads waiting on it to continue working. (Thus, it releases those threads to do work). It doesn't actually change the state of the semaphore other than making the number go up. The semaphore number goes down when a thread has successfully Wait()ed for the semaphore. In cases like the one demonstrated on stream, this usually means the semaphore number will go up/down really fast and stick close to 0 as most of the time the threads are waiting for the semaphore to increment.

What this allows you to do is tell several threads at once that some work is ready without having to signal to each one individually. As long as each one is waiting on the same semaphore object, they'll all know when there's more work to be done.