"); //-->
一 概述
Pthread是一套通用的线程库, 它广泛的被各种Unix所支持, 是由POSIX提出的. 因此, 它具有很好的可移植性.
例1:
/**//* ------ test.c ------- */
#include <pthread.h>
void *pp(void *arg)
{
while (1)
{
printf("%s\n", (char *)arg);
sleep(2);
}
return NULL;
}
main()
{
pthread_t pid;
pthread_create(&pid, NULL, pp, "hello world");
while (1)
{
printf("I am main thread\n");
sleep(1);
}
}
int x,y;
pthread_mutex_t mut = PTHREAD_MUTEX_INITIALIZER;
pthread_cond_t cond = PTHREAD_COND_INITIALIZER;
//Waiting until X is greater than Y is performed as follows:
pthread_mutex_lock(&mut);
while (x <= y)
{
pthread_cond_wait(&cond, &mut);
}
/**//* operate on x and y */
pthread_mutex_unlock(&mut);
/**//* Key for the thread-specific buffer */
static pthread_key_t buffer_key;
/**//* Once-only initialisation of the key */
static pthread_once_t buffer_key_once = PTHREAD_ONCE_INIT;
/**//* Allocate the thread-specific buffer */
void buffer_alloc(void)
{
pthread_once(&buffer_key_once, buffer_key_alloc);
pthread_setspecific(buffer_key, malloc(100));
}
/**//* Return the thread-specific buffer */
char * get_buffer(void)
{
return (char *) pthread_getspecific(buffer_key);
}
/**//* Allocate the key */
static void buffer_key_alloc()
{
pthread_key_create(&buffer_key, buffer_destroy);
}
/**//* Free the thread-specific buffer */
static void buffer_destroy(void * buf)
{
free(buf);
}
int
pthread_create(
pthread_t *tid , // 用于返回新创建线程的线程号.
const pthread_attr_t *attr ,
void*(*start_routine)(void*) , // start_routine 是线程函数指针,
// 线程从这个函数开始独立地运行。
void *arg // arg 是传递给线程函数的参数。
);
//由于start_routine 是一个指向参数类型为void*,返回值为void*的指针,
//所以如果需要传递或返回多个参数时,可以使用强制类型转化。

void
pthread_exit(
void* value_ptr
);
// 参数value_ptr 是一个指向返回状态值的指针。

int
pthread_join(
pthread_t tid ,
void **status
);
// 参数tid 是希望等待的线程的线程号,status 是指向线程返回值的
//指针,线程的返回值就是pthread_exit 中的value_ptr 参数,或者是return
//语句中的返回值。该函数可用于线程间的同步。
int
pthread_mutex_init(
pthread_mutex_t *mutex,
const pthread_mutex_attr_t* attr
);
// 该函数初始化一个互斥体变量,如果参数attr 为NULL,则互斥
//体变量mutex 使用默认的属性。
int
pthread_mutex_lock(
pthread_mutex_t *mutex
);
// 该函数用来锁住互斥体变量。如果参数mutex 所指的互斥体已经
// 被锁住了,那么发出调用的线程将被阻塞直到其他线程对mutex 解锁。
int
pthread_mutex_trylock(
pthread_t *mutex
);
// 该函数用来锁住mutex 所指定的互斥体,但不阻塞。如果该互斥
//体已经被上锁,该调用不会阻塞等待,而会返回一个错误代码。
int
pthread_mutex_unlock(
pthread_mutex_t *mutex
);
// 该函数用来对一个互斥体解锁。如果当前线程拥有参数mutex 所
// 指定的互斥体,该调用将该互斥体解锁。
int
pthread_mutex_destroy (
pthread_mutex_t *mutex
);
// 该函数用来释放分配给参数mutex 的资源。调用成功时返回值为
//0,否则返回一个非0 的错误代码。

int
pthread_cond_init(
pthread_cond_t *cond,
const pthread_cond_attr_t*attr
);
// 该函数按参数attr指定的属性创建一个条件变量。调用成功返回,
// 并将条件变量ID 赋值给参数cond,否则返回错误代码。
int
pthread_cond_wait (
pthread_cond_t *cond ,
pthread_mutex_t*mutex
);
// 该函数调用为参数mutex 指定的互斥体解锁,等待一个事件(由
// 参数cond 指定的条件变量)发生。调用该函数的线程被阻塞直到有其他
// 线程调用pthread_cond_signal 或pthread_cond_broadcast 函数置相应的条
// 件变量,而且获得mutex 互斥体时才解除阻塞。

int
pthread_cond_timewait(
pthread_cond_t *cond ,
pthread_mutex_t*mutex ,
const struct timespec *abstime
);
// 该函数与pthread_cond_wait 不同的是当系统时间到达abstime 参
// 数指定的时间时,被阻塞线程也可以被唤起继续执行。
int
pthread_cond_broadcast(
pthread_cond_t *cond
);
// 该函数用来对所有等待参数cond所指定的条件变量的线程解除阻
// 塞,调用成功返回0,否则返回错误代码。
int
pthread_cond_signal(
pthread_cond_t *cond
);
// 该函数的作用是解除一个等待参数cond所指定的条件变量的线程
// 的阻塞状态。当有多个线程挂起等待该条件变量,也只唤醒一个线程。
int
pthread_cond_destroy(
pthread_cond_t *cond
);
// 该函数的作用是释放一个条件变量。释放为条件变量cond 所分配的
// 资源。调用成功返回值为0,否则返回错误代码。
int
pthread_key_create(
pthread_key_t key ,
void(*destructor(void*))
);
// 该函数创建一个键值,该键值映射到一个专有数据结构体上。如
//果第二个参数不是NULL,这个键值被删除时将调用这个函数指针来释放
//数据空间。
int
pthread_key_delete(
pthread_key_t *key
);
// 该函数用于删除一个由pthread_key_create 函数调用创建的TSD
//键。调用成功返回值为0,否则返回错误代码。
int
pthread_setspecific(
pthread_key_t key ,
const void(value)
);
// 该函数设置一个线程专有数据的值,赋给由pthread_key_create 创
// 建的TSD键,调用成功返回值为0,否则返回错误代码。

void *
pthread_getspecific(
pthread_key_t *key
);
// 该函数获得绑定到指定TSD 键上的值。调用成功,返回给定参数
//key 所对应的数据。如果没有数据连接到该TSD 键,则返回NULL。
int
pthread_once(
pthread_once_t* once_control,
void(*init_routine)(void)
);
//该函数的作用是确保init_routine指向的函数,在调用pthread_once
//的线程中只被运行一次。once_control 指向一个静态或全局的变量。*博客内容为网友个人发布,仅代表博主个人观点,如有侵权请联系工作人员删除。