473,396 Members | 1,816 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

About Shared And SyncLock

1) Do Shared methods always need to be synchronised to be thread-safe?
2) Why must SyncLock be used with a reference type? I can't get the relation
between threads and a type.

Thanks for your help.
Henri

Nov 18 '05 #1
3 2522
1) Not always. If you have any Shared fields on the type and you are
both reading and writing them, then probably. Thread safety is needed
when you are sharing data between threads.

2) Threads are an abstraction that 'execute' code. Types are an
abstraction that 'contain' code. You can have multiple threads
executing the same code inside of a type concurrently. If you have
data that can be touched by multiple threads, you have to be careful
that two threads are not changing an ArrayList, for example, at the
same time, because it leads to unpredictable behavior and you spend a
lot of time debugging it and cursing at the computer.

SyncLock locks on a reference type. Think of it as setting a flag on
that reference type for the duration of the lock. When another thread
tries to take a lock on the same object, the runtime can see the flag
is set and force the second thread to wait for the lock to become
free.

Hope this made some sense..

--
Scott
http://www.OdeToCode.com
On Thu, 16 Sep 2004 00:19:55 +0200, "Henri" <hm********@hotmail.com>
wrote:
1) Do Shared methods always need to be synchronised to be thread-safe?
2) Why must SyncLock be used with a reference type? I can't get the relation
between threads and a type.

Thanks for your help.
Henri


Nov 18 '05 #2
Thanks for your explaination Scott :-) !
In Java, I was used to use an instance of a class to synchronise methods.
That's why I was a bit confused here about using a Type object instead.

About Shared methods: variables local to a method are not shared between
thread, are they.
"Scott Allen" <bitmask@[nospam].fred.net> a écrit dans le message de
news:n4********************************@4ax.com...
1) Not always. If you have any Shared fields on the type and you are
both reading and writing them, then probably. Thread safety is needed
when you are sharing data between threads.

2) Threads are an abstraction that 'execute' code. Types are an
abstraction that 'contain' code. You can have multiple threads
executing the same code inside of a type concurrently. If you have
data that can be touched by multiple threads, you have to be careful
that two threads are not changing an ArrayList, for example, at the
same time, because it leads to unpredictable behavior and you spend a
lot of time debugging it and cursing at the computer.

SyncLock locks on a reference type. Think of it as setting a flag on
that reference type for the duration of the lock. When another thread
tries to take a lock on the same object, the runtime can see the flag
is set and force the second thread to wait for the lock to become
free.

Hope this made some sense..

--
Scott
http://www.OdeToCode.com
On Thu, 16 Sep 2004 00:19:55 +0200, "Henri" <hm********@hotmail.com>
wrote:
1) Do Shared methods always need to be synchronised to be thread-safe?
2) Why must SyncLock be used with a reference type? I can't get the relationbetween threads and a type.

Thanks for your help.
Henri



Nov 18 '05 #3
On Thu, 16 Sep 2004 01:34:06 +0200, "Henri" <hm********@hotmail.com>
wrote:
Thanks for your explaination Scott :-) !
In Java, I was used to use an instance of a class to synchronise methods.
That's why I was a bit confused here about using a Type object instead.

Aaaahhh - that kind of reference. Well, you have to SyncLock on a
reference that is visible among all the threads. In earlier .NET days
if was common practice for a Shared class to lock on the Type instance
for the class. Unfortunately, this is not a great practice.

You want to lock on a reference that is visible to all the threads
that might come into a method on your class, but you do not want to
lock on a reference that is visible to the entire application. Someone
else might decide to take that lock too - and it leads to difficult to
debug deadlock situations where you stay up all night and threaten to
set fire to the computer.

Now sometimes, you might need a lock that can be seen outside of your
class, but generally you want a private object to lock on, so you know
only your class is locking and unlocking on that reference. Make a
Shared, Private field of type Object and lock, unlock on it.

About Shared methods: variables local to a method are not shared between
thread, are they.


Correct. If the method only uses local variables and parameters, no
need to take locks.

--
Scott
http://www.OdeToCode.com
Nov 18 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Paul | last post by:
Hi. Just trying to find out the best approach as I beleive it might give me problems later on down the road. I have an ASP.NET application which references a shared database class which...
1
by: Henri | last post by:
Hi, I'm using a custom class in my ASP.NET which has a Shared property. I don't want this Shared property to be shared between users because of thread concurrent accesses problems. I just want it...
9
by: Bob Day | last post by:
VS 2003, vb.net , sql msde... I have an application with multiple threads running. Its a telephony application where each thread represents a telephone line. For code that would be the same...
2
by: Aaron Cutlip | last post by:
I have been looking all over and have seen many possible ways to create a Syncronized Shared Function in VB.NET, but I would like some advice that will make my life easier. Given the following...
0
by: Chris Dunaway | last post by:
Consider the following simple class: Public MyClass Private t As Thread Shared LockObj As Object = New Object() Public Sub New() t = New Thread(AddressOf DoWork) t.Start
6
by: MobileBoy36 | last post by:
Hi All, I want to make a LogFile class that is thread safe. I use a Mutex for it. But the behavior of the class is not that normal. In a c# guide I read you can achieve it by simply using...
1
by: Core | last post by:
Hello, I am having a class that is used to spawn new threads off. In each of them, I have a shared function that will generate an unique id off a shared class variable. My question is whether...
1
by: =?Utf-8?B?QXJuZSBHYXJ2YW5kZXI=?= | last post by:
I have an ASP.net 2.0 website. This website have a module; Public Module myModule. Is all the data in this module shared and not thread safe? I would like explicitly create a shared object in an...
0
by: =?Utf-8?B?THVjYSBQaW5hc2Nv?= | last post by:
Hello I've had strange memory problem with my application compiled in debug mode. The problem is that in a visual basic class with an event declaration: public class Class1 public event Pippo()...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.