User Tools

Site Tools


concurrency

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revision Previous revision
Next revision
Previous revision
concurrency [2017/04/13 03:50]
barryfp
concurrency [2018/02/09 03:59]
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 30: Line 30:
  
 A function called with ''​asynch::''​ will return a value of type ''​Thread'',​ irrespective of the actual return value of the function. This Thread value can be used with synchronisation. A function called with ''​asynch::''​ will return a value of type ''​Thread'',​ irrespective of the actual return value of the function. This Thread value can be used with synchronisation.
 +
 +====== Mutual exclusion between multiple threads ======
 +
 +In some cases it is necessary to ensure that the logic within two functions (or in multiple instances of the same function) is not interleaved / executed concurrently.
 +
 +This can be done with the ''​mutex''​ control block which takes any Data type (or the Mutex type, which is a synonym of the Data base type) as a parameter to lock:
 +
 +<code d>
 +Mutex myLock = new Mutex()
 +
 +void functionA()
 +   {
 +   ​mutex(myLock)
 +      {
 +      //do something...
 +      }
 +   }
 +
 +void functionB()
 +   {
 +   ​mutex(myLock)
 +      {
 +      //do something else...
 +      }
 +   }
 +</​code>​
 +
 +In the above example, the code within ''​functionA()''​ and ''​functionB()''​ will never run at the same time.
  
 ====== Synchronisation between multiple threads ====== ====== Synchronisation between multiple threads ======
Line 37: Line 65:
 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 74:
 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 65: Line 93:
  
 The ''​wait''​ and ''​signal''​ operators are actually a simple semaphore which is initialised to zero; the ''​signal''​ operation increments this semaphore by one and the ''​wait''​ operation decrements it by one if it is greater than zero or else blocks the thread if it is zero. The ''​wait''​ and ''​signal''​ operators are actually a simple semaphore which is initialised to zero; the ''​signal''​ operation increments this semaphore by one and the ''​wait''​ operation decrements it by one if it is greater than zero or else blocks the thread if it is zero.
- 
-====== Mutual exclusion between multiple threads ====== 
- 
-In some cases it is necessary to ensure that the logic within two functions (or in multiple instances of the same function) is not interleaved / executed concurrently. 
- 
-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> 
-Mutex myLock = new Mutex() 
- 
-void functionA() 
-   { 
-   ​mutex(myLock) 
-      { 
-      //do something... 
-      } 
-   } 
- 
-void functionB() 
-   { 
-   ​mutex(myLock) 
-      { 
-      //do something else... 
-      } 
-   } 
-</​code>​ 
- 
-In the above example, the code within ''​functionA()''​ and ''​functionB()''​ will never run at the same time. 
concurrency.txt ยท Last modified: 2018/02/09 03:59 by barryfp