Docsity

Pthreads: Creating and Managing Multithreaded Programs - Prof. Jingke Li, Exams of Computer Science

Toggle sidebar
bg1
Pthreads
Jingke Li
Portland State University
Jingke Li (Portland State University) CS 415/515 Pthreads 1 / 15
Pthreads
The standard thread package on Unix systems. To use it, compile a
program with -lpthread flag.
•Header file: <pthread.h>
•Thread creation and termination:
pthread_create(&tid, NULL, start_fn, arg);
pthread_exit(status);
pthread_cancel(tid);
•Joinable vs. d etached threads:
•When a parent creates a joinable thread, a join would be needed when
the child thread terminates.
pthread join(tid, &status);
•Threads that are not joinable are called detached threads. When
detached threads terminate, they are destroyed and their resource
released.
Jingke Li (Portland State University) CS 415/515 Pthreads 2 / 15
pf3
pf4
pf5
pf8

Partial preview of the text

Download Pthreads: Creating and Managing Multithreaded Programs - Prof. Jingke Li and more Exams Computer Science in PDF only on Docsity!

Pthreads

Jingke Li

Portland State University

Jingke Li (Portland State University) CS 415/515 Pthreads 1 / 15

Pthreads

The standard thread package on Unix systems. To use it, compile a program with -lpthread flag.

  • Header file: <pthread.h>
  • Thread creation and termination: pthread_create(&tid, NULL, start_fn, arg); pthread_exit(status); pthread_cancel(tid);
  • Joinable vs. detached threads:
    • (^) When a parent creates a joinable thread, a join would be needed when the child thread terminates. pthread join(tid, &status);
    • Threads that are not joinable are called detached threads. When detached threads terminate, they are destroyed and their resource released.

Pthreads (cont.)

  • Synchronization support:
    • (^) Locks — direct support via mutex variables.
    • (^) Semaphores — no direct support; but on Unix, could use OS’s semaphore routines
    • (^) Condition variables — direct support.
  • Others: pthread_self(); --- returns a tid pthread_equal(tid1, tid2); --- compare two tids sched_yield(void); --- yield to another thread

Jingke Li (Portland State University) CS 415/515 Pthreads 3 / 15

A Simple Example

#include <pthread.h> void child(int ip) { printf("Child %d\n", ip); } main() { int i=1, j=2; pthread_t thread1, thread2; pthread_create(&thread1, NULL, (void) child, (void)&i); pthread_create(&thread2, NULL, (void) child, (void)&j); printf("Parent: waiting for child 1\n"); pthread_join(thread1, NULL); printf("Parent: waiting for child 2\n"); pthread_join(thread2, NULL); printf("Parent: done\n"); }

Pthread Mutexes (cont.)

  • Only the thread that locks a mutex can unlock it.
  • Pthreads offers a routine that can test whether a lock is locked without blocking the thread — pthread mutex trylock().

It will lock an unlocked mutex and return 0 or will return with EBUSY if the mutex is locked — useful in overcoming deadlock.

  • (^) A mutex can be destroyed with pthread mutex destroy().

Jingke Li (Portland State University) CS 415/515 Pthreads 7 / 15

Pthread Condition Variables

Condition variables are associated with a specific mutex.

  • (^) Declarations and initializations: pthread_cond_t cond1; pthread_mutex_t mutex1; pthread_cond_init(&cond1, NULL); pthread_mutex_init(&mutex1, NULL);

Pthread Condition Variables (cont.)

  • Usage: action() { ... pthread_mutex_lock(&mutex1); while (c<>0) pthread_cond_wait(cond1, mutex1); pthread_mutex_unlock(&mutex1); take_action(); } counter() { ... pthread_mutex_lock(&mutex1); c--; if (c==0) pthread_cond_signal(cond1); pthread_mutex_unlock(&mutex1); }

Signals are not remembered, which means that threads must already be waiting for a signal to receive it.

Jingke Li (Portland State University) CS 415/515 Pthreads 9 / 15

A Condition Variable Example

Producer/consumer with bounded length.

void *producer(void *arg) { request_t *request; while(TRUE) { request = get_request(); pthread_mutex_lock(&req_lock); while (length >= 10) pthread_cond_wait(&req_producer, &req_lock); add_request(request); length++; pthread_mutex_unlock(&req_lock); pthread_cond_signal(&req_consumer); } }

An Extended Example (cont.)

void *slave(void ignored) { / Slave threads / int local_index, partial_sum = 0; do { pthread_mutex_lock(&mutex1); / get next idx into the array / local_index = global_index; / read current idx & save locally / global_index++; / increment global index */ pthread_mutex_unlock(&mutex1); if (local_index < array_size) partial_sum += *(a + local_index); } while (local_index < array_size);

pthread_mutex_lock(&mutex1); /* add partial sum to global sum / sum += partial_sum; pthread_mutex_unlock(&mutex1); return; / Thread exits */ }

Jingke Li (Portland State University) CS 415/515 Pthreads 13 / 15

An Extended Example (cont.)

void main() { int i; pthread_t thread[no_threads]; /* threads / pthread_mutex_init(&mutex1, NULL); / initialize mutex */

for (i=0; i<array_size; i++) /* initialize a[] / a[i] = i + 1; for (i=0; i<no_threads; i++) / create threads / if (pthread_create(&thread[i], NULL, slave, NULL) != 0) perror("Pthread_create fails"); for (i=0; i<no_threads; i++) / join threads */ if (pthread_join(thread[i], NULL) != 0) perror("Pthread_join fails"); printf("The sum of 1 to %i is %d\n", array_size, sum); }

Attributes

Various attributes can be associated with threads, locks, and condition variables.

  • Thread attributes:
    • scheduling parameters
    • stack size
    • detached state
  • Mutex attributes:
    • (^) Normal — only a single thread is allowed to lock it; if a threads tries to lock it twice a deadlock occurs.
    • (^) Recursive — a thread can lock the mutex multiple time; each successive lock increments a counter and each successive release decrements the counter; a thread can lock a mutex only if its counter is zero.
    • Errorcheck — like normal but an attempt to lock it again by the same thread leads to an error.