473,806 Members | 2,653 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.

[MethodImplAttri bute(MethodImpl Options.Synchro nized)]
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 6935
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.
[MethodImplAttri bute(MethodImpl Options.Synchro nized)]
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...@nn owslpianmk.com>
wrote:
[MethodImplAttri bute(MethodImpl Options.Synchro nized)]
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*********@gm ail.comwrote:
On Aug 18, 1:20Â*pm, "Peter Duniho" <NpOeStPe...@nn owslpianmk.com>
wrote:
[MethodImplAttri bute(MethodImpl Options.Synchro nized)]
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 MethodImplOptio ns.Synchronized .

Pete
Aug 18 '08 #4

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

Similar topics

5
15587
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. One function of the Hashtable is to iterate through its keys, which apparently is inherently not thread-safe. Other functions of the Hashtable include adding/modifying/deleting. To solve the synchronization issues I am doing two things: 1. Lock...
4
3206
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 work across multiple processes just the one. I was wondering if any one new which one used the least overhead. Im at current using mutexes but was wondering if there was something a bit
0
3490
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 Authentication to replicate with the publication and indeed had SQL Authentication working flawlessly. Then our client asked us to use Windows Authentication instead. For incremental changes, Windows authentication works fine. When trying to do the...
7
5406
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 the page. //filename = page.html <script type="text/javascript"> function test1()
5
2684
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 void incre(){ if(x < 20) x++; } } class Form1: WinForm{
12
2017
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 that they all wait until they've all done one job before starting off on each of their next jobs. I have been thinking about this for a day and can't seem to find a solution.
0
1707
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 the synchronization of Web pages/content. ~ What some frameworks do is database development and then let the databases themselves do the synchronization (Should I update my knowledge of this subject ;-))
0
1982
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 a LOT of synchronization overhead. After a bit of digging I concluded that this synchronization has to do with the access to a global locale inside the stream. The problem can be seen by running the small distilled benchmark code
15
2745
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 server with fixed IP address provided by our ISP, while the other sites (which I wish in the future hold the replicas databases) have only standard internet connections with Dynamic IP (which means that they change IP addresses, as given by the ISP...
0
9719
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9598
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
10623
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
10371
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...
0
10111
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9192
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
6877
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
5546
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4330
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

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.