Skip to content

Instantly share code, notes, and snippets.

@exelotl
Created August 19, 2015 22:02
Show Gist options
  • Save exelotl/364c23ff21c1ee000dee to your computer and use it in GitHub Desktop.
Save exelotl/364c23ff21c1ee000dee to your computer and use it in GitHub Desktop.
import structs/ArrayList
import threading/Thread
counter := 0
mutex := Mutex new()
threads := ArrayList<Thread> new()
main: func {
for (i in 0..10) {
threads add(Thread new(||
for (i in 0..1000) {
mutex lock()
counter += 1
mutex unlock()
Thread yield()
}
))
}
for (t in threads) t start()
for (t in threads) t wait()
// prints counter = ???
"counter = %d" printfln(counter)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment