一 x264编码器中也是使用互斥锁来实现多线程之间fpp编码互斥的。
#include <pthread.h>
#include <unistd.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
static pthread_mutex_t mtx = PTHREAD_MUTEX_INITIALIZER;
static pthread_cond_t cond = PTHREAD_COND_INITIALIZER;
static void*thread_func(void*arg)
{ //任务线程
pthread_mutex_lock(&mtx);
pthread_cond_wait(&cond,&mtx); //等待condition ,这里可以释放锁,档条件满足是重新锁上
pthread_mutex_unlock(&mtx);//临界区数据操作完毕,释放互斥锁
}
int main(void)
{ //主线程
pthread_t tid;
int i;
struct node *p;
pthread_create(&tid,NULL, thread_func,NULL);
{
pthread_mutex_lock(&mtx);//需要操作head这个临界资源,先加锁,
pthread_cond_signal(&cond);
pthread_mutex_unlock(&mtx);//解锁
}
}
二 x264中的源码