如何搜索下一个可用线程进行计算 [英] How to search through next available thread to do computation

查看:74
本文介绍了如何搜索下一个可用线程进行计算的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在C ++中进行多线程处理.这可能是非常标准的东西,但我似乎无法在任何地方找到它,也无法知道任何关键术语以在线搜索它.

I am doing multithreading in C++. This may be something very standard but I can't seem to find it anywhere or know any key terms to search for it online.

我想多次执行某种计算,但是要使用多个线程.对于每次计算迭代,我都希望找到已完成其上一次计算的下一个可用线程,以进行下一次迭代.我不想按顺序循环浏览各个线程,因为要调用的下一个线程可能尚未完成工作.

I want to do some sort of computation many times but with multiple threads. For each iteration of computation, I want to find the next available thread that has finished its previous computation to do the next iteration. I don't want to cycle through the threads in order since the next thread to be called may not have finished its work yet.

例如 假设我有一个int向量,我想用5个线程求和.我将要更新的总和存储在某个位置,以及当前正在处理的元素数.每个线程查看该计数以查看下一个位置,然后获取该向量值并将其添加到到目前为止的总和中.然后返回以查找要进行下一次迭代的计数.因此,对于每次迭代,计数都会递增,然后寻找下一个可用线程(也许是一个已经在等待计数的线程;或者也许他们都在忙于工作)以进行下一次迭代.我们不增加线程数量,但是我希望能够以某种方式搜索所有5个线程,以找到第一个完成下一次计算的线程.

E.g. Suppose I have a vector of int and I want to sum up the total with 5 threads. I have the to-be-updated total sum stored somewhere and the count for which element I am currently up to. Each thread looks at the count to see the next position and then takes that vector value and adds it to the total sum so far. Then it goes back to look for the count to do the next iteration. So for each iteration, the count increments then looks for the next available thread (maybe one already waiting for count; or maybe they are all busy still working) to do the next iteration. We do not increase the number of threads but I want to be able to somehow search through all the 5 threads for the first one that finish to do the next computation.

我将如何进行编码.我所知道的每一种方式都涉及到遍历线程,这样我就无法检查下一个可能出现故障的可用线程.

How would I go about coding this. Every way I know of involves doing a loop through the threads such that I can't check for the next available one which may be out of order.

推荐答案

在全局变量上使用semafor(或互斥锁,请始终将这两者混合),以告诉您下一步是什么.只要您访问变量,semafor就会锁定其他线程,从而使该线程的访问变得清晰.

Use semafor (or mutex, always mix up those two) on a global variable telling you what is next. The semafor will lock the other threads out as long as you access the variable making that threads access clear.

因此,假设您有一个X元素数组.并且一个名为nextfree的全局女巫被初始化为0,然后一个伪代码如下:

So, assuming you have an Array of X elements. And a global called nextfree witch is initalized to 0, then a psudo code would look like this:

while (1)
{
    <lock semafor INT>
    if (nextfree>=X)
    {
        <release semnafor INT>
        <exit and terminate thread>
    }
    <Get the data based on "nextfree">
    nextfree++;
    <release semafor INT>

    <do your stuff withe the chunk you got>
}

这里的要点是,每个线程将是单独的,并且可以对semafor锁内的数据结构进行排他性访问,因此可以访问下一个可用线程,而不管其他线程在做什么. (如果其他线程完成了,则其他线程将必须排队等待,而另一个线程正在获取下一个数据块.当您释放时,只有一个站在队列中的线程将获得访问权限.其余线程将不得不等待.)

The point here is that each thread will be alone and have exlusive access to the data struct within the semafor lock and therefore can access the next available regardless of what the others doing. (The other threads will have to wait in line if they are done while another thread working on getting next data chunk. When you release only ONE that stands in queue will get access. The rest will have to wait.)

有些事情要提防.如果您设法退出错误的位置(随便释放它)或创建死锁,Semafor可能会锁定您的系统.

There are some things to be ware of. Semafor's might lock your system if you manage to exit in the wrong position (Withour releasing it) or create a deadlock.

这篇关于如何搜索下一个可用线程进行计算的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆