Multithread - Demonstrating Multithreading within CORBA Servers

Description:

One of the tasks of CORBA's object adaptors is the management of concurrent access to its server objects. Therefore, incoming calls will be routed (depending on the actual configuration) to one or more worker threads concurrently accessing the server object.

This example makes implicit multithreading within the CORBA server process visible.

Source:

\Demo\MultiThread

Goal:

Show multithreading within the CORBA server process.


Example

Step 1. The IDL (MultiThread.idl):

The IDL is more or less our usual 'Greetings' interface:

    module MultiThread
    {
        interface Greetings
        {
            string hello( in string a_strmyName);    
        };
    };
    

(Will be compiled into MultiThread.cs)

Step 2. The Server Implementation (within MtThrdSrv.cs):

To create a "concurrency unfriendly" environment, we send the current thread running the 'hello()' method for some time to sleep:

    public class GreetingsImpl: MultiThread.GreetingsPOA
    {    
        public override string hello(  string _a_strmyName  )
        {
            Monitor.Enter(this);
            System.Console.WriteLine("Call 'hello', Parameter '{0}', is processed by thread {1}",    
                                     _a_strmyName, Thread.CurrentThread.Name);
            Monitor.Exit(this);
            
            // Sleep 2 Sec.
            Thread.Sleep((int)2000);
            
            return "End call";
        }
    }
    

Note, that we have to lock the CORBA server object within this scenario during console output to synchronize the access to the console in a threadsafe manner.

Step 3. The Client (MtThrdClt.cs):

To increase the concurrent load, the client will explicitly start a couple of threads accessing the server concurrently:

    // The Threadpool
    m_thdPool = new Thread[]
    {
        new Thread( new ThreadStart( runRequest)),
        new Thread( new ThreadStart( runRequest)),
        new Thread( new ThreadStart( runRequest)),
        new Thread( new ThreadStart( runRequest)),
        new Thread( new ThreadStart( runRequest))
    };
    
    // Start the threads
    int iCnt = 0;
    foreach( Thread oThd in m_thdPool)
    {
        oThd.IsBackground = true;
        oThd.Name = "CltRequestThd-" + iCnt.ToString();    
        iCnt++;
        oThd.Start();
    }
    

The 'runRequest()' method executed within the threads:

    public void runRequest()
    {
        MultiThread.Greetings  m_oIGreetings;
        
        Ics.CORBA.Object oGreetingsObj = m_oOrb.string_to_object( "file://c:\\MtThd.ior");       
        
        m_oIGreetings  = MultiThread.GreetingsHelper.narrow( oGreetingsObj);
        
        string strRet = m_oIGreetings.hello( Thread.CurrentThread.Name);
        
        Monitor.Enter(this);
        Console.WriteLine("{0} Thread{1}", strRet, Thread.CurrentThread.Name);
        Monitor.Exit( this);
    }
    

Note, that we have to synchronize the access to the console in the client as well.

Step 4. Run the example

a.) Start the Server.

b.) Start the Client.