How to replace the ORB thread factory in order to customize the creation of threads.

From Support
Jump to: navigation, search

Question:

Is it possible to replace the ORB thread factory in order to customize the creation of threads?

How can an application monitor the creation of VisiBroker worker threads?

(Information applies to VBC++ 3.x)

Answer:

It is possible to customize or replace the ORB thread factory. The ORB core internally uses VISThread class to create threads. A new class deriving from VISThread is created and the function "run" is called to create the thread. VISThread internally uses a static member function "_create_thread" which calls the appropriate create thread function (thr_create for Solaris and CreateThread for Windows) to create the thread. This function can be overriden, by calling the static member function VISThread::factory and passing it a new function. This will allow users to install their own thread create function or monitor create/exit of threads.

Example:

...

  1. include "vthread.h"

// New structure to store ORB thread start function and ORB user data struct MyData

{

void *(*func)(void *);

void *data;

};

// A function to monitor thread creation/destruction

void *thread_monitor(void *data)

{

MyData *myData = (MyData *)data; cout << "New thread created " << endl; void *ret = (*myData->func)(myData->data); cout << "Exiting thread" << endl; delete myData; return ret;

}

// Thread factory to replace VISThread::_create_thread thread

// factory function

void MyThreadFactory(CORBA::ULong stack_size, void *(*func)(void *), void *orb_data)

{

MyData *mydata = new MyData; mydata->func = func; mydata->data = orb_data;

VISThread::_create_thread(stack_size, thread_monitor, mydata);

}

int main(int argc, char *const *argv)

{

VISThread::factory(MyThreadFactory); // Replace create thread

// NOTE: It is important to replace the factory before ORB

// or BOA init is done. CORBA::ORB_var orb = CORBA::ORB_init(argc, argv); ..

}

This capability should be used with caution and should be limited wherever possible to adding functionality to monitor thread creation.

Note:

VISThread API is subject to change.

The user supplied _create_thread function should return a thread that is compatible with the native threads, mutexes and condition variables.

The use of this capability may not be supported since this advanced feature is intended for the use of Inprise partners and/or Inprise professional services which have access to internal technical data not available to the general public.

Article originally contributed by