alp-ch04-threads

Alp-ch04-threads - 05 0430 CH04 10:21 AM Page 61 4 Threads T HREADS LIKE PROCESSES ARE A MECHANISM TO ALLOW A PROGRAM to do more than one thing at

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

View Full Document Right Arrow Icon
Threads 4 T HREADS , LIKE PROCESSES , ARE A MECHANISM TO ALLOW A PROGRAM to do more than one thing at a time.As with processes, threads appear to run concurrently; the Linux kernel schedules them asynchronously, interrupting each thread from time to time to give others a chance to execute. Conceptually, a thread exists within a process.Threads are a finer-grained unit of execution than processes.When you invoke a program, Linux creates a new process and in that process creates a single thread, which runs the program sequentially.That thread can create additional threads; all these threads run the same program in the same process, but each thread may be executing a different part of the program at any given time. We’ve seen how a program can fork a child process.The child process is initially running its parent’s program, with its parent’s virtual memory, file descriptors, and so on copied.The child process can modify its memory, close file descriptors, and the like without affecting its parent, and vice versa.When a program creates another thread, though, nothing is copied.The creating and the created thread share the same memory space, file descriptors, and other system resources as the original. If one thread changes the value of a variable, for instance, the other thread subsequently will see the modi- fied value. Similarly, if one thread closes a file descriptor, other threads may not read
Background image of page 1

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

View Full DocumentRight Arrow Icon
62 Chapter 4 Threads from or write to that file descriptor. Because a process and all its threads can be exe- cuting only one program at a time, if any thread inside a process calls one of the exec functions, all the other threads are ended (the new program may, of course, create new threads). GNU/Linux implements the POSIX standard thread API (known as pthreads ).All thread functions and data types are declared in the header file <pthread.h> .The pthread functions are not included in the standard C library. Instead, they are in libpthread , so you should add -lpthread to the command line when you link your program. 4.1 Thread Creation Each thread in a process is identified by a thread ID .When referring to thread IDs in C or C++ programs, use the type pthread_t . Upon creation, each thread executes a thread function .This is just an ordinary func- tion and contains the code that the thread should run.When the function returns, the thread exits. On GNU/Linux, thread functions take a single parameter, of type void* , and have a void* return type.The parameter is the thread argument : GNU/Linux passes the value along to the thread without looking at it.Your program can use this parame- ter to pass data to a new thread. Similarly, your program can use the return value to pass data from an exiting thread back to its creator. The
Background image of page 2
Image of page 3
This is the end of the preview. Sign up to access the rest of the document.

This document was uploaded on 08/16/2010.

Page1 / 34

Alp-ch04-threads - 05 0430 CH04 10:21 AM Page 61 4 Threads T HREADS LIKE PROCESSES ARE A MECHANISM TO ALLOW A PROGRAM to do more than one thing at

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

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