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


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

Is there any normative that regulates the use of silver nanoparticles?
Damian Reply
what king of growth are you checking .?
What fields keep nano created devices from performing or assimulating ? Magnetic fields ? Are do they assimilate ?
Stoney Reply
why we need to study biomolecules, molecular biology in nanotechnology?
Adin Reply
yes I'm doing my masters in nanotechnology, we are being studying all these domains as well..
what school?
biomolecules are e building blocks of every organics and inorganic materials.
anyone know any internet site where one can find nanotechnology papers?
Damian Reply
sciencedirect big data base
Introduction about quantum dots in nanotechnology
Praveena Reply
what does nano mean?
Anassong Reply
nano basically means 10^(-9). nanometer is a unit to measure length.
do you think it's worthwhile in the long term to study the effects and possibilities of nanotechnology on viral treatment?
Damian Reply
absolutely yes
how to know photocatalytic properties of tio2 nanoparticles...what to do now
Akash Reply
it is a goid question and i want to know the answer as well
characteristics of micro business
for teaching engĺish at school how nano technology help us
Do somebody tell me a best nano engineering book for beginners?
s. Reply
there is no specific books for beginners but there is book called principle of nanotechnology
what is fullerene does it is used to make bukky balls
Devang Reply
are you nano engineer ?
fullerene is a bucky ball aka Carbon 60 molecule. It was name by the architect Fuller. He design the geodesic dome. it resembles a soccer ball.
what is the actual application of fullerenes nowadays?
That is a great question Damian. best way to answer that question is to Google it. there are hundreds of applications for buck minister fullerenes, from medical to aerospace. you can also find plenty of research papers that will give you great detail on the potential applications of fullerenes.
what is the Synthesis, properties,and applications of carbon nano chemistry
Abhijith Reply
Mostly, they use nano carbon for electronics and for materials to be strengthened.
is Bucky paper clear?
carbon nanotubes has various application in fuel cells membrane, current research on cancer drug,and in electronics MEMS and NEMS etc
so some one know about replacing silicon atom with phosphorous in semiconductors device?
s. Reply
Yeah, it is a pain to say the least. You basically have to heat the substarte up to around 1000 degrees celcius then pass phosphene gas over top of it, which is explosive and toxic by the way, under very low pressure.
Do you know which machine is used to that process?
how to fabricate graphene ink ?
for screen printed electrodes ?
What is lattice structure?
s. Reply
of graphene you mean?
or in general
in general
Graphene has a hexagonal structure
On having this app for quite a bit time, Haven't realised there's a chat room in it.
what is biological synthesis of nanoparticles
Sanket Reply
how did you get the value of 2000N.What calculations are needed to arrive at it
Smarajit Reply
Privacy Information Security Software Version 1.1a
Got questions? Join the online conversation and get instant answers!
Jobilize.com Reply

Get the best Algebra and trigonometry course in your pocket!

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?