Intro to Parallel Computing
Topic 4 – POSIX Threads
COSC 407: Intro to Parallel Computing
Topic 4: POSIX Threads COSC 407: Intro to Parallel Computing
Copyright By PowCoder代写 加微信 powcoder
Previous pre-recorded lecture (Students’ led Q/As):
More on C programming:
– Intro to parallel computing – Intro to POSIX Threads
Today’s topics:
– POSIX Threads – key concepts Next Lecture:
– Intro to OpenMP Topic 4: POSIX Threads
COSC 407: Intro to Parallel Computing
POSIX Threads
§ Key things
– Parallel concepts with pthreads
– Thread management – Synchronization
– Condition variables
Topic 4: POSIX Threads
COSC 407: Intro to Parallel Computing
POSIX Threads
§ Threads associated with a process share resources § Each thread has their own
– Stack (private variables) – Program Counter (PC)
– Registers
– Thread ID
Topic 4: POSIX Threads
COSC 407: Intro to Parallel Computing
Creating Threads
§ To create a thread a pthread_create function is used that needs four arguments
– Thread variable (holds the reference to thread)
– Thread attribute (specifies the minimum stack size to be used)
– Function to call when thread starts
– Arguments to pass to function
pthread_t a_thread;
pthread_attr_t a_thread_attribute; //pthread_attr_default void thread_function(void *argument);
char *some_argument;
pthread_create( &a_thread, a_thread_attribute,
(void *)&thread_function, (void *)&some_argument );
Topic 4: POSIX Threads
COSC 407: Intro to Parallel Computing
Creating Threads
§ Threads begin their execution at the function specified in pthread_create
§ For each thread, we will need to create a thread variable
§ Let’s consider an example with two threads that will print out a message
– Onethreadprints“Hello“ – Onethreadprints“World!’
Topic 4: POSIX Threads COSC 407: Intro to Parallel Computing
Example #1
#include
printf(“%s “, (char*)ptr); return NULL;
int main()
pthread_t thread_1, thread_2;
char *msg1 = “Hello “; char *msg2 = “World!”;
1 – What does this code do? 2 – What is the issue?
• Run the code multiple times…
pthread_create( &thread_1, NULL, say_something, msg1); pthread_create( &thread_2, NULL, say_something, msg2); printf(“Done!”);
fflush(stdout);
exit(0); }
Topic 4: POSIX Threads COSC 407: Intro to Parallel Computing
Challenges with Example #1
§ Threads execute concurrently
– There is no guarantee that the first thread reaches the printf
function prior to the second thread – Output could be
• “World Hello”
• “Hello World“
• “World “ or ” Hello”
• Nothing…. Why?
§ Call to exit made by the parent thread in the main block
– If the parent thread executes the exit call prior to either of the
child threads executing printf, no output will be generated at all.
• The exit function exits the process (releases the task) thus
terminating all threads
– Any thread, parent or child, who calls exit can terminate all the
other threads along with the process
Topic 4: POSIX Threads COSC 407: Intro to Parallel Computing
§ A race condition…
– Exitingbeforethreadsexit
– Timeforthreadstocomplete
§ We want each child thread to finish before the parent thread – Insertadelayintheparentthatwillgivethechildrentime
to reach printf
– Ensurethatthefirstchildthreadreachesprintfbeforethe
– Insertadelaypriortothepthread_createcallthat creates the second thread
Bad Idea… but let’s have a look with example 2
Topic 4: POSIX Threads COSC 407: Intro to Parallel Computing
Example #2
int main() {
pthread_t thread_1, thread_2; char *msg1 = “Hello “;
char *msg2 = “World!”;
pthread_create( &thread_1, NULL, say_something, msg1);
pthread_create( &thread_2, NULL, say_something, msg2);
printf(“Done!”);
fflush(stdout);
Topic 4: POSIX Threads
COSC 407: Intro to Parallel Computing
Issues with Example #2
§ This code doesn’t really meet the objectives
– Not safe to relay on timing delays for synchronization
– Race condition is still present
– Sleep function impacts entire process • Everything is stalled!
• Our program just takes longer to run….
Topic 4: POSIX Threads COSC 407: Intro to Parallel Computing
Allowing Threads to
§ The pthread_join() function waits for the thread specified by thread to terminate
– Ifthatthreadhasalreadyterminated,then pthread_join() returns immediately
int pthread_join(pthread_t thread, void **retval);
https://man7.org/linux/man-pages/man3/pthread_join.3.html
Topic 4: POSIX Threads COSC 407: Intro to Parallel Computing
Example #3
#include
printf(“%s “, (char*)ptr); return NULL;
int main()
pthread_t thread_1, thread_2;
char *msg1 = “Hello “; char *msg2 = “World!”;
pthread_create( &thread_1, NULL, say_something, msg1); pthread_create( &thread_2, NULL, say_something, msg2);
pthread_join(thread_2, NULL); pthread_join(thread_1, NULL);
printf(“Done!”); fflush(stdout); exit(0);
Topic 4: POSIX Threads
§ Problems?
– While this happily waits,
there is no control over
who finishes first!
– How can we sync this so that it works properly??
COSC 407: Intro to Parallel Computing
aka Mutual Exclusion
§ Threads are lacking Synchronization
– i.e.whogetstorun/accessthingsfirst?
§ Thread synchronization can be achieved using a Mutex (Mutual Exclusion)
– Onlyonethreadatatimecanhaveaccesstoashared resource
§ A Mutex is a lock that is set before using a shared resource and release after using it
– Whenthelockisset,nootherthreadcanaccessthelocked region of code (critical code section)
– Ensuressynchronizedaccessofsharedresourcesinthecode
– Canbeusedtoprotectaccesstokeyresources
Topic 4: POSIX Threads COSC 407: Intro to Parallel Computing
aka Mutual Exclusion
§ A mutex is initialized and then a lock is achieved – Initializesamutex
pthread_mutex_init(&lock, NULL); – Achieve/testlockofthecriticalcodesection – Mutexneedstobereleasedwhendone
void* say_something(void *ptr) {
//this now becomes critical section!
pthread_mutex_lock(&lock);
printf(“%s “, (char*)ptr);
pthread_mutex_unlock(&lock);
pthread_exit(0);
– Mutexesneedtobedeleted(destroyed)whendonewiththem Topic 4: POSIX Threads COSC 407: Intro to Parallel Computing
https://www.geeksforgeeks.org/mutex-lock-for-linux-thread-synchronization/
Topic 4: POSIX Threads COSC 407: Intro to Parallel Computing
Example #4
//get a lock
pthread_mutex_t lock;
void* say_something(void *ptr) {
pthread_mutex_lock(&lock);//this now becomes critical section! printf(“%s “, (char*)ptr);
pthread_mutex_unlock(&lock);
pthread_exit(0);
int main() {
pthread_t thread_1, thread_2; char *msg1 = “Hello “;
char *msg2 = “World!”;
//create the lock -> error checking?
Still problems?
• The thread that gets
the lock first, gets to go first
COSC 407: Intro to Parallel Computing
pthread_mutex_init(&lock, NULL);
pthread_create( &thread_1, NULL, say_something, msg1);
pthread_create( &thread_2, NULL, say_something, msg2);
pthread_join(thread_1, NULL); pthread_join(thread_2, NULL); printf(“Done!”); fflush(stdout);
pthread_mutex_destroy(&lock);
exit(0); }
Topic 4: POSIX Threads
Conditions
§ There are many cases where a thread wishes to check whether a condition is true before continuing its execution
§ Not a variable but are used with an associated mutex
§ A condition variable is an explicit queue that threads can put
themselves on when some state of execution (i.e., some condition) is not as desired (by waiting on the condition);
§ When it changes said state, can then wake one (or more) of those waiting threads and thus allow them to continue
pthread_cond_wait(&cond1, &lock);
pthread_cond_signal(&cond1);
Topic 4: POSIX Threads
If the condition is not true, release lock and wait
wake up threads waiting for the condition variable.
COSC 407: Intro to Parallel Computing
void* say_something(void *ptr) {
pthread_mutex_lock(&lock);//this now becomes critical section!
//check on some condition – if it is hello, wait for world….
if (strcmp(”World!”,(char*)ptr) == 0) {
printf(“Waiting on condition variable cond1\n”);
if (done == 0) //only wait in the event that you need to…
printf(“%s “, (char*)ptr); pthread_mutex_unlock(&lock); pthread_exit(0);
pthread_cond_wait(&cond1, &lock);
printf(“Signaling condition variable cond1\n”); done == 1;
pthread_cond_signal(&cond1);
Topic 4: POSIX Threads
COSC 407: Intro to Parallel Computing
Key Functions
§ pthread_create – Create a thread
§ pthread_join
– Waitforthreadtocompete
§ pthread_mutex_init – Create a lock
§ pthread_mutex_lock/pthread_mutex_unlock – Lock and unlock a mutex (if available)
§ pthread_cond_wait – Check on condition
§ pthread_cond_signal
– Signal threads waiting on condition
Topic 4: POSIX Threads COSC 407: Intro to Parallel Computing
Conclusion/Up Next
§ What we covered today (review key concepts): – POSIX Threads – key concepts
– There is a lot of detail here
• Gives a basic Idea of challenges
• Will expand on this with OpenMP
§ Next Lecture: – OpenMP
Topic 4: POSIX Threads COSC 407: Intro to Parallel Computing
§ Please review
– POSIXThreadsProgramming
• https://hpc-tutorials.llnl.gov/posix/ (sections 1 – 8)
– AdditionalresourcesonCanvas
– Havealookattheexamplecodeinthecourserepo(linkon
Topic 4: POSIX Threads COSC 407: Intro to Parallel Computing
程序代写 CS代考 加微信: powcoder QQ: 1823890830 Email: powcoder@163.com