【问题标题】:Synchronising threads同步线程
【发布时间】:2018-12-23 20:09:52
【问题描述】:

如何让两个线程互相等待,直到它们使用 pthread 完成一个循环?

void* th1Fn()
{  
    while(1)
    {
        //do something
        printf("I'm done");
        //signal that i'm done
        //wait for thread2 so that I can repeat the cycle
    }
}

void* th2Fn()
{       
    while(1)
    {
        //do something
        printf("I'm done");
        //signal that i'm done
        //wait for thread1 so that I can repeat the cycle
    }
}

【问题讨论】:

  • 在线程之间来回悬挂信号量单元 - 很简单。不需要比这更复杂的了。

标签: c linux pthreads semaphore


【解决方案1】:

【讨论】:

  • 这很接近。但我想在循环中使用障碍。我需要声明一个数组还是可以使用相同的屏障?
  • 您阅读过文档吗?这一切都在文档中很清楚。我会为每个同步点使用一个屏障。
  • 你可以多次使用同一个屏障。
猜你喜欢
  • 2014-04-30
  • 1970-01-01
  • 1970-01-01
  • 2016-01-20
  • 1970-01-01
  • 2018-10-02
相关资源
最近更新 更多