Книга: Programming with POSIX® Threads

9.3.3 Threads

9.3.3 Threads

Threads provide concurrency, the ability to have more than one "stream of execution" within a process at the same time. Each thread has its own hardware registers and stack. All threads in a process share the full virtual address space, plus all file descriptors, signal actions, and other process resources.

pthread_attr_destroy

int pthread_attr_destroy (

pthread_attr_t *attr);

Destroy a thread attributes object. The object can no longer be used.

References: 2, 5.2.3

Headers: <pthread.h>

Errors: [EINVAL]attr is invalid.

Hint: Does not affect threads created using attr.

pthread_attr_getdetachstate

int pthread_attr_getdetachstate (

const pthread_attr_t *attr, int *detachstate);

Determine whether threads created with attr will run detached

detachstate

PTHREAD CREATE JOINABLE

PTHREAD CREATE DETACHED

Thread ID is valid, must be joined.

Thread ID is invalid, cannot be joined, canceled, or modified.

References: 2, 5.2.3

Headers: <pthread.h>

Errors: [EINVAL] attr is invalid.

Hint: You can't join or cancel detached threads.

pthread_attr_getstackaddr.....................................................[_POSIX_THREAD_ATTR_STACKADDR]

int pthread_attr_getstackaddr (

const pthread_attr_t *attr,

void **stackaddr);

Determine the address of the stack on which threads created with attr will run.

References: 2, 5.2.3

Headers: <pthread.h>

Errors: [EINVAL]attrisinvalid.

[ENOSYS] stacksize not supported. Hint: Create only one thread for each stack address!

pthread_attr_getstacksize.......................................................[_POSIX_THREAD_ATTR_STACKSIZE]

int pthread_attr_getstacksize (

const pthread_attr_t *attr, size_t *stacksize);

Determine the size of the stack on which threads created with attr will run.

References: 2, 5.2.3

Headers: <pthread.h>

Errors: [EINVAL]attrinvalid.

[ENOSYS] stacksize not supported. Hint: Use on newly created attributes object to find the default stack size.

pthread_attr_init

int pthread_attr_init (

pthread_attr_t *attr);

Initialize a thread attributes object with default attributes.

References: 2, 5.2.3

Headers: <pthread.h>

Errors: [ENOMEM] insufficient memory for attr.

Hint: Use to define thread types.

| pthread_aftr_setdetachstate

int pthread_attr_setdetachstate ( pthread_attr_t *attr, int detachstate);

Specify whether threads created with attr will run detached.

detachstate

PTHREAD CREATE JOINABLE

PTHREAD CREATE DETACHED

Thread ID is valid, must be joined.

Thread ID is invalid, cannot be joined, canceled, or modified.

References: 2, 5.2.3

Headers: <pthread.h>

Errors: [EINVAL]attrinvalid.

[EINVAL] detachstate invalid. Hint: You can't join or cancel detached threads.

pthread_attr_setstackaddr.....................................................[_POSIX_THREAD_ATTR_STACKADDR]

int pthread_attr_setstackaddr ( pthread_attr_t *attr, void *stackaddr);

Threads created with attr will run on the stack starting at stackaddr. Must be at least PTHREAD_STACK_MIN bytes.

References: 2, 5.2.3

Headers: <pthread.h>

Errors: [EINVAL]attrinvalid.

[ENOSYS] stackaddr not supported. Hint: Create only one thread for each stack address, and be careful of

stack alignment.

pthread_attr_setstacksize.......................................................[_POSIX_THREAD_ATTR_STACKSIZE]

int pthread_attr_setstacksize ( pthread_attr_t *attr, size_t stacksize);

Threads created with attr will run on a stack of at least stacksize bytes. Must be at least PTHREAD_STACK_MIN bytes.

References: 2, 5.2.3

<pthread.h>

[EINVAL] attr or stacksize invalid. [EINVAL] stacksize too small or too big. [ENOSYS] stacksize not supported.

Find the default first (pthread_attr_getstacksize), then increase by multiplying. Use only if a thread needs more than the default.

Headers: Errors:

Hint:

pthread_create

int pthread_create (

pthread_t *tid,

const pthread_attr_t *attr,

void *(*start) (void *),

void *arg);

Create a thread running the start function, essentially an asynchronous call to the function start with argument value arg. The attr argument specifies optional creation attributes, and the identification of the new thread is returned in tid.

References: 2, 5.2.3

Headers: <pthread.h>

Errors: [EINVAL] attr invalid.

[EAGAIN] insufficient resources. Hint: All resources needed by thread must already be initialized.

pthread_detach

int pthread_detach (

pthread_t thread);

Detach the thread. Use this to detach the main thread or to "change your mind" after creating a joinable thread in which you are no longer interested.

References: 2, 5.2.3 Headers: <pthread.h>

Errors: [EINVAL] thread is not a joinable thread.

[ESRCH] no thread could be found for ID thread. Hint: Detached threads cannot be joined or canceled; storage is freed

immediately on termination.

pthread_equal

int pthread_equal (

pthread_t tl,

pthread_t t2);

Return value 0 if t1 and t2 are equal, otherwise return nonzero.

References: 2, 5.2.3 Headers: <pthread.h>

Hint: Compare pthread_self against stored thread identifier.

pthread_exit

int pthread_exit (

void *value_ptr);

Terminate the calling thread, returning the value value_ptr to any joining thread.

References: 2, 5.2.3 Headers: <pthread.h>

Hint: value ptr is treated as a value, not the address of a value.

pthread_join

int pthread_join (

pthread_t thread,

void **value_ptr);

Wait for thread to terminate, and return thread's exit value if value_ptr is not NULL. This also detaches thread on successful completion.

References: 2, 5.2.3 Headers: <pthread.h>

Errors: [EINVAL] thread is not a joinable thread.

[ESRCH] no thread could be found for ID thread.

[EDEADLK] attempt to join with self. Hint: Detached threads cannot be joined or canceled.

pthread_self

pthread_t pthread_self (void);

Return the calling thread's ID.

References: 2, 5.2.3 Headers: <pthread.h>

Hint: Use to set thread's scheduling parameters.

sched_yield

int sched_yield (void);

Make the calling thread ready, after other ready threads of the same priority, and select a new thread to run. This can allow cooperating threads of the same priority to share processor resources more equitably, especially on a uniprocessor. This function is from POSIX.1b (realtime extensions), and is declared in <sched.h>. It reports errors by setting the return value to -1 and storing an error code in errno.

References: 2, 5.2.3 Headers: <sched.h>

Errors: [ENOSYS] sched_yield not supported.

Hint: Use before locking mutex to reduce chances of a timeslice while mu-

tex is locked.

Оглавление книги


Генерация: 0.041. Запросов К БД/Cache: 0 / 0
поделиться
Вверх Вниз