码迷,mamicode.com
首页 > 系统相关 > 详细

2017 三进程同步互斥

时间:2019-10-20 19:44:45      阅读:98      评论:0      收藏:0      [点我收藏+]

标签:变量   访问   ret   str   style   turn   struct   thread   div   

typedef struct
{
    float a;
    float b;
}cnum;
cnum x,y,z;
cnum add(cnum p,cnum q)
{
    cnum s;
    s.a=p.a+q.a;
    s.b=p.b+q.b;
    return s;
}
sem mutex1=1;//t1和t2变量y互斥访问
sem mutex2=1;//t2与t3变量y互斥访问
sem mutex3=1;//变量z互斥访问
thread 1
{
   cnum w;
   p(mutex1);
   w=add(x,y);
   v(mutex1);
}
thread 2
{
    cnum w;
    p(mutex2);
    p(mutex3);
    w=add(y,z);
    v(mutex3);
    v(mutex2);
}
thread 3
{
    cnum w;
    w.a=1;
    w.b=1;
    p(mutex3);
    z=add(z,w);
    v(mutex3);
    p(mutex1);
    p(mutex2);
    y=add(y,w);
    v(mutex1);
    v(mutex2);
}

 

2017 三进程同步互斥

标签:变量   访问   ret   str   style   turn   struct   thread   div   

原文地址:https://www.cnblogs.com/yangmenda/p/11708554.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!