{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

alp-ch04-threads

alp-ch04-threads - 05 0430 CH04 10:21 AM Page 61 4 Threads...

Info icon This 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
Image of page 1

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

View Full Document Right 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 pthread_create function creates a new thread.You provide it with the following: 1. A pointer to a pthread_t variable, in which the thread ID of the new thread is stored.
Image of page 2
Image of page 3
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern