Note: Multithreaded Programming
30 Jan 2014####Creating POSIX Threads
pthread_create
- returns a positive value indicating failure
####Structure pointer approach
P46
####Passing arguments to threads
- copy all arguments to the thread’s stack: isn’t supported in either POSIX or Win-32
- pass a pointer to local storage: should make sure this storage doesn’t go out of scope until the thread is finished with it
- pass a pointer to static or global storage: works only if one thread at a time is using the storage
- pass a pointer to dynamically allocated storage: works only if we can free the storage exactly when the thread is finished with it
####Threads terminate
- 2 ways:
- return from its first procedure(a value of type void*)
- call pthread_exit, supplying a void* argument
- difference:
- pthread_exit: terminates just the calling thread
- exit: terminates the entire process, including other threads in the process
####Deadlock conditions
- a thread can hold an item while waiting for another
- a thread cannot be forced toyield the items it is holding
- each container has a finite capacity
- a circular wait
####Semaphore
- binary semaphores
- counting semaphres
####Conditional variables
- P67: why not
if --writers == 0
- P70: Now when the waiting threads wake up, they will find the guard false….
####async-signal
- fork
- _exit
- open
- close
- read
- write
- sem_post
####Question
- POSIX cancellation
####Bear in mind
- it’s unsafe to make more than 1 call to pthread_join on any particular thread
- for each call to pthread_create there should be exactly one call to either pthread_join or pthread_detach.
- pthread_cleanup_push must match exactly with pthread_cleanup_pop
####Notes during db assignment
- It’s a common(if standard) approach to mask the signal you want to handle in the main thread, then create a handler thread, specifically unblocking the signal and waiting for the signal, to handle that signal.
- If you did not
pthread_detach
the thread, then you’ll have topthread_join
the thread, or the resources allocated by the thread will not be collected. - Signal is not that hard at all, just need to figure out the current state.
- If you cherish your life, stay away from
pthread_detach
!!!
Your Comments
comments powered by Disqus