Linux条件变量
- 1. 头文件
- 2. 类型
-
- 3. 接口
-
- 3.1. 条件变量接口
-
- 3.1.1 动态初始化资源
- 3.1.2. 动态释放资源
- 3.1.3. 条件变量阻塞
- 3.1.4. 带超时时间的条件变量阻塞
- 3.1.5. 唤醒单个阻塞线程
- 3.1.6. 唤醒所有被阻塞的线程
- 3.2. 条件变量属性接口
-
- 3.2.1. 属性初始化
- 3.2.2. 属性释放
- 3.2.3. 修改属性
- 3.2.4. 查询属性
- 3.2.5. 获取设置超时时间所用的时钟ID
- 3.2.6. 设置超时时间所用的时钟ID
- 4. 示例
-
1. 头文件
#include
2. 类型
2.1. 类型值
enum
{
PTHREAD_PROCESS_PRIVATE,
PTHREAD_PROCESS_SHARED,
};
2.2. 条件变量和属性类型
pthread_cond_t tmpCond1 = PTHREAD_COND_INITIALIZER;
pthread_cond_t tmpCond2;
pthread_condattr_t tmpCondattr;
3. 接口
3.1. 条件变量接口
3.1.1 动态初始化资源
int pthread_cond_init (pthread_cond_t *__restrict __cond, const pthread_condattr_t *__restrict __cond_attr);
3.1.2. 动态释放资源
int pthread_cond_destroy (pthread_cond_t *__cond);
3.1.3. 条件变量阻塞
int pthread_cond_wait (pthread_cond_t *__restrict __cond, pthread_mutex_t *__restrict __mutex);
3.1.4. 带超时时间的条件变量阻塞
int pthread_cond_timedwait (pthread_cond_t *__restrict __cond, pthread_mutex_t *__restrict __mutex, const struct timespec *__restrict __abstime);
3.1.5. 唤醒单个阻塞线程
int pthread_cond_signal (pthread_cond_t *__cond);
3.1.6. 唤醒所有被阻塞的线程
int pthread_cond_broadcast (pthread_cond_t *__cond);
3.2. 条件变量属性接口
3.2.1. 属性初始化
int pthread_condattr_init (pthread_condattr_t *__attr);
3.2.2. 属性释放
int pthread_condattr_destroy (pthread_condattr_t *__attr);
3.2.3. 修改属性
int pthread_condattr_setpshared (pthread_condattr_t *__attr, int __pshared);
3.2.4. 查询属性
int pthread_condattr_getpshared (pthread_condattr_t *__attr, int *__restrict __pshared);
3.2.5. 获取设置超时时间所用的时钟ID
int pthread_condattr_getclock (const pthread_condattr_t * __restrict __attr, __clockid_t *__restrict __clock_id);
3.2.6. 设置超时时间所用的时钟ID
int pthread_condattr_setclock (pthread_condattr_t *__attr, __clockid_t __clock_id);
4. 示例
4.1. 条件变量示例
#include
#include
#include
#include
#include
pthread_cond_t g_cond;
pthread_condattr_t g_condattr;
pthread_mutex_t g_mutex;
int g_iValue = 0;
void init_cond()
{
pthread_condattr_init(&g_condattr);
pthread_cond_init(&g_cond, &g_condattr);
pthread_mutex_init(&g_mutex, nullptr);
}
void destroy_cond()
{
pthread_condattr_destroy(&g_condattr);
pthread_cond_destroy(&g_cond);
pthread_mutex_destroy(&g_mutex);
}
static void* condWaitThread(void *arg)
{
std::cout << "start condWaitThread." << std::endl;
pthread_mutex_lock(&g_mutex);
pthread_cond_wait(&g_cond, &g_mutex);
std::cout << "continue condWaitThread." << std::endl;
g_iValue += 1;
std::cout << "g_iValue is " << g_iValue << std::endl;
pthread_mutex_unlock(&g_mutex);
std::cout << "over condWaitThread." << std::endl;
return nullptr;
}
static void* condTimewaitThread(void *arg)
{
std::cout << "start condTimewaitThread." << std::endl;
struct timespec struTimeout = {0};
struct timespec struCurrTime = {0};
clock_gettime(CLOCK_REALTIME, &struCurrTime);
struTimeout.tv_sec = struCurrTime.tv_sec + 5;
struTimeout.tv_nsec = struCurrTime.tv_nsec;
pthread_mutex_lock(&g_mutex);
int iret = pthread_cond_timedwait(&g_cond, &g_mutex, &struTimeout);
if (0 == iret)
{
std::cout << "weak up continue condTimewaitThread." << std::endl;
}
else
{
std::cout << "timeout continue condTimewaitThread." << std::endl;
}
g_iValue += 1;
std::cout << "g_iValue is " << g_iValue << std::endl;
pthread_mutex_unlock(&g_mutex);
std::cout << "over condTimewaitThread." << std::endl;
return nullptr;
}
int main()
{
init_cond();
pthread_t threadWait, threadTimewait;
pthread_create(&threadWait, nullptr, condWaitThread, nullptr);
pthread_detach(threadWait);
pthread_create(&threadTimewait, nullptr, condTimewaitThread, nullptr);
pthread_detach(threadTimewait);
sleep(1);
pthread_cond_signal(&g_cond);
sleep(5);
pthread_cond_broadcast(&g_cond);
destroy_cond();
return 0;
}