threads
play

Threads Thread: an execution within a process A multithreaded - PDF document

Threads Thread: an execution within a process A multithreaded process consists of many Pthreads co-existing executions Separate: CPU state, stack Operating Systems Shared: Hebrew University of Jerusalem Everything


  1. Threads • Thread: an execution within a process • A multithreaded process consists of many Pthreads co-existing executions • Separate: – CPU state, stack Operating Systems • Shared: Hebrew University of Jerusalem – Everything else Spring 2004 • Text, data, heap, environment Theading Models - 2 Threading Models - 1 • User Space (N-1) • Kernel (1-1) – Single kernel process, multiple user threads – All threads are first class objects in the kernel – Low kernel overhead – threads are cheap – Scheduling in and by the kernel – Scheduling is determined by the process – Utilizes multi-processors efficiently – Syscalls do not block the other threads – Syscalls block the whole process (and all the threads) – High overhead for large number of threads – No efficiency on multi-processors State of the Art Threading Models - 3 • Linux • Hybrid (M-on-N) – Pre 2.6: LinuxThreads (1-1 with extras) – User both kernel threads and user threads – 2.6 on: NPTL – Native POSIX Thread Library – More complicated to implement (1-1) • Requires changes to libraries • Scheduling is complicated • User space libraries must be synchronized with • Windows kernel version – Threads, but not POSIX (1-1) �

  2. thread creation How to Compile int pthread_create(pthread_t *thread, • #include <pthread.h> pthread_attr_t *attr, • gcc myprog.c –o myprog –l pthread void* (*start_routine)(void*), void *arg); • Create a thread and run the start_routine • attr is usually NULL, don’t mess with it sched_yield Example #include <pthread.h> #include <sched.h> int val = 0; #include <unistd.h> void *thread(void *vargp) { val = (int)vargp; int sched_yield (void); } int main() { • Yield the processor to another thread int i; pthread_t tid; • Useful on uni-processor pthread_create(&tid, NULL, thread, (void *)42); pthread_join(tid, NULL); printf("%d\n",val); } Relationships Who am I • Marriage: • pthread_t pthread_self(void) – pthread_join – I will wait for you forever • Good bye • Uses: – pthread_exit – I am going away now – Debugging • Death – Data structures indexed by thread – pthread_cancel – please die • Divorce: • pthread_equal: compare two pthread_t – pthread_detach – never talk to me again �

  3. pthread_exit pthread_join • int pthread_join(pthread t, void *data) • void pthread_exit(void *data) • Wait until the thread exits and return the exit data. This call blocks! • Performs a detach after the join succeeds • Stops execution of this thread • Return data to anyone trying to join this • Return values: thread – 0: successful completion – EINVAL: thread is not joinable • Don’t call from the main thread, use exit() – ESRCH: no such thread – EDEADLK: a deadlock was detected, or thread specifies the calling thread pthread_cancel Example #include <pthread.h> • Die you !@$#@ void *thread(void *vargp) { • int pthread_cancel(pthread_t thread) pthread_exit((void*)42); } int main() { • return values: int i; – 0: ok pthread_t tid; pthread_create(&tid, NULL, thread, NULL); – EINVAL: thread is invalid pthread_join(tid, (void **)&i); – ESRCH: no such thread printf("%d\n",i); } pthread_detach pthread_cancel • I never want to see this thread again. • Three phases • int pthread_detach(pthread_t thread) – Post a cancel request – Deliver to the target thread at the next • Return values: cancellation point – 0 - ok – EINVAL – thread is not joinable – Call cleanup routines and die – ESRCH – no such thread • On some systems, exit does not cause the program to exit until all non-detached threads are finished �

  4. Review Review: Exiting threads • pthread_create • Four options • pthread_join – Exit from start routine • pthread_detach – Call pthread_exit • pthread_exit – Call exit • pthread_cancel – Killed by pthread_cancel • pthread_self • pthread_equal • sched_yield �

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend