pthread_create
http://man7.org/linux/man-pages/man3/pthread_create.3.html
pthread_exit
http://man7.org/linux/man-pages/man3/pthread_exit.3.html
pthread_join
http://man7.org/linux/man-pages/man3/pthread_join.3.html
pthread_mutex_init
https://linux.die.net/man/3/pthread_mutex_init
对于static的mutex,可以通过PTHREAD_MUTEX_INITIALIZER进行初始化,否则使用pthread_mutex_init:
"
the macro PTHREAD_MUTEX_INITIALIZER can be used to initialize mutexes that are statically allocated.
The effect shall be equivalent to dynamic initialization by a call to pthread_mutex_init() with
parameter attr specified as NULL, except that no error checks are performed.
"
pthread_mutex_lock
https://linux.die.net/man/3/pthread_mutex_lock
阻塞调用,If the mutex is already locked, the calling thread shall block until the mutex becomes available.
pthread_mutex_trylock 非阻塞调用
https://linux.die.net/man/3/pthread_mutex_trylock
pthread_mutex_unlock
https://linux.die.net/man/3/pthread_mutex_unlock
pthread_mutex_destroy
https://linux.die.net/man/3/pthread_mutex_destroy
pthread_mutexattr_settype
https://linux.die.net/man/3/pthread_mutexattr_settype
pthread_mutexattr_gettype
https://linux.die.net/man/3/pthread_mutexattr_gettype
pthread_cond_init
https://linux.die.net/man/3/pthread_cond_init
pthread_cond_wait
https://linux.die.net/man/3/pthread_cond_wait
pthread_cond_timedwait
https://linux.die.net/man/3/pthread_cond_timedwait
pthread_cond_wait/pthread_cond_timedwait需要和pthread_mutex_lock/unlock配合使用:
进入pthread_cond_wait函数以后,会自动释放mutex,其他的signal线程可以
获得mutex并发出signal(pthread_cond_signal或者pthread_cond_broadcast),在signal线程释放mutex以后,pthread_cond_wait会
解除等待,并自动重新获得mutex.
===========================================================
The pthread_cond_wait() routine always returns with the mutex locked and owned by the calling thread, even when returning an error.
This function blocks until the condition is signaled. It atomically releases the associated mutex lock before blocking,
and atomically acquires it again before returning.
=============================================================
A condition variable allows one thread to wake another up from a wait.
They work only if there is a thread waiting at the moment when you trigger the condition.
The way to ensure that is for the waiting thread to lock a mutex which is
linked to the condition, and for the signalling thread to lock that mutex before triggering the condition.
In other words, the signalling thread can only lock the mutex and trigger the condition if the other
thread had the mutex locked but is now waiting
=============================================================
pthread_cond_signal
https://linux.die.net/man/3/pthread_cond_signal
pthread_cond_broadcast
https://linux.die.net/man/3/pthread_cond_broadcast
pthread_cond_broadcast() causes all threads blocked on the condition to contend again for the mutex lock。
由于pthread_cond_wait会暂时让出mutex锁,这样可以让多个consumer thread同时处于wait状态,当producer thread通过pthread_cond_broadcast发出信号以后,所有的consumer线程会被同时唤醒,但是由于pthread_cond_wait在函数返回之前
要再次获取mutex锁,因此consumer thread会依次获取mutex然后通过pthread_mutex_unlock释放锁,从而保证其他consumer thread可以获取锁。
示例:
pthread_mutex_t rsrc_lock;
pthread_cond_t rsrc_add;
unsigned int resources;
get_resources(int amount)
{
pthread_mutex_lock(&rsrc_lock);
while (resources < amount) {
pthread_cond_wait(&rsrc_add, &rsrc_lock);
}
resources -= amount;
pthread_mutex_unlock(&rsrc_lock);
}
add_resources(int amount)
{
pthread_mutex_lock(&rsrc_lock);
resources += amount;
pthread_cond_broadcast(&rsrc_add);
pthread_mutex_unlock(&rsrc_lock);
}
pthread_attr_init
http://man7.org/linux/man-pages/man3/pthread_attr_init.3.html
pthread_attr_destroy
https://linux.die.net/man/3/pthread_attr_destroy
pthread_attr_setinheritsched
pthread_attr_setschedpolicy
pthread_attr_getschedparam
pthread_attr_setschedparam
pthread_attr_setdetachstate
示例:
int rc = 0;
pthread_attr_t attr;
struct sched_param param;
rc = pthread_attr_init(&attr);
if (rc != 0)
{
fprintf(stderr, "pthread_attr_init() failed, errno=%d\n", errno);
return -1;
}
rc = pthread_attr_setinheritsched(&attr, PTHREAD_EXPLICIT_SCHED);
if (rc != 0)
{
fprintf(stderr, "pthread_attr_setinheritsched() failed, errno=%d\n", errno);
return -1;
}
rc = pthread_attr_setschedpolicy(&attr, SCHED_FIFO);
if (rc != 0)
{
fprintf(stderr, "pthread_attr_setschedpolicy() failed, errno=%d\n", errno);
return -1;
}
rc = pthread_attr_getschedparam(&attr, ¶m);
if (rc != 0)
{
fprintf(stderr, "pthread_attr_getschedparam() failed, errno=%d\n", errno);
return -1;
}
param.sched_priority = 10;
rc = pthread_attr_setschedparam(&attr, ¶m);
if (rc != 0)
{
fprintf(stderr, "pthread_attr_setschedparam() failed, errno=%d\n", errno);
return -1;
}
rc = pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
if (rc != 0)
{
fprintf(stderr, "pthread_attr_setdetachstate() failed, errno=%d\n", errno);
return -1;
}
参考:
https://docs.oracle.com/cd/E19455-01/806-5257/6je9h032r/index.html