21 Threads and Libraries

21 Threads and Libraries - !"#$"%& CMSC 216...

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

View Full Document Right Arrow Icon
!"#$"%% % CMSC 216 Introduction to Computer Systems Lecture 22 Concurrent Programming Jan Plane & Pete Keleher {jplane, keleher}@cs.umd.edu Administrivia • Read Chapter 12 on Concurrent Programming • Final Exam Thursday, May 12 4:00-6:00 pm ARM 0126 CMSC 216 - Wood, Sussman, Herman, Plane 2
Background image of page 1

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

View Full DocumentRight Arrow Icon
!"#$"%% # C ONCURRENT P ROGRAMMING ( CONT ) Chapter 12, Bryant and O'Hallaron CMSC 216 - Wood, Sussman, Herman, Plane 3 Semaphore vs. Mutex • Semaphores – counted or binary • Looked at <semaphore.h> • Alternative is mutex – only binary semaphore and defined in pthread.h • mutex = “mutual exclusion” lock CMSC 216 - Wood, Sussman, Herman, Plane 4
Background image of page 2
!"#$"%% ' Pthread Synchronization TO initialize – these are equivalent – pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER; or – pthread_mutex_init(&mutex, NULL); Returns 0 on success (non-zero on failure) • pthread_mutex_lock(*pthread_mutex) – Returns 0 on success (non-zero on failure) • pthread_mutex_unlock(*pthread_mutex) – Returns 0 on success (non-zero on failure) • pthread_mutex_destroy(*pthread_mutex) – Returns 0 on success (non-zero on failure) CMSC 216 - Wood, Sussman, Herman, Plane 5 Mutex Code static int count = 0; static pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER; void *counter(void *args) { int i; for (i = 0; i < LOOPS; i++) { pthread_mutex_lock(&mutex); count++; pthread_mutex_unlock(&mutex); } printf("Executed %d times\n", i); return NULL; } int main() { pthread_t tids[2]; pthread_create(&tids[0], NULL, counter, NULL); pthread_create(&tids[1], NULL, counter, NULL); pthread_join(tids[0], NULL); pthread_join(tids[1], NULL); printf("Count: %d\n", count); return 0; } CMSC 216 - Wood, Sussman, Herman, Plane 6
Background image of page 3

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

View Full DocumentRight Arrow Icon
!"#$"%% ! Thread safety • Thread-safe functions are those that always produce correct results when called by concurrent threads • One class of thread-unsafe functions is those functions that don't protect shared variables • Use of semaphores to protect access to shared variables is one way we can make thread-unsafe functions thread-safe • The fixed example will now report a final count of 20000000, as we expect, but it runs significantly slower CMSC 216 - Wood, Sussman, Herman, Plane 7 Thread safety, cont. Functions that keep state between invocations are also
Background image of page 4
Image of page 5
This is the end of the preview. Sign up to access the rest of the document.

Page1 / 11

21 Threads and Libraries - !&quot;#$&quot;%&amp; CMSC 216...

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

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