1 / 17

Introduction to Threads

Introduction to Threads. CS240 Programming in C. Introduction to Threads. A thread is a path execution By default, a C/C++ program has one thread called &quot;main thread&quot; that starts the main() function. main() --- --- printf( &quot;hello<br>&quot; ); --- }. Introduction to Threads.

arty
Download Presentation

Introduction to Threads

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. Introduction to Threads CS240 Programming in C

  2. 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" ); --- }

  3. 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)

  4. Introduction to Threads Every thread will have its own Stack PC – Program counter Set of registers 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.

  5. 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 1prstr( "a\n" ); }

  6. Multi-threaded Program Example void prstr( char *s ){ while( 1 ){ printf( "%s",s);} } void prstr( char *s ){ while( 1 ){ printf( "%s",s);} } T2 T3 main(): void prstr( char *s ){ while( 1 ){ printf( "%s",s);} } T1

  7. Multi-threaded Program Example Output:

  8. 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.

  9. 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.

  10. 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. Having a single thread doing all this makes the code cumbersome and difficult to read.

  11. Advantages and Disadvantages of Threads vs. Processes Advantages of Threads Fast thread creation - creating a new path of execution is faster than creating a new process with a new virtual memory address space and open file table. Fast context switch - context switching across threads is faster than across processes. Fast communication across threads – threads communicate using global variables that is faster and easier than processes communicating through pipes or files.

  12. Advantages and Disadvantages of Threads vs. Processes Disadvantages of Threads Threads are less robust than processes – If one thread crashes due to a bug in the code, the entire application will go down. If an application is implemented with multiple processes, if one process goes down, the other ones remain running. Threads have more synchronization problems – Since threads modify the same global variables at the same time, they may corrupt the data structures. Synchronization through mutex locks and semaphores is needed for that. Processes do not have that problem because each of them have their own copy of the variables.

  13. Synchronization Problems with Multiple Threads Threads share same global variables. Multiple threads can modify the same data structures at the same time This can corrupt the data structures of the program. Even the most simple operations, like increasing a counter, may have problems when running multiple threads.

  14. Threads in your Talk Client • In your Talk-Client program in lab6 you would like to read characters at the same time messages are received. • To accomplish this you will have the main() thread read programs from the keyboard and a new thread will loop to receive messages from the talk-server.

  15. Threads in your Talk Client void * getMessagesThread(void * arg) { // This code will be executed simultaneously with main() // Get messages to get last message number. Discard the initial Messages while (1) { // Get messages after last message number received. // Print messages // Sleep for ten seconds usleep(2*1000*1000); } } void startGetMessageThread() { pthread_create(NULL, NULL, getMessagesThread, NULL); }

  16. Threads in your Talk Client int main(int argc, char **argv) { // Initialize client // Start message thread to receive messages startGetMessageThread(); while (1) { printPrompt(); char * s = gets(line); // Process commanda } } Both main() and getMessagesThread() will execute simultaneously (concurrently).

  17. Questions?

More Related