简介
pthread_cond_t是一个条件变量的数据类型,用于线程间的同步和通信。它通常与互斥锁(pthread_mutex_t)一起使用,以实现线程的等待和唤醒操作。
以下是两个与pthread_cond_t相关的函数的介绍:
pthread_cond_init:用于初始化条件变量。它接受两个参数,第一个参数是指向条件变量的指针,第二个参数是指向条件变量属性的指针。如果不需要特殊属性,可以将第二个参数设置为NULL。该函数在成功时返回0,否则返回错误代码。
pthread_cond_destroy:用于销毁条件变量。它接受一个参数,即指向条件变量的指针。该函数在成功时返回0,否则返回错误代码。
条件变量相关函数
int pthread_cond_init(pthread_cond_t *cv, const pthread_condattr_t *a);
int pthread_cond_destroy(pthread_cond_t *cv);
int pthread_cond_signal (pthread_cond_t *cv);
int pthread_cond_broadcast (pthread_cond_t *cv);
int pthread_cond_wait (pthread_cond_t *cv, pthread_mutex_t *external_mutex);
int pthread_cond_timedwait(pthread_cond_t *cv, pthread_mutex_t *external_mutex, const struct timespec *t);
int pthread_cond_timedwait_relative_np(pthread_cond_t *cv, pthread_mutex_t *external_mutex, const struct timespec *t);
互斥锁相关函数
int pthread_mutex_lock(pthread_mutex_t *m);
int pthread_mutex_timedlock(pthread_mutex_t *m, const struct timespec *ts);
int pthread_mutex_unlock(pthread_mutex_t *m);
int pthread_mutex_trylock(pthread_mutex_t *m);
int pthread_mutex_init(pthread_mutex_t *m, const pthread_mutexattr_t *a);
int pthread_mutex_destroy(pthread_mutex_t *m);
测试源码:
#include <sys/epoll.h>
#include <fcntl.h>
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <fcntl.h>
#include <errno.h>
#include <pthread.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#define _DEBUG_INFO
#ifdef _DEBUG_INFO
#define DEBUG_INFO(format, ...) printf("%s:%s:%d -- "format"\n" \
,__FILE__,__func__,__LINE__ \
, ##__VA_ARGS__)
#else
#define DEBUG_INFO(format, ...)
#endif
struct my_struct {
pthread_cond_t cond;
pthread_mutex_t mutex;
int run;
};
void * my_func1(void * arg){
struct my_struct *p = (struct my_struct *)arg;
pthread_mutex_lock(&p->mutex);
DEBUG_INFO("Main thread waking up the other thread...\n");
pthread_cond_signal(&p->cond);
DEBUG_INFO("I want to sleep");
sleep(1);
DEBUG_INFO("Are you waiting me?Yes,You must wait");
pthread_mutex_unlock(&p->mutex);
DEBUG_INFO("task 1 exit");
return NULL;
}
void * my_func2(void * arg){
struct my_struct *p = (struct my_struct *)arg;
pthread_mutex_lock(&p->mutex);
DEBUG_INFO("Thread waiting...\n");
pthread_cond_wait(&p->cond, &p->mutex);
DEBUG_INFO("Thread woken up!\n");
pthread_mutex_unlock(&p->mutex);
p->run = 0;
return NULL;
}
int main(int argc, char *argv[]){
pthread_t p1,p2;
int ret;
struct my_struct *p = malloc(sizeof(struct my_struct));
if(p == NULL){
return NULL;
}
pthread_cond_init(&p->cond,NULL);
pthread_mutex_init(&p->mutex,NULL);
p->run = 1;
ret = pthread_create(&p1,NULL,my_func1,p);
if(ret < 0){
perror("pthread_create");
DEBUG_INFO("pthread_create");
return -1;
}
pthread_detach(p1);
ret = pthread_create(&p2,NULL,my_func2,p);
if(ret < 0){
perror("pthread_create");
DEBUG_INFO("pthread_create");
return -1;
}
pthread_detach(p2);
while (p->run)
{
sleep(1);
}
DEBUG_INFO("bye bye");
return 0;
}
CMakeLists.txt
project(libevent_project)
cmake_minimum_required(VERSION 3.8)
message(STATUS "lkmao:CMAKE_SOURCE_DIR -- ${CMAKE_SOURCE_DIR}")
SET(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -pthread -g -Wall")
SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11 -pthread -lstdc++")
# add_executable(app epoll.c)
# add_executable(app epoll2.c)
add_executable(app cond.c)
编译脚本:
#!/bin/bash
set -e
rm -rf _build_
mkdir _build_ -p
cmake -S ./ -B _build_
make -C _build_
# ./_build_/main_01
# ./_build_/app
echo ""
file ./_build_/app
echo ""
./_build_/app hello.txt
小结
注意:一定要保证 pthread_cond_wait在函数pthread_cond_signal之前运行。否则可能接收到不到信号,造成死锁。例如将my_func1中的sleep(1)放到线程2中,如下图所示:
执行结果如下所示: