How to make static methods thread safe 
Author Message
 How to make static methods thread safe

Hello,

Does anybody know a static method can be made thread safe. From the Jave
(forgive me) language I know that there is the keyword 'synchronized'. What
is the equivalent for C#? Can anybody shed some light on it.

thanks,
Henk



Sun, 25 Jul 2004 23:58:11 GMT  
 How to make static methods thread safe
try adding this attribute to the method:
    [MethodImpl(MethodImplOptions.Synchronized)]

and check for namespace:
    using System.Runtime.CompilerServices;


Quote:
> Does anybody know a static method can be made thread safe. From the Jave
> (forgive me) language I know that there is the keyword 'synchronized'. What
> is the equivalent for C#? Can anybody shed some light on it.



Mon, 26 Jul 2004 00:09:06 GMT  
 How to make static methods thread safe
I believe you could you use the lock keyword for this, like below:

class MyClass {
    public static void MyThreadSafeFunc() {
        lock(typeof(MyClass)) {
            //my thread safe code here
        }
    }

Quote:
}

The fact that I believe this means it's probably wrong, though...

GB
Inside the method, could you use lock(typeof(<your-type-here>))


Quote:
> Hello,

> Does anybody know a static method can be made thread safe. From the Jave
> (forgive me) language I know that there is the keyword 'synchronized'.
What
> is the equivalent for C#? Can anybody shed some light on it.

> thanks,
> Henk



Mon, 26 Jul 2004 00:14:44 GMT  
 How to make static methods thread safe
Quote:

> I believe you could you use the lock keyword for this, like below:

> class MyClass {
>     public static void MyThreadSafeFunc() {
>         lock(typeof(MyClass)) {
>             //my thread safe code here
>         }
>     }
> }

> The fact that I believe this means it's probably wrong, though...

Sure not, it's completely correct.
By setting the lock on a type, you effectively prevent other threads to run the code inside in the block.

Willy.



Mon, 26 Jul 2004 01:22:01 GMT  
 How to make static methods thread safe
You might want to look into the System.Threading.Mutex class as well.
Instance a mutex in you main class for each member (say, a collection) you
want to make thread safe.

System.Threading.Mutex m1 = new System.Threading.Mutex();

Then in each place where you access this collection, lock it using
m1.WaitOne();

and release it using:
m1.ReleaseMutex();

I believe you get more control over your locks and can optimize your apps
better.

--
Robert Jeppesen
MindCom AB



Quote:
> > I believe you could you use the lock keyword for this, like below:

> > class MyClass {
> >     public static void MyThreadSafeFunc() {
> >         lock(typeof(MyClass)) {
> >             //my thread safe code here
> >         }
> >     }
> > }

> > The fact that I believe this means it's probably wrong, though...

> Sure not, it's completely correct.
> By setting the lock on a type, you effectively prevent other threads to

run the code inside in the block.
Quote:

> Willy.



Mon, 26 Jul 2004 07:20:30 GMT  
 How to make static methods thread safe


Quote:
> You might want to look into the System.Threading.Mutex class as well.
> Instance a mutex in you main class for each member (say, a collection) you
> want to make thread safe.
[snip]
> I believe you get more control over your locks and can optimize your apps
> better.

Why is this better than ReaderWriterLock (except for interprocess locking) ?
With ReaderWriterLock you can have many threads reading while accept only
one writing.
That makes it IMHO more appropriate (faster) than lock or Mutex
I am missing something ?

Regards



Mon, 26 Jul 2004 07:42:30 GMT  
 How to make static methods thread safe
That's true.
I had missed this one!

--
Robert Jeppesen
MindCom AB

Quote:



> > You might want to look into the System.Threading.Mutex class as well.
> > Instance a mutex in you main class for each member (say, a collection)
you
> > want to make thread safe.
> [snip]
> > I believe you get more control over your locks and can optimize your
apps
> > better.

> Why is this better than ReaderWriterLock (except for interprocess locking)
?
> With ReaderWriterLock you can have many threads reading while accept only
> one writing.
> That makes it IMHO more appropriate (faster) than lock or Mutex
> I am missing something ?

> Regards



Mon, 26 Jul 2004 07:55:41 GMT  
 How to make static methods thread safe
Hi,

Thanks for your response to my question. I applied your suggestion and
it works. From all reponses this one makes the most sense.

Thanks,

Henk van der Geld
Development Engineer
DataBalk The Netherlands

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!



Mon, 26 Jul 2004 16:37:04 GMT  
 
 [ 8 post ] 

 Relevant Pages 

1. Thread Safe Events and Static Methods

2. Are static methods thread safe?

3. Making gcc thread safe.

4. Making thread safe STL containers

5. Making legacy C code thread-safe

6. Making things thread-safe

7. Making code thread-safe

8. Call to local static object constructor not thread-safe

9. Why is function static singleton not thread-safe?

10. Is static variable initialization thread-safe?

11. Thread safety in static methods

12. ANN: sigslot - C++ Portable, Thread-Safe, Type-Safe Signal/Slot Library

 

 
Powered by phpBB® Forum Software