Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/148.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/algorithm/10.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 菱形信号灯_C++_Algorithm_Semaphore - Fatal编程技术网

C++ 菱形信号灯

C++ 菱形信号灯,c++,algorithm,semaphore,C++,Algorithm,Semaphore,我的作业是在菱形模式的信号量数组上执行等待和信号 模式如下: 08 06 07 03 04 05 01 02 00 Thread #08 cannot join the diamond until threads #06 and #07 are both in position. Thread #07 cannot join until threads #04 and #05 are both in position. Thread #06 cannot j

我的作业是在菱形模式的信号量数组上执行等待和信号

模式如下:

    08
  06  07  
03  04  05
  01  02
    00

Thread #08 cannot join the diamond until threads #06 and #07 are both in position.
Thread #07 cannot join until threads #04 and #05 are both in position.
Thread #06 cannot join until threads #03 and #04 are both in position. and so on...
到目前为止,我的想法导致了以下丑陋的算法:

   if (me==0) {
       wait_sem (tCount[1]) ;
       wait_sem (tCount[2]) ;
       signal_sem (tCount[0]) ;
   }
   if (me==1) {
       wait_sem (tCount[3]) ;
       wait_sem (tCount[4]) ;
   }
   if (me==2) {
       wait_sem (tCount[4]) ;
       wait_sem (tCount[5]) ;
   }
   if (me==3) {
       wait_sem (tCount[6]) ;
   }
   if (me==4) {
       wait_sem (tCount[6]) ;
       wait_sem (tCount[7]) ;
   }
   if (me==5) {
       wait_sem (tCount[7]) ;
   }
   if (me==6) {
       wait_sem (tCount[8]) ;
   }
   if (me==7) {
       wait_sem (tCount[8]) ;
   }

有没有更干净的方法?我听过
开关
,但我以前从未使用过,因此如果有人建议使用,请向我提供解释和/或示例。非常感谢您的所有输入。

让我们采取一种非常简单的方法,将信号量保存在一个全局数组中(或者只对线程进行访问)。您可以设置如下所示的依赖项列表:

std::vector<std::vector<int>> thread_depends = {
    { },      // 0
    { 0 },    // 1
    { 0 },    // 2
    { 1 },    // 3
    { 1, 2 }, // 4
    { 2 },    // 5
    { 3, 4 }, // 6
    { 4, 5 }, // 7
    { 6, 7 }, // 8
};

这种方法的好处是,您不会重复针对每种情况执行操作的逻辑。相反,您只是表示依赖项,并且您只有一段代码来执行实际工作。这意味着更少发生复制粘贴错误的机会。

此解决方案仅用于说明如何使用case完成此操作,对于此问题有更好的实现

开关比if/else好,因为它是一个跳转,当if需要测试所有条件直到满足它(if0、if1、if2…)时,开关将直接切换到正确的情况

switch(me)
{
    case 0:
        wait_sem(tCount[1]);
        wait_sem(tCount[2]);
        signal_sem(tCount[me]);
        break;
    case 1:
        wait_sem(tCount[3]) ;
        wait_sem(tCount[4]) ;
        signal_sem(tCount[me]);
        break;
    case 2:
        wait_sem(tCount[4]);
        wait_sem(tCount[5]);
        signal_sem(tCount[me]);
        break;
    case 3:
        wait_sem(tCount[6]);
        signal_sem(tCount[me]);
        break;
    case 4:
       wait_sem(tCount[6]);
       wait_sem(tCount[7]);
       signal_sem(tCount[me]);
       break;
    case 5:
       wait_sem(tCount[7]);
       signal_sem(tCount[me]);
       break;
    case 6:
       wait_sem(tCount[8]);
       signal_sem(tCount[me]);
       break;
    case 7:
       wait_sem(tCount[8]);
       signal_sem(tCount[me]);
       break;
    case 8:
       signal_sem(tCount[me]);
       break;
}

快速提问,我会在涉及算法的函数中包含这段代码,还是应该将其中的一些代码放在全局访问中?很难快速给出答案。好的,我理解。那我就玩一会儿。谢谢,有一件事我想我应该指出的是,如果我在这里使用的概念(C++11初始值设定项列表、
、迭代器和lambdas)对您来说是全新的,那么您的老师可能会非常清楚,您的家庭作业答案不是您写的好吧,当然我可能会把这写下来,然后和他谈谈,以便更好地理解:)
switch(me)
{
    case 0:
        wait_sem(tCount[1]);
        wait_sem(tCount[2]);
        signal_sem(tCount[me]);
        break;
    case 1:
        wait_sem(tCount[3]) ;
        wait_sem(tCount[4]) ;
        signal_sem(tCount[me]);
        break;
    case 2:
        wait_sem(tCount[4]);
        wait_sem(tCount[5]);
        signal_sem(tCount[me]);
        break;
    case 3:
        wait_sem(tCount[6]);
        signal_sem(tCount[me]);
        break;
    case 4:
       wait_sem(tCount[6]);
       wait_sem(tCount[7]);
       signal_sem(tCount[me]);
       break;
    case 5:
       wait_sem(tCount[7]);
       signal_sem(tCount[me]);
       break;
    case 6:
       wait_sem(tCount[8]);
       signal_sem(tCount[me]);
       break;
    case 7:
       wait_sem(tCount[8]);
       signal_sem(tCount[me]);
       break;
    case 8:
       signal_sem(tCount[me]);
       break;
}