Add Book to My BookshelfPurchase This Book Online

Chapter 1 - Why Threads

Pthreads Programming
Bradford Nichols, Dick Buttlar and Jacqueline Proulx Farrell
 Copyright © 1996 O'Reilly & Associates, Inc.

Who Am I? Who Are You?
When you create a thread, pthread_create returns a thread handle of type pthread_t. You can save this handle and use it to determine a thread's identity using the pthread_self and pthread_equal function calls. The pthread_self call returns the thread handle of the calling thread and pthread_equal compares two thread handles.* You might use the two calls to identify a thread when it enters a routine, as shown in Example 1-7.
 * The Pthreads standard leaves the exact definition of the pthread_t type up to system implementors. Because a system implementor might define a thread handle to be a structure, you should always use pthread_equal  to compare threads. A direct comparison (such as io_thread == thread) may not work.
Example 1-7: Code that Examines the Identity of the Calling Thread (ident.c)
.
.
.
pthread_t io_thread;
.
.
extern int
main(void)
{
      .
      .
      .
      pthread_create(&io_thread,
                         .... );
      .
      .
      .
}
void routine_x(void)
{
pthread_t thread;
      .
      .
      .
      thread = pthread_self();
      if (pthread_equal(io_thread, thread)) {
      .
      .
      .
      }
      .
      .
      .
}

Previous SectionNext Section
Books24x7.com, Inc © 2000 –  Feedback