To register for an Internet.com membership to receive newsletters and white papers, use the Register button ABOVE.
To participate in the message forums BELOW, click here
VBForums  

VB Wire News
Part 10 of the Visual Basic .NET 2010 Express Tutorial Complete!
How to Use the Visual Studio Code Analysis Tool FxCop
Article :: Interview with Andrei Alexandrescu (Part 3 of 3)
Introducing Visual Studio LightSwitch
Visual Studio LightSwitch Beta 1 is Available



Go Back   VBForums > Visual Basic > Visual Basic .NET

Reply Post New Thread
 
Thread Tools Display Modes
Old Mar 11th, 2007, 06:34 PM   #1
GottaGetITDone
Lively Member
 
Join Date: Mar 06
Posts: 65
GottaGetITDone is on a distinguished road (10+)
[RESOLVED] [2005] Threading and Synchronization

Top of the morning to you all!

I am just having a little trouble understanding thread synchronization. Allow me to demonstrate.

I have a class that exposes 3 methods like so:
Code:
     Class a

        Private x As Collection

        Public Sub Add()

        End Sub

        Public Sub Remove()

        End Sub

        Public Sub Print()
            For counter As Integer = 0 To x.Count-1 Step 1

            Next counter
        End Sub
    End Class
Now I have declared an instance of 'class a' and multiple threads are able to perform actions upon it.

My problem comes when one thread is running a print operation and another may be removing an item from the collection. The print operation is printing the data in a loop, when the loop began 'counter' was set to the count of the collection. The collection's size has since changed and we are headed for an "index out of bounds" error.

I need to lock the instance of the class as a whole so that only one thread can perform operations on the instance at any one time.

I know about the 'SyncLock' feature but am unsure if this is sufficient. Is the answer just to synclock the calls to the class methods? E.G...

Code:
Dim objSync as Object

SyncLock objSync
   instance.Print()
End SyncLock
Any pointers or leads would be greatly appreciated,
Have a great morning!
Matt.

Last edited by GottaGetITDone; Mar 11th, 2007 at 10:26 PM. Reason: Resolved by the weapon JM!
GottaGetITDone is offline   Reply With Quote
Old Mar 11th, 2007, 07:43 PM   #2
jmcilhinney
.NUT
 
jmcilhinney's Avatar
 
Join Date: May 05
Location: Sydney, Australia
Posts: 61,544
jmcilhinney has a reputation beyond repute (3000+)jmcilhinney has a reputation beyond repute (3000+)jmcilhinney has a reputation beyond repute (3000+)jmcilhinney has a reputation beyond repute (3000+)jmcilhinney has a reputation beyond repute (3000+)jmcilhinney has a reputation beyond repute (3000+)jmcilhinney has a reputation beyond repute (3000+)jmcilhinney has a reputation beyond repute (3000+)jmcilhinney has a reputation beyond repute (3000+)jmcilhinney has a reputation beyond repute (3000+)jmcilhinney has a reputation beyond repute (3000+)
Re: [2005] Threading and Synchronization

It would be more fitting to handle the synchronisation within ClassA, e.g.:
vb Code:
  1. Public Class ClassA
  2.     Private items As ArrayList
  3.     Public Sub Add()
  4.         SyncLock items.SyncRoot
  5.             'Add item here.
  6.         End SyncLock
  7.     End Sub
  8.     Public Sub Remove()
  9.         SyncLock items.SyncRoot
  10.             'Remove item here.
  11.         End SyncLock
  12.     End Sub
  13.     Public Sub Print()
  14.         SyncLock items.SyncRoot
  15.             For index As Integer = 0 To Me.items.Count - 1 Step 1
  16.             Next index
  17.         End SyncLock
  18.     End Sub
  19. End Class
You now have three linked critical sections. Only one thread can enter any one of the critical sections at a time. If a thread enters the Print method and enters the SyncLock block, then another thread enters the Add or Remove method, the second thread will have to wait at the SyncLock statement until the first thread completes its job and exits the SyncLock block in the Print method. It's like having a house with multiple doors and rooms and a sensor that can tell when someone is inside. It doesn't matter which door someone enters or which room they're in, as long as someone is inside all entrances remain locked until they exit.
__________________

2007-2010

Why is my data not saved to my database? | Communicating between multiple forms | MSDN Data Walkthroughs
MSDN "How Do I?" Videos: VB | C#
VBForums Database Development FAQ
My CodeBank Submissions: VB (Nullable Data Extensions *NEW*) (Serial Code TextBox *NEW*) | C# (ForumAccount has translated some of my VB submissions to C#)
My Blog: Defining and Raising Custom Events | Using Parameters in ADO.NET | Keyboard Events *NEW*
jmcilhinney is online now   Reply With Quote
Old Mar 11th, 2007, 10:25 PM   #3
GottaGetITDone
Lively Member
 
Join Date: Mar 06
Posts: 65
GottaGetITDone is on a distinguished road (10+)
Re: [2005] Threading and Synchronization

PERFECT! Absolutely <expletive> PERFECT!!!

Damn JM your onto it mate! That is EXACTLY what I was looking for.

Using that theory, I could declare a member variable of type Object, instantiate it in the constructor and lock my critical sections using the same object each time? By using the same object instance I will create the desired effect, basically copy the SyncRoot feature. Yeah?

I was not properly conceptualizing the function of the lock object in the SyncLock statement...all clear now though.

Thanks AGAIN JM, you always seem to sort us out!
Matt.
GottaGetITDone is offline   Reply With Quote
Old Mar 11th, 2007, 11:56 PM   #4
jmcilhinney
.NUT
 
jmcilhinney's Avatar
 
Join Date: May 05
Location: Sydney, Australia
Posts: 61,544
jmcilhinney has a reputation beyond repute (3000+)jmcilhinney has a reputation beyond repute (3000+)jmcilhinney has a reputation beyond repute (3000+)jmcilhinney has a reputation beyond repute (3000+)jmcilhinney has a reputation beyond repute (3000+)jmcilhinney has a reputation beyond repute (3000+)jmcilhinney has a reputation beyond repute (3000+)jmcilhinney has a reputation beyond repute (3000+)jmcilhinney has a reputation beyond repute (3000+)jmcilhinney has a reputation beyond repute (3000+)jmcilhinney has a reputation beyond repute (3000+)
Re: [RESOLVED] [2005] Threading and Synchronization

Are you trying to create your own strongly-typed collection or just a class that contains a collection?
__________________

2007-2010

Why is my data not saved to my database? | Communicating between multiple forms | MSDN Data Walkthroughs
MSDN "How Do I?" Videos: VB | C#
VBForums Database Development FAQ
My CodeBank Submissions: VB (Nullable Data Extensions *NEW*) (Serial Code TextBox *NEW*) | C# (ForumAccount has translated some of my VB submissions to C#)
My Blog: Defining and Raising Custom Events | Using Parameters in ADO.NET | Keyboard Events *NEW*
jmcilhinney is online now   Reply With Quote
Reply

Go Back   VBForums > Visual Basic > Visual Basic .NET


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT -5. The time now is 05:38 AM.





Acceptable Use Policy

Internet.com
The Network for Technology Professionals

Search:

About Internet.com

Legal Notices, Licensing, Permissions, Privacy Policy.
Advertise | Newsletters | E-mail Offers

Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.