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
concurrency [2017/06/23 09:41]
barryfp
concurrency [2018/02/09 03:59]
barryfp
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 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 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. 
concurrency.txt ยท Last modified: 2018/02/09 03:59 by barryfp