Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/13.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
Arrays 从线程c、cygwin读取数组_Arrays_Multithreading_Semaphore_Progress_Percentage - Fatal编程技术网

Arrays 从线程c、cygwin读取数组

Arrays 从线程c、cygwin读取数组,arrays,multithreading,semaphore,progress,percentage,Arrays,Multithreading,Semaphore,Progress,Percentage,我对线程非常陌生,希望了解一些细节。我试图得到每个线程完成计算的百分比。每个线程将向同一数组的不同元素报告其百分比。我在pthread\u create之后立即使用pthread\u join和一个单独的线程来读取数组的所有值并打印百分比,但是当我让所有线程在不等待前一个线程完成的情况下彼此运行时,我会出现一些奇怪的行为。这就是我访问共享(全局)阵列的方式 }线程访问的一些非同步代码段会导致此问题 要同步的第一个位置是: myId = temp->member++; 但更重要的是,

我对线程非常陌生,希望了解一些细节。我试图得到每个线程完成计算的百分比。每个线程将向同一数组的不同元素报告其百分比。我在
pthread\u create
之后立即使用
pthread\u join
和一个单独的线程来读取数组的所有值并打印百分比,但是当我让所有线程在不等待前一个线程完成的情况下彼此运行时,我会出现一些奇怪的行为。这就是我访问共享(全局)阵列的方式


}

线程访问的一些非同步代码段会导致此问题

要同步的第一个位置是:

   myId = temp->member++;
但更重要的是,主线正在做:

   args->incrS = (i)*increment +1;
   args->incrF = (i+1)*increment +1;
同时在线程中:

   for(count=temp->incrS; count<= temp->incrF; count++)
    {

        percent = (float)(count-temp->incrS)/total*100;
        currentProgress[myId] = (int)percent;

        if (number%count == 0)
         {
                factors[counter++] = count;
                factors[counter++] = number/count;
         }   
        usleep(1);
    }
for(count=temp->incrS;countincrF;count++)
{
百分比=(浮动)(计数温度->增量)/总计*100;
当前进度[myId]=(整数)百分比;
如果(数字%count==0)
{
系数[计数器++]=计数;
系数[计数器++]=数量/计数;
}   
usleep(1);
}

上述非同步访问会影响
百分比
值的计算,从而导致此类异常事件。您必须在所有这些地方进行同步,以获得您期望的行为

导致此问题的线程访问了一些不同步的代码片段

要同步的第一个位置是:

   myId = temp->member++;
但更重要的是,主线正在做:

   args->incrS = (i)*increment +1;
   args->incrF = (i+1)*increment +1;
同时在线程中:

   for(count=temp->incrS; count<= temp->incrF; count++)
    {

        percent = (float)(count-temp->incrS)/total*100;
        currentProgress[myId] = (int)percent;

        if (number%count == 0)
         {
                factors[counter++] = count;
                factors[counter++] = number/count;
         }   
        usleep(1);
    }
for(count=temp->incrS;countincrF;count++)
{
百分比=(浮动)(计数温度->增量)/总计*100;
当前进度[myId]=(整数)百分比;
如果(数字%count==0)
{
系数[计数器++]=计数;
系数[计数器++]=数量/计数;
}   
usleep(1);
}

上述非同步访问会影响
百分比
值的计算,从而导致此类异常事件。您必须在所有这些地方进行同步,以获得您期望的行为

我想你必须把完整的代码放在这里,因为做事的顺序很重要。我想你必须把完整的代码放在这里,因为做事的顺序很重要。谢谢。我看到了问题的Id部分,但完全忽略了增量。我正在使用mutex和条件变量编写一个sem类,但在此之前,我只会将它们分配给新数据,并让主线程暂时休眠。我建议将所有并发的
读/写
操作移动到一些正确同步的方法。同时,将
temp->incrS
temp->incrF
提取到
for
循环之前的一些局部变量中,并在循环中使用这些变量也会有所帮助。谢谢。我看到了问题的Id部分,但完全忽略了增量。我正在使用mutex和条件变量编写一个sem类,但在此之前,我只会将它们分配给新数据,并让主线程暂时休眠。我建议将所有并发的
读/写
操作移动到一些正确同步的方法。同时,将
temp->incrS
temp->incrF
提取到
for
循环之前的一些局部变量中,并在循环中使用这些变量也会有所帮助。