Re: Static Functions in a Multi Threaded App
- From: "Nicholas Paldino [.NET/C# MVP]" <mvp@xxxxxxxxxxxxxxxxxxxxxxxxxxx>
- Date: Tue, 14 Jun 2005 12:08:15 -0400
SRLoka,
It all depends on what you are doing in the function. Just because you
arent accessing fields (static or instance), doesn't mean that you won't run
into threading issues.
Your best bet would be to place the MethodImpl attribute on the method,
passing the MethodImplOptions.Synchronized value into the constructor. This
will ensure that access to the Log method is synchronized.
If you created a separate class instance on each thread, it wouldn't
necessarily work, since they might all access the same file, and you could
end up interweaving the writes to the file in between each other (if you did
multiple writes per call to Log).
Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mvp@xxxxxxxxxxxxxxxxxxxxxxxxxxx
"Seenu" <Srinivas.Reddy.Loka@xxxxxxxxx> wrote in message
news:1118764208.995894.148090@xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
> Is it safe to create a static function in a multi threaded application
> ? As an example, I have a function that logs errors ,exceptions and
> some informational data
> public static void Log(....)
> {
> //
> }
>
> There are multiple threads in this application that call this function.
> Is there any advantage if each thread creates an instance of the
> class(I would remove the static) and uses it ? The function itself does
> not access any instance variables. Will there be any thread saefty
> issues if it is static. ?
>
> Thank You
>
> SRLoka
>
.
- Follow-Ups:
- Re: Static Functions in a Multi Threaded App
- From: Seenu
- Re: Static Functions in a Multi Threaded App
- References:
- Static Functions in a Multi Threaded App
- From: Seenu
- Static Functions in a Multi Threaded App
- Prev by Date: Re: Problem converting byte[] to string
- Next by Date: Re: for loop not stopping when it should
- Previous by thread: Static Functions in a Multi Threaded App
- Next by thread: Re: Static Functions in a Multi Threaded App
- Index(es):
Relevant Pages
|