linux 下用p,V操作对两线程的操作

  1. 计算/打印线程的同步: 两个线程,共享公共变量a,线程1负责计算(+1),线程2负责打印

我不太理解你问的问题的意图!
公共变量a,在满足什么条件下,两线程的开始分配工作,你没有说清楚
我假设你a==1时生产者工作,
当a==2时,生产完成,
线程2(即消费线程)开始工作即,打印a
那么代码如下:

#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
pthread_mutex_t mutex=PTHREAD_MUTEX_INITIALIZER;
pthread_cond_t cond=PTHREAD_COND_INITIALIZER;
void * add(void*);
void * print(void*);
int a=1;
int main(void)
{
    pthread_t t_a;
    pthread_t t_b;
    pthread_create(&t_a,NULL,add,(void*)NULL);
    pthread_create(&t_b,NULL,print,(void*)NULL);
    pthread_join(t_a,NULL);
    pthread_join(t_b,NULL);
    pthread_mutex_destroy(&mutex);
    pthread_cond_destroy(&cond);
    exit(0);
}
void * add(void *arg)
{
    pthread_mutex_lock(&mutex);
    ++a;
    if(a==2)
        pthread_cond_signal(&cond);
    pthread_mutex_unlock(&mutex);
}
void * print(void *arg)
{
    while(a<2)
    {
        pthread_mutex_lock(&mutex);
        if(a==1)
            pthread_cond_wait(&cond,&mutex);
            printf("a=%d\n",a);
            pthread_mutex_unlock(&mutex);
    }
}

典型的生产者消费者的进程同步问题、、

就是1楼说的代码了,我觉的人家写的挺好的