473,507 Members | 13,597 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Synchronization Attributes

Dear All,

[Synchronization]
Can synchronize a class, any function is called and the entire class
is locked.

[MethodImplAttribute(MethodImplOptions.Synchronized )]
Can synchronize a method

What if I need to Lock a class only when specific method is call?

For Example:

public class C
{
public void A() {}
public void B() {}
}

The instance of class C need to be locked when method B() is called,
but not lock when method A() is called.

Certainly I can manually put 'lock(this)' in method B(). But is there
any other way of doing this?

Thanks in advance
Aug 18 '08 #1
3 6914
On Sun, 17 Aug 2008 21:49:28 -0700, CKKwan <ck****@my-deja.comwrote:
Dear All,

[Synchronization]
Can synchronize a class, any function is called and the entire class
is locked.
No, not at all. That attribute is used for remoting. I admit, I'm not
completely sure of the details, but I can guarantee you that it doesn't
lock "an entire class". I wrote a small test program and confirmed that
multiple threads can still access members of a given class simultaneously
even with that attribute applied to the class.

It appears to me that it handles synchronization across remoting contexts,
whatever those are. It probably ensures synchronization/consistency
across multiple remoting endpoints. But whatever it does, it's definitely
not a general-purpose synchronization mechanism.
[MethodImplAttribute(MethodImplOptions.Synchronized )]
Can synchronize a method
No, not precisely. Synchronizes _all_ such labeled methods in a class,
along with anything that explicitly uses the type (when applied to static
members) or instance (when applied to instance members) for locking.
What if I need to Lock a class only when specific method is call?
I'm not convinced the question makes sense. See below...
For Example:

public class C
{
public void A() {}
public void B() {}
}

The instance of class C need to be locked when method B() is called,
but not lock when method A() is called.
What does it mean to lock "the instance", if you don't want method A() to
participate in the locking? If A() doesn't participate, then at least
some of the instance (i.e method A()) doesn't wind up locked. You can
lock "on the instance", but I wouldn't say that's the same as locking
"_the_ instance".
Certainly I can manually put 'lock(this)' in method B(). But is there
any other way of doing this?
Putting a "lock(this)" in method B() would have the effect of
synchronizing method B() and any other code that is also protected by
locking on the instance. That is, only one thread could be executing any
of those at once.

If that's the effect you want, then why not use that approach? If it's
not the effect you want, then maybe you can try to be more clear about
what effect you do want.

Pete
Aug 18 '08 #2
On Aug 18, 1:20*pm, "Peter Duniho" <NpOeStPe...@nnowslpianmk.com>
wrote:
[MethodImplAttribute(MethodImplOptions.Synchronized )]
Can synchronize a method

No, not precisely. *Synchronizes _all_ such labeled methods in a class,*
along with anything that explicitly uses the type (when applied to static*
members) or instance (when applied to instance members) for locking.
Oh Pete, you are correct! It lock on the instance. And that is exactly
what I want.

Thanks for the help and sorry for the confusion.

p.s. I wonder why google forced me to use this stupid Id on reply :(
Aug 18 '08 #3
On Sun, 17 Aug 2008 23:40:59 -0700, <st*********@gmail.comwrote:
On Aug 18, 1:20Â*pm, "Peter Duniho" <NpOeStPe...@nnowslpianmk.com>
wrote:
[MethodImplAttribute(MethodImplOptions.Synchronized )]
Can synchronize a method

No, not precisely. Â*Synchronizes _all_ such labeled methods in a class,
Â*
along with anything that explicitly uses the type (when applied to
static Â*
members) or instance (when applied to instance members) for locking.

Oh Pete, you are correct! It lock on the instance. And that is exactly
what I want.
For what it's worth, it is generally better to _not_ use the instance for
locking. The instance of your class is exposed to users of the class and
while it would be better for _that_ code to also not use the instance,
there's nothing your class can do to stop it. That means that if your
code uses its own instance for locking, it's risking being blocked for
access to itself by code elsewhere outside your control. This can cause a
variety of problems, including deadlock.

It's better for you to allocate a dummy object instance used solely for
the purpose of locking, and do the locking yourself explicitly rather than
using something like MethodImplOptions.Synchronized.

Pete
Aug 18 '08 #4

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

Similar topics

5
15562
by: Cyrus | last post by:
I have a question regarding synchronization across multiple threads for a Hashtable. Currently I have a Threadpool that is creating worker threads based on requests to read/write to a hashtable....
4
3165
by: scott | last post by:
hi all, Thx to any one that can offer me help, it will be much appreciated. iv got a multithreaded program and need to use thread synchronization. The synchronization does not have to...
0
3477
by: Rod | last post by:
I orginally posted this to microsoft.public.sqlserver.ce but had not received any responses. I have a CF.NET application (C#) with a SqlCE database. We had originally planned to use SQL...
7
5395
by: Robert | last post by:
Hi, I have noticed some synchronization issues when using javascript. I'll give you an example. It is easy to reproduce the problem if you can cause some delay in the webserver before sending...
5
2670
by: fei.liu | last post by:
Hello, in the application I am developing, I am having trouble to synchronize event triggered actions using 'lock(ob){...};' technique. Here is a outline of my code: class C{ int x = 0; public...
12
1988
by: emma_middlebrook | last post by:
Hi Say you had N threads doing some jobs (not from a shared queue or anything like that, they each know how to do their own set of jobs in a self-contained way). How can you coordinate them so...
0
1680
by: lbrtchx | last post by:
Say you need to serve the same Web content from a number of IP address, which you need to keep out there with high availability requirements ~ I think Web Services would be a good candidate for...
0
1953
by: sundman.anders | last post by:
Hi all! I have a question about thread synchronization and c++ streams (iostreams, stringstreams, etc). When optimizing a program for a multicore processor I found that stringstream was causing...
15
2690
by: ingejg | last post by:
I am starting to study internet synchronization, and my head is still spinning since internet is not my forte, however my boss is breathing down my neck at the moment. Our company has only one...
0
7223
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
7110
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...
1
7030
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...
0
7482
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...
0
5623
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,...
0
3191
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3179
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
758
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
411
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.