Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Static Method and thread saftey!? Burning midnight oil.

Status
Not open for further replies.

Insider1984

Technical User
Feb 15, 2002
132
US
Hi everyone. Not sure why this wasn't a problem before but we are in a multi thread application and multiple threads are calling into the same static class and method. Well as you can guess they are conflicting and while I can do fairly well on interface and general programming I'm a virgin thread guy.

I'm aware of ways to lock calls but the examples I have seen do not seem to work on my code. I'm sure I'm doing something really silly but hey, that is why I call myself a virgin.

Any help on this would be great. Thank you kindly.

=====================
Insider
4 year 'on the fly' programmer
C, C++, C#, MFC, Basic, Java ASP.NET
 
Static means a single-instance of the method exists. It does not protect against multiple access by different threads.

Inside your static method you need to use the lock keyword, or possibly even a heavyweight mutex to protect your contents from being changed by another thread.

Chip H.


____________________________________________________________________
If you want to get the best response to a question, please read FAQ222-2244 first
 
As a rule of thumb - if you want to use static classes with threaded applications without making the application wait and process one thread at a time the static class can only change data in a class which is accessible from the active thread.

Once you start modifiying anything with multiple thread scope - such as other static object or values, or objects and values retreived from an application global store - you're going to get interesting and infuriating problems.

lock and mutual exclusions are the final tool in your arsenel if you can't make an architectural work around - remember that when a thread is locked your code is going nowhere and somewhere down the line some poor, lonely other thread is going to be waiting with the dinner going cold for the locked thread to come home.

You don't mind having two trains on the same track as long as they don't share the same point on the track at the same time. That's what's called a crash.

Post code if you like - we'll point at it, nod sonerously and say 'Yeup, yeup, yeup - that's your problem alright...'


Yet another unchecked rambling brought to you by:
Oddball
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top