Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/algorithm/11.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
Multithreading 使用组合树的共享计数器死锁问题_Multithreading_Algorithm_Counter - Fatal编程技术网

Multithreading 使用组合树的共享计数器死锁问题

Multithreading 使用组合树的共享计数器死锁问题,multithreading,algorithm,counter,Multithreading,Algorithm,Counter,我正在使用组合树的概念开发一个共享计数器增量应用程序。我的目标是让这个应用程序在2^n个核上工作,比如4、8、16、32等等。这个算法在任何线程失败时都可能出错。假设不会出现线程故障或非常慢的线程 两个线程在叶节点上竞争,后一个到达树上 到达的第一个等待,直到第二个到达层次结构并返回正确的返回值 第二个线程唤醒第一个线程 每个线程都获得正确的fetchAndAdd值 但此算法有时在(节点[index].isActive==1)或(节点[index].waiting==1)循环时被锁定在内部。

我正在使用组合树的概念开发一个共享计数器增量应用程序。我的目标是让这个应用程序在2^n个核上工作,比如4、8、16、32等等。这个算法在任何线程失败时都可能出错。假设不会出现线程故障或非常慢的线程

  • 两个线程在叶节点上竞争,后一个到达树上
  • 到达的第一个等待,直到第二个到达层次结构并返回正确的返回值
  • 第二个线程唤醒第一个线程
  • 每个线程都获得正确的fetchAndAdd值
但此算法有时在(节点[index].isActive==1)或(节点[index].waiting==1)循环时被锁定在内部。我看不到任何死锁的可能性,因为每个节点上只有两个线程在竞争。你们能在这个问题上启发我吗

int increment(int threadId, int index, int value) {
    int lastValue = __sync_fetch_and_add(&nodes[index].firstValue, value);
    if (index == 0) return lastValue;
    while (nodes[index].isActive == 1) {
    }
    if (lastValue == 0) {
        while(nodes[index].waiting == 1) {
        }
        nodes[index].waiting = 1;
        nodes[lindex].isActive = 0;
    } else {
        nodes[index].isActive = 1;
        nodes[index].result = increment(threadId, (index - 1)/2, nodes[index].firstValue);
            nodes[index].firstValue = 0;
            nodes[index].waiting = 0;
    }
    return nodes[index].result + lastValue;
}

我不认为这将工作在1核心。您可以无限循环isActive,因为除非isActive为0,否则无法将其设置为0

我不确定您的代码是否有阻止这种情况的机制,但这里是我最擅长的破解方法,这里是运行并导致问题的线程:

ex)


很难确切理解这里发生了什么/您正试图做什么,但我建议您需要能够停用节点[index].isActive。您可能希望在函数末尾将其设置为0

您是对的,这在1核上不起作用。其中一个未提及的假设是将有2^n个核。对。N=0对应于1芯。而且我很确定如果它不能在1核上运行,那么它就不能在1的任何倍数上运行。这就是评论的初衷
thread1                         thread 2
nodes[10].isActive = 1
                                //next run on index 10
                                while (nodes[index].isActive == 1) {//here is the deadlock}