ics491_pthreads - Henri Casanova([email protected] ICS491...

Info iconThis preview shows pages 1–3. Sign up to view the full content.

View Full Document Right Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: Henri Casanova ([email protected]) ICS491 - Spring 2007 Concurrent and High-Performance Programming Multi-threading in C with Pthreads Pthreads: POSIX Threads ! Pthreads is a standard set of C library functions for multithreaded programming " IEEE Portable Operating System Interface, POSIX, section 1003.1 standard, 1995 ! Pthread Library (60+ functions) " Thread management: create, exit, detach, join, . . . " Thread cancellation " Mutex locks: init, destroy, lock, unlock, . . . " Condition variables: init, destroy, wait, timed wait, . . . " . . . ! Programs must include the file pthread.h ! Programs must be linked with the pthread library (- lpthread ) Pthreads Naming Convention ! Types: pthread [_object] _t ! Functions: pthread [_object ]_action ! Constants/Macros: PTHREAD_ PURPOSE ! Examples: " pthread_t: the type of a thread " pthread_create(): creates a thread " pthread_mutex_t: the type of a mutex lock " pthread_mutex_lock(): lock a mutex " PTHREAD_CREATE_DETACHED pthread_self() ! Returns the thread identifier for the calling thread " At any point in its instruction stream a thread can figure out which thread it is " Convenient to be able to write code that says: “If you’re thread 1 do this, otherwise to that” " However, the thread identifier is an opaque object (just a pthread_t value) " you must use pthread_equal() to test equality pthread_t pthread_self (void); int pthread_equal (pthread_t id1, pthread_t id2); pthread_create() ! Creates a new thread int pthread_create ( pthread_t *thread, pthread_attr_t *attr, void * (*start_routine) (void *) , void *arg); " Returns 0 to indicate success, otherwise returns error code " thread : output argument for the id of the new thread " attr : input argument that specifies the attributes of the thread to be created (NULL = default attributes) " start_routine: function to use as the start of the new thread ! must have prototype: void * foo(void*) " arg: argument to pass to the new thread routine ! If the thread routine requires multiple arguments, they must be passed bundled up in an array or a structure pthread_create() example ! Want to create a thread to compute the sum of the elements of an array void *do_work(void *arg); ! Needs three arguments " the array, its size, where to store the sum " we need to bundle them in a structure struct arguments { double *array; int size; double *sum; } pthread_create() example int main(int argc, char *argv) { double array[100]; double sum; pthread_t worker_thread; struct arguments *arg; arg = (struct arguments *)calloc(1, sizeof(struct arguments)); arg->array = array; arg->size=100; arg->sum = ∑ if ( pthread_create (&worker_thread, NULL, do_work, (void *)arg)) { fprintf(stderr,”Error while creating thread\n”); exit(1); } ......
View Full Document

This note was uploaded on 07/16/2009 for the course SYSC 3303 taught by Professor Shramp during the Spring '09 term at Carleton CA.

Page1 / 9

ics491_pthreads - Henri Casanova([email protected] ICS491...

This preview shows document pages 1 - 3. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online