Difference between revisions of "Semaphore"

From Xojo Documentation

(Notes)
(Examples)
 
Line 27: Line 27:
 
The Semaphore class is different from the [[CriticalSection]] and [[Mutex]] classes in this way: calling Signal in the same thread will cause the counter to decrement. If you call Signal recursively, you will cause the application to hang.
 
The Semaphore class is different from the [[CriticalSection]] and [[Mutex]] classes in this way: calling Signal in the same thread will cause the counter to decrement. If you call Signal recursively, you will cause the application to hang.
  
==Examples==
+
== Sample Code ==
 
 
 
This example uses a Semaphore to ensure that only one thread at a time writes to a file.
 
This example uses a Semaphore to ensure that only one thread at a time writes to a file.
  
 
Add a public property to the App class:
 
Add a public property to the App class:
 
 
<rbcode>
 
<rbcode>
 
FileAccess As Semaphore
 
FileAccess As Semaphore
Line 38: Line 36:
  
 
And in the Open event handler, initialize it:
 
And in the Open event handler, initialize it:
 
 
<rbcode>
 
<rbcode>
 +
// Allow only a single thread to access the file at a time
 
FileAccess = New Semaphore(1)
 
FileAccess = New Semaphore(1)
 
</rbcode>
 
</rbcode>
  
 
In the thread, check the FileAccess semaphore to see if you can write to the file:
 
In the thread, check the FileAccess semaphore to see if you can write to the file:
 
 
<rbcode>
 
<rbcode>
Dim accessingFile As Boolean
+
// This will Suspend this thread if another has the lock
 
+
// When it is released, this thread will continue to run
Do
+
App.FileAccess.Signal
If App.FileAccess.TrySignal Then
 
  accessingFile = True
 
Else
 
  // File is locked by another thread so
 
  // Sleep and try again
 
  Self.Sleep(1000)
 
End If
 
Loop Until accessingFile
 
  
 
// Now you can open the file for writing
 
// Now you can open the file for writing
WriteDataToFile // Supply your own code here
+
WriteDataToFile // Call your code here
  
 
// When you are finished, release the semaphore
 
// When you are finished, release the semaphore

Latest revision as of 14:41, 1 October 2018

Class (inherits from Object)

Used to control access to resources in a multithreaded environment.

Methods
Release TrySignal
Signal


Constructors

Constructor(NumResources as Integer)


Notes

A Semaphore is an object that can be used to coordinate access to a shared resource.

To acquire the ownership of a semaphore, a thread calls the Signal or TrySignal methods. If the Semaphore isn't owned, the thread acquires ownership, which means you have a lock on it. Otherwise the thread is forced to wait until the Semaphore is released via the Release method by the owning thread.

Every time you successfully obtain an ownership lock on the resource, the Semaphore will decrement its internal count of available resources. When there are no more resources, threads that request ownership locks will begin to block and wait for resources. This is why you can specify the initial count of resources -- to give you more control over the behavior of the Semaphore.

The Semaphore class is different from the CriticalSection and Mutex classes in this way: calling Signal in the same thread will cause the counter to decrement. If you call Signal recursively, you will cause the application to hang.

Sample Code

This example uses a Semaphore to ensure that only one thread at a time writes to a file.

Add a public property to the App class:

FileAccess As Semaphore

And in the Open event handler, initialize it:

// Allow only a single thread to access the file at a time
FileAccess = New Semaphore(1)

In the thread, check the FileAccess semaphore to see if you can write to the file:

// This will Suspend this thread if another has the lock
// When it is released, this thread will continue to run
App.FileAccess.Signal

// Now you can open the file for writing
WriteDataToFile // Call your code here

// When you are finished, release the semaphore
App.FileAccess.Release

See Also

CriticalSection, Mutex, Thread classes.