<< Chapter < Page
  Ti dsp/bios lab   Page 1 / 1
Chapter >> Page >
The module is a lab assignment to help you better understand the very basics of Microsoft Visual Basic threads and semaphores.

Introduction

This lab module will help you become familiar with the basics of threads and semaphores in Microsoft Visual Basic(V). The exercises are written assuming you are using Visual Basic 2005 and the .NET Framework version 2.0 (or later). Some older versions will work the same, but you may need to figure out some differences. It should work with newer version also.

Module prerequisites

It would be helpful to have completed the TSK and SEM modules for the DSP/BIOS operating system but it is not necessary if you are familiar with tasks and semaphores.

Labratory

Part 1

  • In this part you will make a project that will have two threads that will run at the same priority and print to the console. Examine how the threads at the same priority execute in a round-robin fashion.
  • Start up Visual Studio and create a new Visual Basic project.
  • Create two threads and start them up with the same priority, Lowest.
  • In each thread make a continuous while loop and print the thread name in the loop. Your code can look like this.
While 1 Console.WriteLine("Thread1")End While

Part 2

  • In this part you will make a project with three threads. The first two threads will write to a single array. The data that each thread writes will be different. The third thread will print the contents of the array. Without locking the resource the threads will not write to the array completely before the other thread writes to it also. The contents of the array will be a mix of data from each thread.
  • Start up Visual Studio and create a new Visual Basic project.
  • Create three threads and start them up with the same priority, Lowest.
  • Make an integer array in the Form1 class. This should be just below the class statement.

Dim IntArray(100) As Integer

  • In the first thread, make an infinite loop and within the loop write the numbers 1-100 (count) into the array. Have the tread sleep for 10 ms each loop too.
Dim count As Integer While 1For count = 1 To 100 Thread.Sleep(10)IntArray(count) = count NextEnd While
  • In the second thread, make an infinite loop and within the loop write the numbers 100-count into the array. Have the tread sleep for 10 ms each loop too.
  • In the third thread, have it pause for ten seconds or so ( Thread.Sleep(10000) ) and then print the values from the array. Do this one time.
  • Run the program and observe whether the array values get written and printed in order.
  • You may not notice but the printing could get interrupted by the other threads.

Part 3

  • In this part you will add a semaphore to the previous project so that the data gets written to the array without interruption. Also, the data will get printed without interruption.
  • Start up Visual Studio and create a new Visual Basic project.
  • Copy all the code from the previous part into this new project.
  • To the code in the previous part, add a semaphore with a maximum value of 1 and an initial value of 1.
  • For the threads that write to the array, add a WaitOne() call before the for loop that writes to the array and a Release() call after the for loop.
  • For the thread that prints the array data, add a WaitOne() call before the printing of the array and a Release() call after the printing.
  • Run the program and observe whether the array values get printed in order.

Laboratory write-up

  • Print out your projects' code.
  • Explain what is happening in each part.

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