Singleton and concurrency in .NET 2.0

  • Thread starter Thread starter Luis Farzati
  • Start date Start date
L

Luis Farzati

Hello,

I have some questions about Singleton and concurrency, I'd appreciate
any help you can give me.

I have a Windows Service which, on start, registers on remoting (using
IPC channel and WellKnownObjectMode.Singleton) a 'Test' class.

public class Test
{
public void SendMessage()
{
// etc. etc.
}
}

1. First, just to be sure, the instantiation and singleton
implementation is already provided by Remoting, right? Because I just
ask for the object using Activator.GetObject and I receive it without
additional code.

2. If two (or more) simultaneous invocations to SendMessage() occurs,
is the execution sequential? I mean, the first thread executes
SendMessage() and the second waits for the first to finish? Or both of
them executes the method at the same time?

3. In case the execution is sequential, if I'd want to SendMessage() be
executed simultaneously any times as invoked, it would be correct that
SendMessage() be just a wrapper that creates a new Thread and executes
inside it a method i.e. SendMessageImpl()?

Thank you in advance.

Luis
 
Luis,

See inline:
1. First, just to be sure, the instantiation and singleton
implementation is already provided by Remoting, right? Because I just
ask for the object using Activator.GetObject and I receive it without
additional code.

Yes, the remoting infrastructure will handle the instantiation.

2. If two (or more) simultaneous invocations to SendMessage() occurs,
is the execution sequential? I mean, the first thread executes
SendMessage() and the second waits for the first to finish? Or both of
them executes the method at the same time?

No, the execution is not sequential, you will have to write thread-safe
code.
3. In case the execution is sequential, if I'd want to SendMessage() be
executed simultaneously any times as invoked, it would be correct that
SendMessage() be just a wrapper that creates a new Thread and executes
inside it a method i.e. SendMessageImpl()?

No need to do this, since the calls are not sequential.

Hope this helps.
 
Back
Top