3

John's console application calls my DLL function many times (~15x per sec). I am thinking to put this function as a static method.

I know that :

  1. It can only access static props and objects.
  2. It doesn't need an instance to run the function.

But I don't know if these are the only questions which i need to ask myself.

Each John's calls to my function is in a new thread that he creates.

  • If there is an error in my function, how will this affect all other calls?
  • Should I make this function a regular function with instance to the class (which John will create)?
  • What about GC?

What is the best practice answer for this question?

2
  • Me too :-) Static methods are fine, in context, it's the state (i.e. things like static fields/properties/objects) you have to worry about. There's lots of questions on Stack Overflow already about this - see stackoverflow.com/questions/2524524/… for example. Commented Dec 3, 2011 at 19:14
  • 1
    @Christoph I always use examples of : john , paul George and Ringo. Commented Dec 3, 2011 at 19:19

3 Answers 3

2

Sounds like there could be a problem. Calling a method which operates on static (shared) objects in a multithread environment should ring some alert bells for you.

Review your code and if there's a chance that a shared object is accessed from two (or more) threads at the same time, make the object an instance field and make your methods instance methods.

Of course, whether or not there is a risk depends much on the actual code (which you don't show) but making all calls nonstatic means that you lower the potential risk.

Sign up to request clarification or add additional context in comments.

1 Comment

now that you mentioned about 2 can access it at the same time (and i dont want to pause / lock any) , it does ring a bell ( a big one). thanks.
2

Generally, if your static method doesn't use any state (i.e. both reading and writing to static fields and properties, or calling other static methods that do so), there won't be any side effects for the caller, no matter how many threads they start.

If you're asking for best practice, static methods are mostly a bad idea though. If at all, static methods should be only used for very generic utility functionality.

It's not recommended because you can't predict if requirements change and you need some state one day. Then you'd better use a class that the caller can instantiate, and you'll break all existing code that uses your function.

About garbage collection, yes sure that has some overhead, but that is currently the sacrifice if you go the route of memory-managed OO. If you want more control, use unsafe code or a language like C++ or Vala.

2 Comments

If i recall correctly - STATIC are not going to GC... right ?
I don't know exactly what you mean, but for a static method there doesn't really exist anything for a GC to collect.
0

I would agree with Wiktor (+1), but would also add that if synchronization is required in the static method, it may be more efficient to use multiple instances. Otherwise, having many threads might be pointless as only one can access a critical section at a time.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.