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

how to create a software using Android phone
Wiseman Reply
how
basra
what is the difference between C and C++.
Yan Reply
what is software
Sami Reply
software is a instructions like programs
Shambhu
what is the difference between C and C++.
Yan
yes, how?
Hayder
what is software engineering
Ahmad
software engineering is a the branch of computer science deals with the design,development, testing and maintenance of software applications.
Hayder
who is best bw software engineering and cyber security
Ahmad
Both software engineering and cybersecurity offer exciting career prospects, but your choice ultimately depends on your interests and skills. If you enjoy problem-solving, programming, and designing software syste
Hayder
what's software processes
Ntege Reply
I haven't started reading yet. by device (hardware) or for improving design Lol? Here. Requirement, Design, Implementation, Verification, Maintenance.
Vernon
I can give you a more valid answer by 5:00 By the way gm.
Vernon
it is all about designing,developing, testing, implementing and maintaining of software systems.
Ehenew
hello assalamualaikum
Sami
My name M Sami I m 2nd year student
Sami
what is the specific IDE for flutter programs?
Mwami Reply
jegudgdtgd my Name my Name is M and I have been talking about iey my papa john's university of washington post I tagged I will be in
Mwaqas Reply
yes
usman
how disign photo
atul Reply
hlo
Navya
hi
Michael
yes
Subhan
Show the necessary steps with description in resource monitoring process (CPU,memory,disk and network)
samuel Reply
What is software engineering
Tafadzwa Reply
Software engineering is a branch of computer science directed to writing programs to develop Softwares that can drive or enable the functionality of some hardwares like phone , automobile and others
kelvin
if any requirement engineer is gathering requirements from client and after getting he/she Analyze them this process is called
Alqa Reply
The following text is encoded in base 64. Ik5ldmVyIHRydXN0IGEgY29tcHV0ZXIgeW91IGNhbid0IHRocm93IG91dCBhIHdpbmRvdyIgLSBTdGV2ZSBXb3puaWFr Decode it, and paste the decoded text here
Julian Reply
what to do you mean
Vincent
hello
ALI
how are you ?
ALI
What is the command to list the contents of a directory in Unix and Unix-like operating systems
George Reply
how can i make my own software free of cost
Faizan Reply
like how
usman
hi
Hayder
The name of the author of our software engineering book is Ian Sommerville.
Doha Reply
what is software
Sampson Reply
the set of intruction given to the computer to perform a task
Noor
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