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

Need clarifikation about lock(this) and [MethodImpl(MethodImplOptions.Synchronized)]

Hi

Can somebody tell me if I'm right or not

The attribute [MethodImpl(MethodImplOptions.Synchronized)]
Is like an lock(this) over the Method where i put this Attribute?!

If I have two methods like:

[MethodImpl(MethodImplOptions.Synchronized)]
void method1() {do something;}
[MethodImpl(MethodImplOptions.Synchronized)]
void method2() {do something else;}

And I call method1 and while it is still running method2.
The processing of method2 will only start after method1 is finished!

I hope I understand this right
(because if not I will get a serious synchronizing Problem :-/ )
regards
Norbert

Nov 17 '05 #1
5 4886
Norbert,

You are right, when you add the MethodImpl attribute with the
synchronized option, it would be the same as locking on the instance of the
object (if it is an instance method) and locking on the Type instance for
that type if it is a static method.

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

<no**********@gmail.com> wrote in message
news:11**********************@g14g2000cwa.googlegr oups.com...
Hi

Can somebody tell me if I'm right or not

The attribute [MethodImpl(MethodImplOptions.Synchronized)]
Is like an lock(this) over the Method where i put this Attribute?!

If I have two methods like:

[MethodImpl(MethodImplOptions.Synchronized)]
void method1() {do something;}
[MethodImpl(MethodImplOptions.Synchronized)]
void method2() {do something else;}

And I call method1 and while it is still running method2.
The processing of method2 will only start after method1 is finished!

I hope I understand this right
(because if not I will get a serious synchronizing Problem :-/ )
regards
Norbert

Nov 17 '05 #2
<no**********@gmail.com> wrote:
Can somebody tell me if I'm right or not

The attribute [MethodImpl(MethodImplOptions.Synchronized)]
Is like an lock(this) over the Method where i put this Attribute?!

If I have two methods like:

[MethodImpl(MethodImplOptions.Synchronized)]
void method1() {do something;}
[MethodImpl(MethodImplOptions.Synchronized)]
void method2() {do something else;}

And I call method1 and while it is still running method2.
The processing of method2 will only start after method1 is finished!

I hope I understand this right
(because if not I will get a serious synchronizing Problem :-/ )


Yes, that's right. It's rarely a good idea though:

1) It's rare to find a method where the whole method needs to be
synchronized rather than just a portion of it.

2) Locking on "this" or the type object isn't a good idea:
http://www.pobox.com/~skeet/csharp/t...ckchoice.shtml

3) The attribute is easier to overlook than the much more common lock
statement.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 17 '05 #3
The reason i need it is the following

i have a class encapsulate communication over tcp

there are several commandos supported
like
start
stop
getvalue(valueID) and so on

the problem is, that because of synchronisation issues i only want one
command
to be run at one time
(i know its dirty, i want to redesign it next week, but i have a
deadline this week :-( )

nevertheless
thanks for your help

Nov 17 '05 #4
nobs <no**********@gmail.com> wrote:
The reason i need it is the following

i have a class encapsulate communication over tcp

there are several commandos supported
like
start
stop
getvalue(valueID) and so on

the problem is, that because of synchronisation issues i only want one
command
to be run at one time
(i know its dirty, i want to redesign it next week, but i have a
deadline this week :-( )


That just shows you need synchronization - it doesn't show that you
need to use MethodImplOptions.Synchronized.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 17 '05 #5
I know, I know
but on monday everything would be fine again :-)

Nov 17 '05 #6

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

Similar topics

4
by: Rich Sienkiewicz | last post by:
Some classes, like Queue and SortedList, have a Synchronized method which gives a thread safe wrapper object for these classes. But the lock() statement does the same thing. Is there any rules as to...
2
by: Robert A. van Ginkel | last post by:
I have a strange C# problem. Is the following a bug? Because it should be possible to run unsafe code. How can I accomplish this? And where can I read more about this, because documentation on this...
9
by: Roger Down | last post by:
Lets say I have a method UpdateCache() called from a single thread. I also have a method GetCache() called from multiple threads. When UpdateCache() is called, the cache updating is being...
5
by: Seeker | last post by:
Hello, I've read conflicting posts about . Does it or does it not lock the entire object? In my simple test it appears to block just the method but I wouldn't exactly call my meager test...
24
by: Dan Bass | last post by:
I know that XslTransform's Transform is thread safe according to the MSDN, and that Load is not. I've therefore applied this simply Mutex to it and would just like to confirm this is okay. ...
2
by: yaron | last post by:
Hi, Does MethodImpl Synchronized attribute is like lock(this) ? i mean that a call to Monitor.PulseAll(this) from a Synchronized method will trigger other thread that call to Monitor.Wait(this)...
3
by: Ryan Liu | last post by:
Hi, What does ArrayList.Synchronized really do for an ArrayList? Is that equal to add lock(this) for all its public methods and properties? Not just for Add()/Insert()/Remvoe()/Count, but also...
12
by: cody | last post by:
Why can I overload operator== and operator!= separately having different implementations and additionally I can override equals() also having a different implementation. Why not forbid...
10
by: master | last post by:
As far as I understand, 'lock' is the only option, isn't it? Namely, If I want to declare a 'synchronised' method, I do it this way: class A { void AMethod() { lock (this) { // the method...
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
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...
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...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
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
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

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.