6-Threads

6-Threads - Threads and Thread Synchronization Introduction...

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

View Full Document Right Arrow Icon

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

View Full DocumentRight Arrow Icon

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

View Full DocumentRight Arrow Icon

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

View Full DocumentRight Arrow Icon

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

View Full DocumentRight Arrow Icon

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

View Full DocumentRight Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: Threads and Thread Synchronization Introduction to Threads A thread is a path execution By default, a C/C++ program has one thread called "main thread" that starts the main() function. main() --- --- printf( "hello\n" ); --- } Introduction to Threads You can create multiple paths of execution using: POSIX threads ( standard ) pthread_create( &thr_id, attr, func, arg ) Solaris threads thr_create( stack, stack_size, func, arg, flags, &thr_id ) Windows CreateThread(attr, stack_size, func, arg, flags, &thr_id) Introduction to Threads Every thread will have its own Stack PC Program counter Set of registers State Each thread will have its own function calls, and local variables. The process table entry will have a stack, set of registers, and PC for every thread in the process. Multi-threaded Program Example #include <pthread.h> void prstr( char *s ){ while( 1 ){ printf( "%s",s); } } int main(){ // thread 2 pthread_create( NULL, NULL, prstr, "b\n" ); // thread 3 pthread_create(NULL, NULL, prstr, "c\n" ); // thread 1 prstr( "a\n" ); } Multi-threaded Program Example void prstr( char *s ){ while( 1 ){ printf( "%s",s); } } void prstr( char *s ){ while( 1 ){ printf( "%s",s); } } main(): void prstr( char *s ){ while( 1 ){ printf( "%s",s); } } T1 T2 T3 Output: Multi-threaded Program Example Applications of Threads Concurrent Server applications Assume a web server that receives two requests: First, one request from a computer connected through a modem that will take 2 minutes. Then another request from a computer connected to a fast network that will take .01 secs. If the web server is single threaded, the second request will be processed only after 2 minutes. In a multi-threaded server, two threads will be created to process both requests simultaneously. The second request will be processed as soon as it arrives. Application of Threads Taking Advantage of Multiple CPUs A program with only one thread can use only one CPU. If the computer has multiple cores, only one of them will be used. If a program divides the work among multiple threads, the OS will schedule a different thread in each CPU. This will make the program run faster. Applications of Threads Interactive Applications. Threads simplify the implementation of interactive applications that require multiple simultaneous activities. Assume an Internet telephone application with the following threads: Player thread - receives packets from the internet and plays them. Capture Thread captures sound and sends the voice packets Ringer Server Receives incoming requests and tells other phones when the phone is busy....
View Full Document

Page1 / 122

6-Threads - Threads and Thread Synchronization Introduction...

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

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