<< Chapter < Page | Chapter >> Page > |
Semaphores in VB are used to control access to a resource. They act as a counting semaphore except that there is a maximum value that the semaphore can have. This value is set when the semaphore is declared. The following is an example of the definition of a semaphore.
Dim Sem As New Semaphore(1, 2)
This defines a semaphore object named
Sem
that has a maximum value of 2 and an initial value of 1. The main methods for using the semaphore are
WaitOne()
and
Release()
. The
WaitOne()
method decrements the semaphore and the
Release()
method increases it. If the value of the semaphore is 0 when the
WaitOne()
method is called the thread will block. When the
Release()
method is called when a thread is blocked on the semaphore, it will unblock a thread without incrementing the semaphore. If the
Release()
method is called too any times and the semaphore value is increased past the maximum value, a
SemaphoreFullException
exception will occur. This module does not discuss how to handle the exception.
The main way to use the semaphore is shown in the following example.
Dim Sem As New Semaphore(1, 1)Sem.WaitOne()
' Use the protected resource hereSem.Release()
To write to the console use function calls such as
Console.WriteLine("Name = {0}, hours = {1:hh}", My.Name, DateTime.Now)
The fixed text is
"Name = "
and
", hours = "
. The format items are
"{0}"
, whose index is 0, which corresponds to the object
My.Name
, and
"{1:T}"
, whose index is 1, which corresponds to the object
DateTime.Now
. The format
T
will print the time in the long time format. An example of the output is:
Name = Form1, Time = 5:50:54 PM
This simple example uses Visual Studio 2010 and VB.NET Framework 4. Create a Form Application and enter the following code:
Imports System.Threading
Public Class Form1Dim thread1 As New Thread(AddressOf Task)Dim thread2 As New Thread(AddressOf Task)
Dim Sem As New Semaphore(1, 1)' When the form program starts or is loaded the following function is' called. This is where you can put the commands to set up our program.
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load' Make this a background thread so it automatically' aborts when the main program stops.
thread1.IsBackground = True' Set the thread priority to lowest
thread1.Priority = ThreadPriority.Lowest' Start the thread
thread1.Start("thread1")' Make this a background thread so it automatically' aborts when the main program stops.
thread2.IsBackground = True' Set the thread priority to lowest
thread2.Priority = ThreadPriority.Lowest' Start the thread
thread2.Start("thread2")End SubPrivate Sub Task(ByVal name As Object)While 1' wait on the semaphore
Sem.WaitOne()' Print the name of this thread and the current time
Console.WriteLine("Name = {0}, Time1 = {1:T}", name, DateTime.Now)' Sleep to 2 seconds
Thread.Sleep(2000)Console.WriteLine("Name = {0}, Time2 = {1:T}", name, DateTime.Now)
' Release the semaphoreSem.Release()End WhileEnd SubEnd Class
This program will start up two threads,
thread1
and
thread2
. Each thread will execute the method
Task()
and will pass the name of the thread. This will allow each thread to print something different even though they execute the same method. A semaphore is use to prevent the other thread from interrupting the two print statements. The two print statements for the thread should always print after each other. The following shows the results of the run. Note that the two times always print together.
Name = thread1, Time1 = 6:17:16 PM
Name = thread1, Time2 = 6:17:18 PMName = thread1, Time1 = 6:17:18 PM
Name = thread1, Time2 = 6:17:20 PMName = thread1, Time1 = 6:17:20 PM
Name = thread1, Time2 = 6:17:22 PMName = thread2, Time1 = 6:17:22 PM
Name = thread2, Time2 = 6:17:24 PMName = thread2, Time1 = 6:17:24 PM
Name = thread2, Time2 = 6:17:26 PMName = thread2, Time1 = 6:17:26 PM
Name = thread2, Time2 = 6:17:28 PM
The following shows the loop with the semaphore statements removed.
While 1
' Print the name of this thread and the current timeConsole.WriteLine("Name = {0}, Time1 = {1:T}", name, DateTime.Now)
' Sleep to 2 secondsThread.Sleep(2000)
Console.WriteLine("Name = {0}, Time2 = {1:T}", name, DateTime.Now)End While
This causes a printout like the following. Notice the two prints do not occur next to each other.
Name = thread2, Time1 = 6:20:44 PM
Name = thread1, Time1 = 6:20:44 PMName = thread2, Time2 = 6:20:46 PM
Name = thread1, Time2 = 6:20:46 PMName = thread1, Time1 = 6:20:46 PM
Name = thread2, Time1 = 6:20:46 PMName = thread2, Time2 = 6:20:48 PM
Name = thread1, Time2 = 6:20:48 PMName = thread1, Time1 = 6:20:48 PM
Name = thread2, Time1 = 6:20:48 PMName = thread2, Time2 = 6:20:50 PM
Name = thread2, Time1 = 6:20:50 PMName = thread1, Time2 = 6:20:50 PM
Name = thread1, Time1 = 6:20:50 PM
Notification Switch
Would you like to follow the 'Ti dsp/bios lab' conversation and receive update notifications?