Question: In this section you will learn how to create threads using the Pthreads ( POSIX standard ) API. 1 . The C program shown on

In this section you will learn how to create threads using the Pthreads (POSIX standard)
API.
1. The C program shown on the next page demonstrates the basic Pthreads API for
constructing a multithreaded program that calculates the summation of a non-
negative integer in a separate thread.
2. In a Pthreads program, separate threads begin execution in a specified function. In
the code on the next page, this is the runner() function.
3. When this program begins, a single thread of control begins in main().
4. After some initialization, main() creates a second thread that begins control in the
runner() function. Both threads share the global data sum.
5. All Pthreads programs must include the pthread.h header file.
6. The statement pthread_t tid declares the identifier for the thread we will create.
Each thread has a set of attributes, including stack size and scheduling information.
The pthread_attr_t attr declaration represents the attributes for the thread.
We set the attributes in the function call pthread_attr_init(&attr). Because
we did not explicitly set any attributes, we use the default attributes provided.
7. A separate thread is created with the pthread_create() function call. In addition
to passing the thread identifier and the attributes for the thread, we also pass the
name of the function where the new thread will begin executionin this case, the
runner() function. Last, we pass the integer parameter that was provided on the
command line, argv[1].

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!