110 likes | 227 Views
IO Concurrency. Kurt Niemi CPSC 5135 Spring Semester 2011. Main Components. These are the major components in IO that provide a rich concurrency mechanism. Coroutines Actors Futures Scheduler. Coroutines. Coroutines are the foundation for concurrency within IO.
E N D
IO Concurrency Kurt Niemi CPSC 5135 Spring Semester 2011
Main Components These are the major components in IO that provide a rich concurrency mechanism. • Coroutines • Actors • Futures • Scheduler
Coroutines • Coroutinesare the foundation for concurrency within IO. • This is the mechanism in IO to allow for a process to voluntarily suspend and resume execution. • Coroutines can conceptually be thought of as a function that has multiple entry points • Calling yield within a coroutine suspends a process, allowing another one to execute
Coroutines • Note: IO multitasking is different than the traditional pre-emptive multitasking found in Java and C-based languages. • In Java/C, the execution of a method may be interrupted at any point in time – requiring extra care to make code thread safe if multiple threads are accessing are manipulating the same object. • In IO, it is up to the programmer to call yield (at convenient/appropriate points). • Remember with greater power, comes greater responsibility.
Actors • Actors are a mechanism of IO to avoid race conditions. • Actors can only change their own state • Actors can only access other actors via internal queues in IO. • Sending an asynchronous message to any object makes it an actor. • Adding @@ in front of a message is the IO syntax to make a message asynchronous.
Futures • A future is a result object that is immediately returned from an asynchronous message call • This can be thought of as a placeholder for the final result. • If the value of a future is attempted to be accessed before the result is available, IO automatically blocks the current process until the result is available. • Futures also provide for automatic deadlock detection
Futures • Messages can be fired asynchronously using both @ and @@ • Using @ returns a Future. • Using @@ returns nil and starts the message in it’s own thread.
Scheduler • IO has a Scheduler object that is responsible for resuming coroutines that have yielded execution. • The Scheduler, like the IO language, is simple. A FIFO (First in-First Out) policy with no priorities is used.
Putting it All Together • OK, enough talk. Show me the code. Let’s walk through an example application showing IO’s concurrency features
Summary • IO has some very powerful concurrency features, and like the language itself it is simple to use. • Issues such as Race Conditions and Deadlock are automatically handled by the Actor and Futures in IO • IO multitasking/threading is different than Java / C-based languages. The application code determines when it is ready to yield to another thread.
Resources • IO language documentation http://www.iolanguage.com/scm/io/docs/IoGuide.html • 7 languages in 7 weeks (IO section – Day 3) http://www.pragprog.com/titles/btlang/seven-languages-in-seven-weeks