Semaphore

From Real Software Documentation

Jump to: navigation, search
Class (inherits from Object)

Used to control access to resources in a multithreaded environment.



Methods
Release TrySignal
Signal


Constructors

Semaphore.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, otherwise the thread will be forced to wait until the Semaphore is released via the Release method by the owning thread.

Every time you successfully obtain a lock on the resource, the Semaphore will decrement its internal count of available resources. When there are no more resources, threads that request locks will begin to block and wait for resources. This is why you are allowed to pass 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.

Examples

The example uses a Pushbutton in a window to spawn some threads. Its Action event code is:

// Make a new thread
Dim newThread as ThreadSpawn
newThread = New ThreadSpawn( Self, mNextID )
// Be sure to advance our thread id
mNextID = mNextID + 1
// And run the new thread
newThread.Run


The ThreadSpawn class is derived from the Thread class. Its constructor takes a Window and an integer as its parameters.

Sub Constructor (manager as Window1, ID as Integer)
mSharedResourceManger = manager
mThreadNumber = ID


mSharedResourceManager is a Window1 property of ThreadSpawn and mThreadNumber is an Integer property of ThreadSpawn.

Its Run event handler is:

// We're going to loop forever and ever
While True
// We want to try to access the shared
// resource, so we call it on the Window
mSharedResourceManager.ChangeSharedResource(mThreadNumber, Str(Rnd) )

// And then go to sleep for some random amount of time
Me.Sleep(Rnd * 10000, True )
Wend


When the window opens, the PushButton is pushed programmatically to create some threads. The window's Open event handler is:

mLock = New Semaphore

// We're going to spawn a few threads
// to start off with
For i as Integer = 0 to 4
PushButton1.Push
Next i


Window1 is derived from Window and has the ChangedSharedResource method:

// We want to make sure that only the right thread gets in here. One at a
// time!
//mLock is a Semaphore property of Window1
//mNextID is an Integer property of Window1
mLock.Signal

// Now that we're here, we can change the resource
//SharedResource and AccessText are StaticTexts in Window1
SharedResource.Text = value
AccessText.Text = Str( id )

// And now that we've done something with the
// resource, we want to release our lock on it
mLock.Release

See Also

CriticalSection, Mutex, Thread classes.

Personal tools