Skip to content

Instantly share code, notes, and snippets.

@1dolinski
Last active December 16, 2015 06:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save 1dolinski/5390679 to your computer and use it in GitHub Desktop.
Save 1dolinski/5390679 to your computer and use it in GitHub Desktop.
#performance

Now you know the fundamental requirement for thread-safe code: mutation of shared state must be done atomically. Any time you change a variable that is shared by many threads, it needs to be done atomically. Unfortunately Ruby and most other mainstream languages only give you one tool to do this: the lock aka the mutex.

Mutex is short for “mutual exclusion” as in “only one thread can be executing this code at a time”. Usage is simple:

@mutex = Mutex.new
@mutex.synchronize do
  i += 1
end

The main difference between Parallel Extentions and Regular Threading is the control flow.

Running Tasks in parallel does not actually alter the program flow. It will run different tasks on different threads so they can be executed on multiple CPU cores. They are not running in the "background" as they would be with a thread.

Why Multithreading is becoming important Whats the differnce between processes, threads and fibers How do you synchronize either of them

Not everythign can be made parallel and not everything that can, should.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment