【问题标题】:How to I make threads starts their work in order of their IDs (using semaphores)?如何让线程按照 ID 的顺序开始工作(使用信号量)?
【发布时间】:2018-03-02 15:48:58
【问题描述】:

我有 10 个线程,每个线程都有自己的 ID,从 1 到 10;所有线程都有 2 个阶段要做(即 Phase1 和 Phase2)。我试图让所有线程首先完成它们的 Phase1,在任何线程进入 Phase2 之前,使用信号量(我做到了并且效果很好),但是我应该让所有 10 个线程按照它们的 TID(线程 ID)的顺序开始。 我尝试了很多方法,但没有得到结果!我得到的最终结果是只为前 4 个线程(有时是 5 或 6 个)工作,然后在其余线程中出现顺序混乱!

这些是我创建的信号量:

...private static Semaphore mutex = new Semaphore(1);

 // s1 is to make sure phase I for all is done before any phase II begins
private static Semaphore s1 = new Semaphore(0);

// s2 is for use in conjunction with Thread.turnTestAndSet() for phase II proceed in the thread creation order
private static Semaphore s2 = new Semaphore(1);

private static int n=10;
private static int count = 0;

这是我的线程方法:

static class AcquireBlock extends BaseThread
{
    public void run()
    {
        mutex.P();

        phase1();
        count++;

        mutex.V();

        if (count == n)
        {
            s1.V();
        }

        s1.P();
        s1.V(); 

        while(!this.turnTestAndSet());

        s2.P();
        phase2();
        s2.V();
    }
} // class AcquireBlock

turnTestAndSet方法如下:

public synchronized boolean turnTestAndSet()
{
    if(siTurn == this.iTID)
    {
        siTurn++;
        return true;
    }

    return false;
}

siTurn 初始化为 1。

我在代码中遇到的问题(我认为)是,当线程到达 While 循环 [while(!this.turnTestAndSet())] 时,它可能会成功跳过循环(在成功的情况下),但在前一个线程进入阶段 2 之前,另一个线程可能会启动并执行其 while 循环!因此,在任何线程进入阶段 2 之前,siTurn 可能会保持递增。

我知道我应该以更好的方式使用信号量 s2,并尝试从中受益,而不是将其用作互斥体。 任何新的解决方案或修复我当前的解决方案?或使用信号量的通用解决方案,以便我可以将其应用于我的代码。

【问题讨论】:

  • 请更改语言标签。这不是 C++。
  • Re, "...那么我应该让所有 10 个线程按照它们的 TID 顺序启动。"这是一个很大的红色警告标志。任何时候你认为你希望不同的线程以特定的顺序做事,你应该重新考虑线程是否是解决问题的正确方法。线程被发明来解决的最初问题是如何为彼此独立发生的不同活动编写代码。这仍然是线程最擅长的。
  • Re,“我正在尝试让所有线程先完成 Phase1,然后再任何线程 [开始] phase2。”与其让线程分别做两件事情,不如将tasks 提交到thread pool?首先,提交十个“第一阶段”任务。然后,当这些都完成后,您可以提交十个“阶段 2”任务。

标签: java multithreading semaphore pthread-barriers


【解决方案1】:

您可以通过使用条件变量来做到这一点。请参考下面我为 Github 项目编写的程序。在您的程序中使用相同的概念并解决您的问题。从下面的示例中,您可以了解如何控制线程的执行。

std::condition_variable _tcond1;
std::condition_variable _tcond2;
std::condition_variable _tcond3;

class SimpleThread1
{
private:
    std::mutex  _lockprint;
    bool isThreadAlive = true;
    int iam;
    bool print = true;
public:
    SimpleThread1(int iam)
    {
        while (print)
        {
            this->iam = iam;
            print = false;
        }

    }
    SimpleThread1(SimpleThread1 &st){};

    void PrintThread()
    {
        std::unique_lock<std::mutex> locker(_lockprint);
        _tcond1.wait(locker);
        //while (print)
        //{
            std::cout << "I am thread :" << iam << std::endl;
            //print = false;
        //}

        _tcond3.notify_one();
    }
    void operator()()
    {
        while (isThreadAlive)
         PrintThread();
    }

    void stopeThread()
    {
        isThreadAlive = false;
    }
};

class SimpleThread2
{
private:
    std::mutex  _lockprint;
    bool isThreadAlive = true;

public:
    SimpleThread2(){}
    SimpleThread2(SimpleThread2 &st) {};

    void PrintThread()
    {
        std::unique_lock<std::mutex> locker(_lockprint);
        _tcond2.wait(locker);
        std::cout << "I am thread :2"<< std::endl;
        _tcond1.notify_one();
    }
    void operator()()
    {
        while (isThreadAlive)
            PrintThread();
    }

    void stopeThread()
    {
        isThreadAlive = false;
    }
};


class SimpleThread3
{
private:
    std::mutex  _lockprint;
    bool isThreadAlive = true;

public:
    SimpleThread3(){}
    SimpleThread3(SimpleThread3 &st) {};

    void PrintThread()
    {
        std::unique_lock<std::mutex> locker(_lockprint);
        _tcond3.wait(locker);
        std::cout << "I am thread :3"<< std::endl;
        _tcond2.notify_one();
    }
    void operator()()
    {
        while (isThreadAlive)
            PrintThread();
    }

    void stopeThread()
    {
        isThreadAlive = false;
    }
};

int main()
{
    SimpleThread1 st1(1);
    SimpleThread2 st2;
    SimpleThread3 st3;
    std::thread t1(st1);
    std::thread t2(st2);
    std::thread t3(st3);
    _tcond1.notify_one();
    t1.detach();
    t2.detach();
    t3.detach();
    std::this_thread::sleep_for(std::chrono::milliseconds(200));
    st1.stopeThread();
    st2.stopeThread();
    st3.stopeThread();
    return 0;
}

【讨论】:

  • 你能分享你的代码的github版本吗?我问这个是因为如果你在那里有相关的工作会对我有所帮助。
  • 对不起 Jawad 我没有 github 版本。
猜你喜欢
  • 2015-06-08
  • 2017-04-29
  • 2018-01-03
  • 2015-05-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-04-01
相关资源
最近更新 更多