<< Chapter < Page Chapter >> Page >

Visual basic semaphores

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()

Writing to the console

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

Example

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

Questions & Answers

what are components of cells
ofosola Reply
twugzfisfjxxkvdsifgfuy7 it
Sami
58214993
Sami
what is a salt
John
the difference between male and female reproduction
John
what is computed
IBRAHIM Reply
what is biology
IBRAHIM
what is the full meaning of biology
IBRAHIM
what is biology
Jeneba
what is cell
Kuot
425844168
Sami
what is cytoplasm
Emmanuel Reply
structure of an animal cell
Arrey Reply
what happens when the eustachian tube is blocked
Puseletso Reply
what's atoms
Achol Reply
discuss how the following factors such as predation risk, competition and habitat structure influence animal's foraging behavior in essay form
Burnet Reply
cell?
Kuot
location of cervical vertebra
KENNEDY Reply
What are acid
Sheriff Reply
define biology infour way
Happiness Reply
What are types of cell
Nansoh Reply
how can I get this book
Gatyin Reply
what is lump
Chineye Reply
what is cell
Maluak Reply
what is biology
Maluak
what is vertibrate
Jeneba
what's cornea?
Majak Reply
what are cell
Achol
Got questions? Join the online conversation and get instant answers!
Jobilize.com Reply

Get Jobilize Job Search Mobile App in your pocket Now!

Get it on Google Play Download on the App Store Now




Source:  OpenStax, Ti dsp/bios lab. OpenStax CNX. Sep 03, 2013 Download for free at http://cnx.org/content/col11265/1.8
Google Play and the Google Play logo are trademarks of Google Inc.

Notification Switch

Would you like to follow the 'Ti dsp/bios lab' conversation and receive update notifications?

Ask