Thread attributes

In our initial discussion on Thread Creation earlier in this chapter, we saw the pthread_create(3) API; the second parameter is a pointer to the thread attribute structure: const pthread_attr_t *attr. We mentioned there that passing NULL here, in effect, has the library create a thread with default attributes. While that is indeed the case, the problem is that, for truly portable applications, this is not good enough. Why? Because the default thread attributes actually differ quite widely from implementation to implementation. The right way-specify the thread attributes explicitly at thread creation time. 

Firstly, of course, we need to learn what attributes a pthread has. The following table enumerates this:

Attribute

Meaning

APIs: 

pthread_attr_[...](3)

Values Possible

Linux Default

Detach state

Create threads as joinable or detached

pthread_attr_
[get|set]detachstate

PTHREAD_CREATE_JOINABLE
PTHREAD_CREATE_DETACHED

PTHREAD_CREATE_JOINABLE

Scheduling/contention scope

Set of threads against which we compete for resources (CPU)

pthread_attr_

[get|set]scope

PTHREAD_SCOPE_SYSTEM
PTHREAD_SCOPE_PROCESS

PTHREAD_SCOPE_SYSTEM

Scheduling/inheritance

Determines whether scheduling attributes are inherited implicitly from calling a thread or explicitly from the attr structure

pthread_attr_

[get|set]inheritsched

PTHREAD_INHERIT_SCHED
PTHREAD_EXPLICIT_SCHED

PTHREAD_INHERIT_SCHED

Scheduling/policy

Determines the scheduling policy of the thread being created

pthread_attr_

[get|set]schedpolicy

SCHED_FIFO
SCHED_RR
SCHED_OTHER

SCHED_OTHER

Scheduling/priority

Determines the scheduling priority of the thread being created

pthread_attr_

[get|set]schedparam

struct sched_param holds 
  int sched_priority

0 (non real-time)

Stack/guard region

A guard region for the thread's stack

pthread_attr_

[get|set]guardsize

Stack guard region size in bytes

1 page

Stack/location, size

Query or set the thread's stack location and size

pthread_attr_
[get|set]stack

pthread_attr_
[get|set]stackaddr

pthread_attr_
[get|set]stacksize

Stack address and/or stack size, in bytes

Thread Stack Location: left to the OS

Thread Stack Size: 8 MB

As you can see, clearly understanding what exactly many of these attributes signify requires further information. Please be patient as we proceed through this chapter (and, in fact, this book), as several of these attributes and their meanings will become abundantly clear ( details on scheduling will be shown in Chapter 17, CPU Scheduling on Linux).

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset