Answered step by step
Verified Expert Solution
Question
1 Approved Answer
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.
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.
In a Pthreads program, separate threads begin execution in a specified function. In
the code on the next page, this is the runner function.
When this program begins, a single thread of control begins in main
After some initialization, main creates a second thread that begins control in the
runner function. Both threads share the global data sum.
All Pthreads programs must include the pthread.h header file.
The statement pthreadt tid declares the identifier for the thread we will create.
Each thread has a set of attributes, including stack size and scheduling information.
The pthreadattrt attr declaration represents the attributes for the thread.
We set the attributes in the function call pthreadattrinit&attr Because
we did not explicitly set any attributes, we use the default attributes provided.
A separate thread is created with the pthreadcreate 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
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started