HP OpenVMS Systems

ask the wizard
Content starts here

Threads, Objects, and Spawning?

» close window

The Question is:

 
one thread is executing a some C++ functionality.
can that thread spawn another thread that
executes an objects method? If possible can you
give a brief example?  Do you know of any
documentation that I can use to learn more?
 
I appreciate your time.
 
Joe Martorana
 


The Answer is :

 
  Provided that you guarantee that the object's lifetime is valid in the
  other thread, and provided that you interlock any necessary (shared)
  data structures, you should be to do this.  The Wizard would guess
  that the folks maintain object-oriented database software perform
  this task regularly.
 
  The following is some skeleton example code; the Wizard would hope it
  counts as a brief example:
 
  Callee in new thread:
 
    void *
    normal_function_or_non_static_member_function(void * void_handle)
    {
      handle_t * handle = (handle_t *) void_handle;
      (handle->object)->method(handle->args...);
    }
 
  Caller from wherever:
 
    ...
    handle->object = object;
    handle->args = ...;
    // both object and handle must outlive their usage in the other thread
    syscode = pthread_create(&thread_id, NULL,
      normal_function_or_non_static_member_function,
      (void *) handle);
 
	--
 
  As an aside: the Wizard does not believe that there is anything about
  the DEC C++ invocation method (e.g. possible accounting, hinted as one
  possibility in the Alpha Architecture Reference Manual (ARM), and which
  is why it is technically illegal to call methods of NULL objects) that
  might prevent the C++ RTL from interlocking the object.
 

answer written or last revised on ( 29-SEP-1998 )

» close window