42 int main (int argc, char *argv[])

43 {

44  pthread_t thread_id;

45  char *input, buffer[64];

46  int status;

47

48  status = pthread_create (&thread_id, NULL, thread_routine, NULL);

49  if (status != 0)

50  err_abort (status, 'Create thread');

51  status = pthread_once (&once_block, once_init_routine);

52  if (status != 0)

53  err_abort (status, 'Once init');

54  status = pthread_mutex_lock (&mutex);

55  if (status != 0)

56  err_abort (status, 'Lock mutex');

57  printf ('Main has locked the mutex. ');

58  status = pthread_mutex_unlock (&mutex);

59  if (status != 0)

60  err_abort (status, 'Unlock mutex');

61  status = pthread_join (thread_id, NULL);

62  if (status != 0)

63  err_abort (status, 'Join thread');

64  return 0;

65 }

5.2 Attributes objects

The fifth ls ambition. It next will be right

To describe each particular batch:

Distinguishing those that have feathers, and bite,

From those that have whiskers, and scratch.

Lewis Carroll, The Hunting of the Snark

So far, when we created threads, or dynamically initialized mutexes and condition variables, we have usually used the pointer value NULL as the second argument. That argument is actually a pointer to an attributes object. The value NULL indicates that Pthreads should assume the default value for all attributes— just as it does when statically initializing a mutex or condition variable.

An attributes object is an extended argument list provided when you initialize an object. It allows the main interfaces (for example, pthread_create) to be relatively simple, while allowing 'expert' capability when you need it. Later POSIX standards will be able to add options without requiring source changes to existing code. In addition to standard attributes provided by Pthreads, an implementation can provide specialized options without creating nonstandard parameters.

You can think of an attributes object as a private structure. You read or write the 'members' of the structure by calling special functions, rather than by accessing public member names. For example, you read the stacksize attribute from a thread attributes object by calling pthread_attr_getstacksize, or write it by calling pthread_attr_setstacksize.

In a simple implementation of Pthreads the type pthread_attr_t might be a typedef struct and the get and set functions might be macros to read or write members of the variable. Another implementation might allocate memory when you initialize an attributes object, and it may implement the get and set operations as real functions that perform validity checking.

Threads, mutexes, and condition variables each have their own special attributes object type. Respectively, the types are pthread_attr_t, pthread_ mutexattr_t, and pthread_condattr_t.

5.2.1 Mutex attributes

pthread_mutexattr_t attr;

int pthread_mutexattr_init (pthread_mutexattr_t *attr);

int pthread_mutexattr_destroy (

pthread_mutexattr_t *attr);

#ifdef _POSIX_THREAD_PROCESS_SHARED

int pthread_mutexattr_getpshared (

pthread_mutexattr_t *attr, int *pshared); int pthread_mutexattr_setpshared (

pthread_mutexattr_t *attr, int pshared);

#endif

Pthreads defines the following attributes for mutex creation: pshared, protocol, and prioceiling. No system is required to implement any of these attributes, however, so check the system documentation before using them.

You initialize a mutex attributes object by calling pthread_mutexattr_init, specifying a pointer to a variable of type pthread_mutexattr_t, as in mutex_ attr.c, shown next. You use that attributes object by passing its address to pthread_mutex_init instead of the NULL value we've been using so far.

If your system provides the _POSIX_THREAD_PROCESS_SHARED option, then it supports the pshared attribute, which you can set by calling the function pthread_mutexattr_setpshared. If you set the pshared attribute to the value PTHREAD_PROCESS_SHARED, you can use the mutex to synchronize threads within separate processes that have access to the memory where the mutex (pthread_ mutex_t) is initialized. The default value for this attribute is PTHREAD_PROCESS_PRIVATE.

The mutex_attr.c program shows how to set a mutex attributes object to create a mutex using the pshared attribute. This example uses the default value. PTHREAD_PROCESS_PRIVATE, to avoid the additional complexity of creating shared memory and forking a process. The other mutex attributes, protocol and prioceiling, will be discussed later in Section 5.5.5.

¦ mutex_attr.c

1 #include <pthread.h>

2 #include 'errors.h'

3

4 pthread_mutex_t mutex;

5

6 int main (int argc, char *argv[])

7 {

8  pthread_mutexattr_t mutex_attr;

9  int status;

10

Добавить отзыв
ВСЕ ОТЗЫВЫ О КНИГЕ В ИЗБРАННОЕ

0

Вы можете отметить интересные вам фрагменты текста, которые будут доступны по уникальной ссылке в адресной строке браузера.

Отметить Добавить цитату