473,795 Members | 2,986 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 2550
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********@hot mail.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.c om...
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********@hot mail.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********@hot mail.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
1861
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 contains methods for serialising and de-serialising objects to the database storage. I put this as a shared class as multiple web clients will be using the class to store and retreive data, the problem I'm haivng now is that I think multiple threads...
1
1760
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 to be shared between the several instances of the class belonging to the same request/user. I think I've read somewhere that shared properties are not shared among users in ASP.NET. (that would be logic as a shared property should die after each...
9
5405
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 for each thread, I put in Shared methods as below. It is only now that I am realizing the complexity of multiple threads accessing shared methods. And, quite honestly, I am very confused. I have tried System.Threading.Monitor.Enter, Synclock,...
2
9266
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 code how do I ensure that the two methods are thread safe? Public Class Class1 Public Shared Function addMe(ByVal i1 As Integer, ByVal i2 As Integer) As Integer Return i1 + i2 End Function
0
895
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
1321
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 Mutex.Waitone and Mutex.ReleaseMutex. Is that right? What 's wrong with my sub Writelog then? Best regards, Mobileboy
1
2637
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 using a shared object as a synclock candidate will generate any race conditions. Thanks. Core Public class ThreadChilds
1
2533
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 asp.net web page; private shared lockkey string SyncLock me lockey = "COBOLCALL" monitor.enter(lockey)
0
1152
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() end class
0
9519
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10439
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10215
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10165
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9043
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6783
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5563
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4113
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3727
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.