This shows you the differences between two versions of the page.
Both sides previous revision Previous revision | Last revision Both sides next revision | ||
concurrency [2017/04/13 03:50] barryfp |
concurrency [2017/06/23 09:41] barryfp |
||
---|---|---|---|
Line 5: | Line 5: | ||
Imagine a function: | Imagine a function: | ||
- | <code dana> | + | <code d> |
void printNumbers(int to) | void printNumbers(int to) | ||
{ | { | ||
Line 17: | Line 17: | ||
This prints out all of the numbers from 0 up to and including the given parameter. We can call this function synchronously by using: | This prints out all of the numbers from 0 up to and including the given parameter. We can call this function synchronously by using: | ||
- | <code dana> | + | <code d> |
printNumbers(100) | printNumbers(100) | ||
</code> | </code> | ||
Line 23: | Line 23: | ||
In this case the caller is blocked until all numbers have been printed. Alternatively we can call this function asynchronously using: | In this case the caller is blocked until all numbers have been printed. Alternatively we can call this function asynchronously using: | ||
- | <code dana> | + | <code d> |
asynch::printNumbers(100) | asynch::printNumbers(100) | ||
</code> | </code> | ||
Line 37: | Line 37: | ||
The ''join'' operator is simply used to wait for another thread to finish executing: | The ''join'' operator is simply used to wait for another thread to finish executing: | ||
- | <code dana> | + | <code d> |
Thread t = asynch::printNumbers(100) | Thread t = asynch::printNumbers(100) | ||
t.join() | t.join() | ||
Line 46: | Line 46: | ||
The ''wait'' and ''signal'' operators allow for more controlled interaction between two threads. They are typically used when one thread needs to wait for a specific event to occur, where that event will arrive in a different thread: | The ''wait'' and ''signal'' operators allow for more controlled interaction between two threads. They are typically used when one thread needs to wait for a specific event to occur, where that event will arrive in a different thread: | ||
- | <code dana> | + | <code d> |
Thread main; | Thread main; | ||
Line 72: | Line 72: | ||
This can be done with the ''mutex'' control block which takes any Data type (or the Mutex type, which is a subtype of Data) as a parameter to lock: | This can be done with the ''mutex'' control block which takes any Data type (or the Mutex type, which is a subtype of Data) as a parameter to lock: | ||
- | <code dana> | + | <code d> |
Mutex myLock = new Mutex() | Mutex myLock = new Mutex() | ||