473,385 Members | 1,925 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,385 software developers and data experts.

Class and Multi-thread safety

Hello,

Is there any keyword applicable to a class to make it thread-safe?
Without having to put lock(this){} in all functions?

Thanks.

Oct 16 '07 #1
5 3345
Nuno Magalhaes <nu************@hotmail.comwrote:
Is there any keyword applicable to a class to make it thread-safe?
Without having to put lock(this){} in all functions?
Putting lock(this) doesn't make it thread-safe.

Thread safety is not a simple matter - you need to carefully consider
threading and various issues when making a class thread-safe. In
particular, think about any code in other classes you call while
holding a lock - if you're not careful, you can easily deadlock.

I'd also recommend against locking on "this" to start with.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Oct 16 '07 #2
Nuno Magalhaes wrote:
Is there any keyword applicable to a class to make it thread-safe?
Without having to put lock(this){} in all functions?
No.
Oct 16 '07 #3
On 16 Out, 19:25, Jon Skeet [C# MVP] <sk...@pobox.comwrote:
Nuno Magalhaes <nunommagalh...@hotmail.comwrote:
Is there any keyword applicable to a class to make it thread-safe?
Without having to put lock(this){} in all functions?

Putting lock(this) doesn't make it thread-safe.

Thread safety is not a simple matter - you need to carefully consider
threading and various issues when making a class thread-safe. In
particular, think about any code in other classes you call while
holding a lock - if you're not careful, you can easily deadlock.

I'd also recommend against locking on "this" to start with.

--
Jon Skeet - <sk...@pobox.com>http://www.pobox.com/~skeet Blog:http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
What is the difference between locking on "this" and locking on an
object inside the class? My object is never changed. What can go wrong?

Oct 16 '07 #4
Nuno Magalhaes <nu************@hotmail.comwrote:
What is the difference between locking on "this" and locking on an
object inside the class? My object is never changed. What can go wrong?
Someone else can acquire a lock on it, inadvertently blocking things at
the wrong time. The more control you can exercise over what lock is
taken out when, the fewer deadlock risks you'll have.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Oct 16 '07 #5
Nuno Magalhaes wrote:
>
What is the difference between locking on "this" and locking on an
object inside the class? My object is never changed. What can go wrong?
lock(this) is not considered good practice (anymore). Check MS docs for the
lock statement for more information.

Regards,

Mads

--
Med venlig hilsen/Regards

Systemudvikler/Systemsdeveloper cand.scient.dat, Ph.d., Mads Bondo
Dydensborg
Dansk BiblioteksCenter A/S, Tempovej 7-11, 2750 Ballerup, Tlf. +45 44 86 77
34
Oct 17 '07 #6

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

Similar topics

1
by: BJS | last post by:
Sorry for the cross-posting, but based on the number of people I have seen ask for a solution to this problem, I hope by cross-posting this, that it will help a lot of people out of a common...
2
by: msnews.microsoft.com | last post by:
How can we access an internal class in a referenced file in a multi-file assembly. All documentation states the obvious purpose of the internal keyword on a class, but when referencing an...
2
by: Steve Stover | last post by:
I want to use the caching API in .net to store data. The data would be stored in a data table class that has approx. 10 columns and 71 rows. What I need to know is this: According to Microsoft's...
2
by: John Salerno | last post by:
I'm reading about class methods now and I have a question about calling them. I know it is wrong of me to assume it needs to work in a parallel manner to an instance method, but I'm curious why it...
16
by: tshad | last post by:
This is a little complicated to explain but I have some web services on a machine that work great. The problem is that I have run into a situation where I need to set up my program to access one...
6
by: napi | last post by:
C to Java Class Files ------------------------------- We are pleased to announce the release of version '1.5.1' of AMPC (Axiomatic Multi-Platform C) C to Java class files compiler suite. It is...
3
by: Bob Altman | last post by:
In my unmanaged C++ app (VS 2005) I have written a class that inherits from exception. I want to export this class from a library DLL, but the compiler complains because exception is not itself...
43
by: Tony | last post by:
I'm working with GUI messaging and note that MFC encapsulates the message loop inside of a C++ class member function. Is this somehow inherently less robust than calling the message loop functions...
2
by: Smile | last post by:
Hi there, I have a question, please give me some idea. Thanks in advance! Solution1 | |---DBLogic (class library) | | | |-dblogic (class) which defines ConnString, functions to create...
16
by: Stefano Sabatini | last post by:
Hi all, I'm facing this design problem. I have a table which defines the behaviour of an Object in a given state according to the type of event it is receiving. So for each couple...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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?
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
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
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,...
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...

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.