pthread_cond_t

#include <pthread.h>
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

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 <unistd.h>
#include <time.h>

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include <pthread.h>

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;

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值