English subtitles

← Synchronization - Barrier - Intro to Parallel Programming

Get Embed Code
3 Languages

Showing Revision 4 created 05/24/2016 by Udacity Robot.

  1. So now we know that threads can access each others' results
  2. through shared and global memory.
  3. This means they can work together on a computation, but there's a problem.
  4. What if a thread tries to read a result before another thread
  5. has had a chance to compute or write it?
  6. This means we need synchronization.
  7. Threads need to synchronize with each other to avoid this situation.
  8. This need for synchronization
  9. is really one of the most fundamental problems in parallel computing.
  10. Now the simplest form of synchronization is called a barrier.
  11. A barrier is a point in the program
  12. where all the threads stop and wait.
  13. When all the threads have reached the barrier,
  14. they can proceed on to the rest of the code.
  15. Let's illustrate this.
  16. Here's some threads and they're all proceeding along through to code.
  17. I'll draw them in different colors,
  18. and I'm also drawing them different lengths
  19. so that you get the idea that they're at different places in the code.
  20. They're at different points in their execution of the program.
  21. The idea is that when they reach the barrier,
  22. they're going to stop and wait for all the others to catch up.
  23. So in my drawing, the red one reaches the barrier first and stops.
  24. In the meantime, the blue one is proceeding along, and the green one is proceeding along,
  25. and eventually the blue one arrives at the barrier
  26. and stops,
  27. and the green one is the last one to arrive at the barrier and stops.
  28. And now all 3 threads--in my example, say I only have 3 threads.
  29. Now that all of the threads have arrived at the barrier, then they're all free to go again.
  30. And so they'll all proceed again, and we don't actually know which one's going to go first.
  31. It might be that the blue one is the first out of the gate,
  32. maybe green is next, maybe red is last.
  33. So let's look at some code to illustrate this.