本文共 3796 字,大约阅读时间需要 12 分钟。
Thread Control
Introduction
In this chapter, we will learn the details of controlling thread behavior. We will look at thread attributes and synchronization primitive attributes, which we ignored in the previous chapter in favor of the default behavior.
Thread Limits
The Single UNIX Specification defines several limits associated with the operation of threads, which we didn’t show in Figure 2.11.
As with the other limits reported by sysconf, use of these limits is intended to promote application portability among different operating system implementations.
Note that although an implementation may not provide access to these limits, that doesn’t mean that the limits don’t exist. It just means that the implementation doesn’t provide us with a way to get at them using sysconf.
Generally, the functions for managing these attributes follow the same pattern:
1.Each object is associated with its own type of attribute object (threads with thread attributes, mutexes with mutex attributes, and so on).
initialization function exists to set the attributes to their default values. 3.Another function exists to destroy the attributes object. If the initialization function allocated any resources associated with the attributes object, the destroy function frees those resources. 4.Each attribute has a function to get the value of the attribute from the attribute object. Because the function returns 0 on success or an error number on failure, the value is returned to the caller by storing it in the memory location specified by one of the arguments. 5.Each attribute has a function to set the value of the attribute. In this case, the value is passed as an argument, by value.#includeint pthread_attr_init(pthread_attr_t *attr);int pthread_attr_destroy(pthread_attr_t *attr);
#includeint pthread_attr_getdetachstate(const pthread_attr_t *restrict attr, int *detachstate);int pthread_attr_setdetachstate(pthread_attr_t *attr, int detachstate);
#include "apue.h"#includeint makethread(void *(*fn)(void *), void *arg){ int err; pthread_t tid; pthread_attr_t attr; err=pthread_attr_init(&attr); if(err!=0) return err; err=pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED); if(err==0) err=pthread_create(&tid,&attr, fn,arg); pthread_attr_destroy(&attr); return err;}
Creating a thread in the detached state
Support for thread stack attributes is optional for a POSIX-conforming operating system, but is required if the system supports the XSI option in the Single UNIX Specification.
We can manage the stack attributes using the pthread_attr_getstack and pthread_attr_setstack functions.
#includeint pthread_attr_getstack(const pthread_attr_t *restrict attr, void **restrict stackaddr, size_t *restrict stacksize);int pthread_attr_setstack(pthread_attr_t *attr, void *stackaddr, size_t stacksize);
An application can also get and set the stacksize thread attribute using the pthread_attr_getstacksize and pthread_attr_setstacksize functions.
#includeint pthread_attr_getstacksize(const pthread_attr_t *restrict attr, size_t *restrict stacksize);int pthread_attr_setstacksize(pthread_attr_t *attr, size_t stacksize);
The pthread_attr_setstacksize function is useful when you want to change the default stack size but don’t want to deal with allocating the thread stacks on your own. When setting the stacksize attribute, the size we choose can’t be smaller than
PTHREAD_STACK_MIN.#includeint pthread_attr_getguardsize(const pthread_attr_t *restrict attr, size_t *restrict guardsize);int pthread_attr_setguardsize(pthread_attr_t *attr, size_t guardsize);
The Single UNIX Specification defines several other optional thread attributes intended for use by real-time applications.
转载地址:https://blog.csdn.net/myfather103/article/details/79952386 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!