/* Copyright (C) 2002,2003,2004,2005,2006,2007 Free Software Foundation, Inc.
This file is part of the GNU C Library.
Contributed by Ulrich Drepper <drepper@redhat.com>, 2002.
The GNU C Library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
The GNU C Library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with the GNU C Library; if not, write to the Free
Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
02111-1307 USA. */
#ifndef _BITS_PTHREADTYPES_H
#define _BITS_PTHREADTYPES_H 1
#include <bits/wordsize.h>
#if __WORDSIZE == 64
# define __SIZEOF_PTHREAD_ATTR_T 56
# define __SIZEOF_PTHREAD_MUTEX_T 40
# define __SIZEOF_PTHREAD_MUTEXATTR_T 4
# define __SIZEOF_PTHREAD_COND_T 48
# define __SIZEOF_PTHREAD_CONDATTR_T 4
# define __SIZEOF_PTHREAD_RWLOCK_T 56
# define __SIZEOF_PTHREAD_RWLOCKATTR_T 8
# define __SIZEOF_PTHREAD_BARRIER_T 32
# define __SIZEOF_PTHREAD_BARRIERATTR_T 4
#else
# define __SIZEOF_PTHREAD_ATTR_T 36
# define __SIZEOF_PTHREAD_MUTEX_T 24
# define __SIZEOF_PTHREAD_MUTEXATTR_T 4
# define __SIZEOF_PTHREAD_COND_T 48
# define __SIZEOF_PTHREAD_CONDATTR_T 4
# define __SIZEOF_PTHREAD_RWLOCK_T 32
# define __SIZEOF_PTHREAD_RWLOCKATTR_T 8
# define __SIZEOF_PTHREAD_BARRIER_T 20
# define __SIZEOF_PTHREAD_BARRIERATTR_T 4
#endif
/* Thread identifiers. The structure of the attribute type is not
exposed on purpose. */
typedef unsigned long int pthread_t;
typedef union
{
char __size[__SIZEOF_PTHREAD_ATTR_T];
long int __align;
} pthread_attr_t;
#if __WORDSIZE == 64
typedef struct __pthread_internal_list
{
struct __pthread_internal_list *__prev;
struct __pthread_internal_list *__next;
} __pthread_list_t;
#else
typedef struct __pthread_internal_slist
{
struct __pthread_internal_slist *__next;
} __pthread_slist_t;
#endif
/* Data structures for mutex handling. The structure of the attribute
type is not exposed on purpose. */
typedef union
{
struct __pthread_mutex_s
{
int __lock;
unsigned int __count;
int __owner;
#if __WORDSIZE == 64
unsigned int __nusers;
#endif
/* KIND must stay at this position in the structure to maintain
binary compatibility. */
int __kind;
#if __WORDSIZE == 64
int __spins;
__pthread_list_t __list;
# define __PTHREAD_MUTEX_HAVE_PREV 1
#else
unsigned int __nusers;
__extension__ union
{
int __spins;
__pthread_slist_t __list;
};
#endif
} __data;
char __size[__SIZEOF_PTHREAD_MUTEX_T];
long int __align;
} pthread_mutex_t;
typedef union
{
char __size[__SIZEOF_PTHREAD_MUTEXATTR_T];
int __align;
} pthread_mutexattr_t;
/* Data structure for conditional variable handling. The structure of
the attribute type is not exposed on purpose. */
typedef union
{
struct
{
int __lock;
unsigned int __futex;
__extension__ unsigned long long int __total_seq;
__extension__ unsigned long long int __wakeup_seq;
__extension__ unsigned long long int __woken_seq;
void *__mutex;
unsigned int __nwaiters;
unsigned int __broadcast_seq;
} __data;
char __size[__SIZEOF_PTHREAD_COND_T];
__extension__ long long int __align;
} pthread_cond_t;
typedef union
{
char __size[__SIZEOF_PTHREAD_CONDATTR_T];
int __align;
} pthread_condattr_t;
/* Keys for thread-specific data */
typedef unsigned int pthread_key_t;
/* Once-only execution */
typedef int pthread_once_t;
#if defined __USE_UNIX98 || defined __USE_XOPEN2K
/* Data structure for read-write lock variable handling. The
structure of the attribute type is not exposed on purpose. */
typedef union
{
# if __WORDSIZE == 64
struct
{
int __lock;
unsigned int __nr_readers;
unsigned int __readers_wakeup;
unsigned int __writer_wakeup;
unsigned int __nr_readers_queued;
unsigned int __nr_writers_queued;
int __writer;
int __shared;
unsigned long int __pad1;
unsigned long int __pad2;
/* FLAGS must stay at this position in the structure to maintain
binary compatibility. */
unsigned int __flags;
} __data;
# else
struct
{
int __lock;
unsigned int __nr_readers;
unsigned int __readers_wakeup;
unsigned int __writer_wakeup;
unsigned int __nr_readers_queued;
unsigned int __nr_writers_queued;
/* FLAGS must stay at this position in the structure to maintain
binary compatibility. */
unsigned char __flags;
unsigned char __shared;
unsigned char __pad1;
unsigned char __pad2;
int __writer;
} __data;
# endif
char __size[__SIZEOF_PTHREAD_RWLOCK_T];
long int __align;
} pthread_rwlock_t;
typedef union
{
char __size[__SIZEOF_PTHREAD_RWLOCKATTR_T];
long int __align;
} pthread_rwlockattr_t;
#endif
#ifdef __USE_XOPEN2K
/* POSIX spinlock data type. */
typedef volatile int pthread_spinlock_t;
/* POSIX barriers data type. The structure of the type is
deliberately not exposed. */
typedef union
{
char __size[__SIZEOF_PTHREAD_BARRIER_T];
long int __align;
} pthread_barrier_t;
typedef union
{
char __size[__SIZEOF_PTHREAD_BARRIERATTR_T];
int __align;
} pthread_barrierattr_t;
#endif
#if __WORDSIZE == 32
/* Extra attributes for the cleanup functions. */
# define __cleanup_fct_attribute __attribute__ ((__regparm__ (1)))
#endif
#endif /* bits/pthreadtypes.h */
分享到:
相关推荐
- 使用`pthread_mutexattr_getprotocol`获取互斥锁类型的属性。 **设置互斥锁属性的协议** - 使用`pthread_mutexattr_setprioceiling`设置互斥锁属性的协议。 **获取互斥锁属性的协议** - 使用`pthread_...
1. 互斥锁(Mutex):`pthread_mutex_t`类型的数据结构,用于保护临界区,防止多个线程同时访问共享资源。 2. 条件变量(Condition Variable):`pthread_cond_t`,配合互斥锁实现线程间的同步等待和唤醒。 3. 读写...
在Pthreads中,我们使用`pthread_mutex_t`类型来表示互斥锁,并通过`pthread_mutex_init()`初始化,`pthread_mutex_lock()`获取锁,`pthread_mutex_unlock()`释放锁。 以下是一个简单的示例,展示了如何在Pthreads...
这个函数接受一个`pthread_t`类型的变量作为线程标识符,一个`pthread_attr_t`结构体指针(可选地指定线程属性,如栈大小和调度策略),一个回调函数(线程入口点),以及一个传递给该函数的参数。 ```c pthread_...
在这个过程中,开发者需要理解并掌握pthread_create()函数用于创建新线程,pthread_join()函数等待线程结束,以及pthread_mutex_t类型用于线程间的同步,防止数据竞争。 接下来是"pbs脚本"。PBS(Portable Batch ...
在Linux中,pthread库提供了`pthread_mutex_t`类型来表示互斥锁,使用`pthread_mutex_init()`初始化,`pthread_mutex_lock()`获取锁,`pthread_mutex_unlock()`释放锁。通过这些函数,可以避免竞态条件,保证线程...
1. **线程标识符类型**:`pthread_t` 是线程的标识符类型,它定义在头文件 `/usr/include/bits/pthreadtypes.h` 中,其定义为 `typedef unsigned long int pthread_t;`。 2. **线程创建**:`pthread_create` 函数...
它需要一个线程标识符(pthread_t类型)来存储新线程的ID,一个函数指针(线程执行的入口点),以及一个指向用户定义的数据结构的指针,该数据结构可以在线程函数中使用。 3. **线程函数**:在`pthread_fab`中,...
至于参数传递,`pthread_create`允许通过`arg`参数向线程函数传递任意类型的数据。你可以封装成结构体或指针,以传递复杂的数据结构。例如: ```cpp struct ThreadArgs { int value1; QString value2; }; void* ...
在main函数中,首先创建了一个pthread_t类型的线程标识符tid。通过pthread_create函数创建一个新线程,该函数的参数依次是线程标识符、线程属性(此处为NULL,即使用默认属性)、指向线程函数的指针以及传递给线程...
- `thread`:指向一个pthread_t类型的指针,用于存储新创建的线程ID。 - `attr`:指向一个pthread_attr_t类型的指针,用于指定新线程的属性。如果为NULL,则使用默认属性。 - `start_routine`:指向新线程执行的...
pthread_mutex_t 类型,其本质是一个结构体。为简化理解,应用时可忽略其实现细节,简单当成整数看待。 pthread_mutex_t mutex; 变量mutex只有两种取值1、0。 pthread_mutex_init函数 初始化一个互斥锁(互斥量) --->...
使用 pthread_setcanceltype 函数可以设置取消类型,原型为: ```c int pthread_setcanceltype(int type, int *oldtype); ``` 其中,type 是取消类型,oldtype 是原来的取消类型。 14. 初始化属性 使用 pthread_...
头文件通常以`pthread.h`为名,它是使用pthread函数的入口,定义了线程创建、同步、通信等功能的函数原型和数据结构。库文件则包含实现了这些函数的代码,通常在编译链接时需要引用。 配置文件可能是指导如何在特定...
`pthread`库是POSIX标准的一部分,提供了创建、同步和管理线程的一系列函数,如`pthread_create`用于创建线程,`pthread_join`用于等待线程结束,以及`pthread_mutex_t`和`pthread_rwlock_t`等用于线程同步的类型。...
- 使用`pthread_setcanceltype()`函数设置取消类型。 - 示例代码: ```c int type = PTHREAD_CANCEL_ASYNCHRONOUS; pthread_setcanceltype(type, NULL); ``` - **创建取消点:** - 可以在代码中插入取消点,...
- **pthread_key_delete()**: 删除一个线程特定数据键,一旦删除,所有线程中与此键相关的数据也将被释放。 #### 三、设置线程特定数据 **3.1 获取线程特定数据** - **pthread_getspecific()**: 从指定的线程特定...
- 使用`pthread_setcanceltype`函数设置线程的取消类型,例如延时取消或立即取消。 **创建取消点** - 在可能的取消点处调用`pthread_testcancel`函数,检查线程是否已被取消。 **将处理程序推送到栈上** - 使用`...
- **设置互斥锁类型的属性**: 使用`pthread_mutexattr_settype`函数设置互斥锁类型。 - **获取互斥锁的类型属性**: 使用`pthread_mutexattr_gettype`函数获取互斥锁类型。 - **设置互斥锁属性的协议**: 使用`pthread...