great info from https://computing.llnl.gov/tutorials/pthreads/
see also: IntelThreadingToolsExamples

3 Major pieces

Thread management

  • create ( pthread_create(thread_returned, attribute_returned, start_routine, args_for_start_routine)
  • destroy ( pthread_exit)
    • Threads die when
      • its starting routine returns
      • it calls pthread_exit
      • another thread cancels it with pthread_cancel
      • process dies
      • Note: if main() terminates before its threads do with pthread_exit(), the other threads keep on going; else they die when main() terminates

  • see PthreadCreateDemo
  • set/get thread attributes ( pthread_attr_init and pthread_attr_destroy)
    • a thread is either joinable (works with pthread_join()) or detached. If it's detached it uses a bit less system resources.
  • pthread_self() - get my system thread ID.
  • pthread_equal() - compares 2 thread IDs.

Mutex (mutual exclusion) variables

  • creating, destroying, locking, unlocking
  • set/get mutex attributes
  • PthreadMutexDemo

Condition variables

  • for comms between threads that share a mutex
  • create, destroy, wait and signal
  • get/set attributes

Naming Conventions

  • pthread_attr_ - thread attribute objects
  • pthread_mutex - duh
  • pthread_mutexattr_ - mutex attribute objects
  • pthead_cond - duh
  • pthead_condattr_ - duh
  • pthread_key_ - thread-specific data keys (?)

Opaque object

  • Used a lot in design of API
  • Basic calls work to create or modify opaque objects, attribute funcs deal with opaque attributes

OS Considerations

  • Threads run as independent entities in OS, like processes, but 'borrow' parent process' resources
  • pthreads_create() around 40-50x faster than fork
  • Careful to make sure libs you use are thread safe
  • compile with -pthread

Semaphores

  • Used for inter-process
Topic revision: r2 - 29 Sep 2008 - 19:09:26 - MattWalsh
 
This site is powered by the TWiki collaboration platformCopyright © by the contributing authors. All material on this collaboration platform is the property of the contributing authors.
Ideas, requests, problems regarding TWiki? Send feedback