pthread_cond_t

#include
#include
#include
#include
#include

typedef struct ShareInt
{
int x;
pthread_mutex_t lock;
pthread_cond_t cond1;
pthread_cond_t cond2;

}Int;

void* Thread1(void *pv);
void *Thread2(void *pv);
int main()
{
pthread_t tid1;
pthread_t tid2;

Int st={0};
int ret=0;

pthread_mutex_init(&st.lock,NULL);//参数attr指定了新建互斥锁的属性
pthread_cond_init(&st.cond1,NULL);
pthread_cond_init(&st.cond2,NULL);//参数cattr为空指针等价于cattr中的属性为缺省属性,

ret=pthread_create(&tid1,NULL,Thread1,(void*)&st);
ret+=pthread_create(&tid2,NULL,Thread2,(void*)&st);
if(ret<0)
{
	printf("create failed");
	
}

pthread_join(tid1,NULL);
pthread_join(tid2,NULL);

printf("\n");
pthread_mutex_destroy(&st.lock);
pthread_cond_destroy(&st.cond1);
pthread_cond_destroy(&st.cond2);
return 0;

}
void *Thread1(void *pv)
{
Int pst=(Int)pv;
int v=0;
while(1)
{
pthread_mutex_lock(&pst->lock);
if(pst->x%2==0)//如果是偶数则不打印
{
pthread_cond_wait(&pst->cond1,&pst->lock);
}
pst->x++;
printf(“2”);
v++;
pthread_mutex_unlock(&pst->lock);
pthread_cond_signal(&pst->cond2);
if(v>9)
{
break;
}

}
return (void*)0;

}
/*
pthread_cond_wait的工作流程可以总结为:unlock mutex,start waiting -> lock mutex。
unlock(mutex); condition_signal()顺序,condition_signal(); unlock(mutext)
*/
void *Thread2(void *pv)
{

Int *pst=(Int *)pv;
int v=0;
while(1)
{
	pthread_mutex_lock(&pst->lock);
	if(pst->x%2!=0)//如果是奇数不打印
	{
		pthread_cond_wait(&pst->cond2,&pst->lock);
	}
	pst->x++;
	printf("1");
	v++;
	pthread_mutex_unlock(&pst->lock);
	pthread_cond_signal(&pst->cond1);
	if(v>9)
	{
		break;
	}
}
return (void*)0;

}

#include
#include

#include
#include
#include

#include

struct Data
{
int num;
int rcount;
pthread_mutex_t lock;
};

void *userData(void pv);
/

  1. pthread_join是一个阻塞函数,调用方会阻塞到pthread_join所指定的tid的线程结束后才被回收,但是在此之前,调用方是霸占系统资源的。

  2. pthread_detach,不会阻塞,调用它后,线程运行结束后会自动释放资源。
    */
    int main(int argc,char *argv[])
    {
    struct Data *pstData = NULL;
    int ret = 0;
    pthread_t tidArr[3];
    int i = 0;

    pstData = (struct Data *)malloc(sizeof(struct Data));
    if(NULL == pstData)
    {
    printf(“Malloc Failed\n”);
    return 1;
    }
    memset(pstData,0,sizeof(struct Data));

    pstData->num = 99;
    pthread_mutex_init(&pstData->lock,NULL);

    for(i = 0;i < 3;i++)
    {
    ret = pthread_create(&tidArr[i],NULL,userData,pstData);//创建线程
    if(ret != 0)
    {
    printf(“pthread create failed\n”);
    pthread_mutex_destroy(&pstData->lock);
    free(pstData);
    return 0;
    }
    pthread_detach(tidArr[i]);
    }

    while(1)
    {
    sleep(2);
    pthread_mutex_lock(&pstData->lock);//获取锁
    if(pstData->rcount == 0)
    {
    pthread_mutex_unlock(&pstData->lock);
    break;
    }
    pthread_mutex_unlock(&pstData->lock);
    }

    pthread_mutex_destroy(&pstData->lock);
    free(pstData);
    return 0;
    }

void *userData(void *pv)
{
struct Data *pstData = (struct Data *)pv;

srand((unsigned int)&pstData);

pthread_mutex_lock(&pstData->lock);
pstData->rcount++;
pthread_mutex_unlock(&pstData->lock);

sleep(rand() % 6 + 1);
printf("Num is %d,in %lu %d\n",pstData->num,pthread_self(),pstData->rcount);

pthread_mutex_lock(&pstData->lock);
pstData->rcount--;
pthread_mutex_unlock(&pstData->lock);
printf("Num is %d,in %lu %d\n",pstData->num,pthread_self(),pstData->rcount);
return (void *)0;

}

你可能感兴趣的:(pthread_cond_t)