471,306 Members | 1,290 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,306 software developers and data experts.

synchronous call


how to make a single method thread safe?

example:
class A
{
void func() {}
}

thread1:
A a1 = new A();
a.func();

thread2:
A a2 = new A();
a.func();
Ivan
Dec 13 '05 #1
4 1921
Ivan,

The code that you just posted is actually thread safe because it doesn't
do anything.

Making an object or method call thread-safe depends on the granularity
you are looking to achieve, along with the resources you are trying to make
thread safe.

Can you be more specific about what you are trying to make thread-safe?
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Ivan" <iv*********@dynamic-imaging.com> wrote in message
news:%2***************@TK2MSFTNGP09.phx.gbl...

how to make a single method thread safe?

example:
class A
{
void func() {}
}

thread1:
A a1 = new A();
a.func();

thread2:
A a2 = new A();
a.func();
Ivan

Dec 13 '05 #2
A.func() needs to be application-wide thread safe.
Having the example in mind, if a1 enters func(), a2 should wait.

example:
class A
{
void func() {}
}

thread1:
A a1 = new A();
a1.func();

thread2:
A a2 = new A();
a2.func();
Ivan
Dec 13 '05 #3
Ivan,

Check out the MethodImpl attribute, and pass in the
MethodImplOptions.Synchronized value to the attribute.

However, this will make any call on the object (you need to apply it to
every method) hold whenever any other method is called on any other thread.

Again, you didn't show anything that you were trying to synchronize, so
this is the most generic code you could use.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Ivan" <iv*********@dynamic-imaging.com> wrote in message
news:OO**************@TK2MSFTNGP15.phx.gbl...
A.func() needs to be application-wide thread safe.
Having the example in mind, if a1 enters func(), a2 should wait.

example:
class A
{
void func() {}
}

thread1:
A a1 = new A();
a1.func();

thread2:
A a2 = new A();
a2.func();
Ivan

Dec 14 '05 #4
Again, you didn't show anything that you were trying to synchronize, so ... synchronized code is substituted for simplicity by A.func()
this is code that only one thread needs to be in.
if I make it static and use this attribute (thanks for the attribute), my problem's fixed.

example:

using System.Runtime.CompilerServices;

class A
{
[MethodImplAttribute(MethodImplOptions.Synchronized )]
public static void func() {}
}

thread1:
A a1 = new A();
a1.func();

thread2:
A a2 = new A();
a2.func();
Ivan
Nicholas Paldino [.NET/C# MVP] wrote: Ivan,

Check out the MethodImpl attribute, and pass in the
MethodImplOptions.Synchronized value to the attribute.

However, this will make any call on the object (you need to apply it to
every method) hold whenever any other method is called on any other thread.

Dec 14 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

1 post views Thread by Chris | last post: by
1 post views Thread by Alexander Kaplunov | last post: by
2 posts views Thread by duncansinclair | last post: by
3 posts views Thread by Pro1712 | last post: by
1 post views Thread by Walther W | last post: by
4 posts views Thread by keyofdminor | last post: by
2 posts views Thread by ng01 | last post: by

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.