1 / 40

implementation

Pthreads & Concurrency. implementation. Acknowledgements. The material in this tutorial is based in part on: POSIX Threads Programming , by Blaise Barney. History Advantages Content. Pthreads Overview. History. Different versions of threads each hardware vendor created own version

zoltan
Download Presentation

implementation

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Pthreads & Concurrency implementation

  2. Acknowledgements • The material in this tutorial is based in part on: • POSIX Threads Programming , by Blaise Barney

  3. History Advantages Content Pthreads Overview

  4. History • Different versions of threads • each hardware vendor created own version • IEEE POSIX 1003.1c Standard • C programming types and procedure calls

  5. Advantages • Potential program performance gains

  6. Advantages • More efficient inter-thread communication • Threaded applications • Overlap CPU work and I/O • Priority/Real-time scheduling • Asynchronous event handling

  7. The Pthreads API • Thread management • Create, detach, join • Set/query thread attributes • Mutex • Mutual Exclusion • Condition variables • Address communications between threads • Non-mutex synchronization • Read/write locks • Barriers

  8. The Pthreads API • Naming conventions

  9. Create & Terminate • Program: Hello World Threads Management

  10. Creation • Routine • pthread_create( thread, attr, start_routine, arg)

  11. Termination • Four ways for a Pthread to terminate • Returns from start_routine • Call pthread_exit routine • Cancel by another thread through pthread_cancel routine • exit call

  12. Example: Hello World • Program Requirements • Print “Hello World” through standard output • Use two threads to finish the task, one should print “Hello”, the other should print “World” • Terminate all related threads after finishing the printing task

  13. Example: Hello World #include <stdio.h> #include <pthread.h> ……

  14. Example: Hello World void print_message_function( void *ptr ) { char *message; message = (char *) ptr; printf("%s ", message); }

  15. Example: Hello World main() { pthread_t thread1, thread2; char *message1 = "Hello"; char *message2 = "World"; …… }

  16. Example: Hello World main() { pthread_t thread1, thread2; char *message1 = "Hello"; char *message2 = "World"; …… }

  17. Example: Hello World main() { pthread_t thread1, thread2; char *message1 = "Hello"; char *message2 = "World"; …… }

  18. Example: Hello World main() { …… pthread_create(&thread1, pthread_attr_default, (void*)&print_message_function, (void*) message1); pthread_create(&thread2, pthread_attr_default, (void*)&print_message_function, (void*) message2); exit(0); }

  19. Example: Hello World main() { …… pthread_create(&thread1, pthread_attr_default, (void*)&print_message_function, (void*) message1); pthread_create(&thread2, pthread_attr_default, (void*)&print_message_function, (void*) message2); exit(0); }

  20. Example: Hello World main() { …… pthread_create(&thread1, pthread_attr_default, (void*)&print_message_function, (void*) message1); pthread_create(&thread2, pthread_attr_default, (void*)&print_message_function, (void*) message2); exit(0); }

  21. Example: Hello World main() { …… pthread_create(&thread1, pthread_attr_default, (void*)&print_message_function, (void*) message1); pthread_create(&thread2, pthread_attr_default, (void*)&print_message_function, (void*) message2); exit(0); }

  22. Example: Hello World main() { …… pthread_create(&thread1, pthread_attr_default, (void*)&print_message_function, (void*) message1); pthread_create(&thread2, pthread_attr_default, (void*)&print_message_function, (void*) message2); exit(0); }

  23. Example: Hello World main() { …… pthread_create(&thread1, pthread_attr_default, (void*)&print_message_function, (void*) message1); pthread_create(&thread2, pthread_attr_default, (void*)&print_message_function, (void*) message2); exit(0); }

  24. Example: Hello World main() { …… pthread_create(&thread1, pthread_attr_default, (void*)&print_message_function, (void*) message1); pthread_create(&thread2, pthread_attr_default, (void*)&print_message_function, (void*) message2); exit(0); }

  25. Example: Hello World Thread 1 default thread Thread 2

  26. Example: Hello World Thread 1 default thread Thread 2

  27. Example: Hello World Thread 1 default thread Thread 2

  28. Example: Hello World Thread 1 default thread Thread 2

  29. Example: Hello World Thread 1 default thread Thread 2

  30. Example: Hello World Thread 1 default thread Thread 2 Printf Printf

  31. Example: Hello World Thread 1 default thread Thread 2 Printf Exit Printf

  32. Example: Hello World Thread 1 standard output Thread 2 Thread 1 default thread Thread 2 Return

  33. Example: Hello World void print_message_function( void *ptr ) { char *message; message = (char *) ptr; printf("%s ", message); } main() { …… pthread_exit(); //exit(0); }

  34. Example: Hello World void print_message_function( void *ptr ) { char *message; message = (char *) ptr; printf("%s ", message); pthread_exit(); } main() { …… pthread_exit(); //exit(0); }

  35. Passing parameters to threads • The pthread_create() routine permits the programmer to pass one argument to the thread start routine. • For cases where multiple arguments must be passed, this limitation is overcome by creating a structure which contains all of the arguments, and then passing a pointer to that structure. • All arguments must be passed by reference and cast to (void *).

  36. Joining and detaching threads • pthread_join(threadid,status) • blocks the calling thread until the specified threadid thread terminates. • target thread's termination return status placed in status if specified in the target thread's call to pthread_exit(). • A joining thread can match one pthread_join() call. • It is a logical error to attempt multiple joins on the same thread. • pthread_detach(threadid, status) • to explicitly detach a thread • pthread_attr_setdetachstate(attr,detachstate) • pthread_attr_getdetachstate(attr, detachstate)

  37. Create & Destroy • Lock & Unlock • Program: Bounded Buffer Mutex

  38. Mutex • Mutual Exclusion • Act as a “lock” to prevent race conditions • Typical sequence of use • create, initialize • lock, unlock • destroy • Losers in the competition for the mutex variable have to block

  39. Creating & Destroying • Type: pthread_mutex_t • Initializing • Statically • pthread_mutex_t mymutex = PTHREAD_MUTEX_INITIALIZER • Dynamically • pthread_mutex_init (mutex, attr) • pthread_mutex_attr_tattr specifies protocol for priorities, priority ceiling, and process sharing • Destroying • pthread_mutex_destroy – to delete a mutex object • pthread_mutex_attr_destroy – to delete a mutexattr object

  40. Lock & Unlock • Lock • pthread_mutex_lock (mutex) • blocks calling thread if mutex already locked • pthread_mutex_trylock(mutex) • returns with “busy” code if already locked • Unlock • pthread_mutex_unlock (mutex) • Error • mutex is released • mutex owned by others

More Related