如何使用 OpenMP 等待互斥体

发布于 2024-10-23 01:22:45 字数 1749 浏览 4 评论 0原文

我有一个 for 循环,它将并行启动进程,每个启动的进程都会返回一个响应,表明它已准备好。我想等待响应,如果达到一定的超时时间,我将中止。

开发环境是VS2008
这是伪代码:

void executeCommands(std::vector<Command*> commands)
{
    #pragma omp parallel for
    for (int i = 0; i < commands.size(); i++)
    {
        Command* cmd = commands[i];
        DWORD pid = ProcessLauncher::launchProcess(cmd->getWorkingDirectory(),  cmd->getCommandToExcecute(), cmd->params);

        //Should I wait for process to become ready?
        if (cmd->getWaitStatusTimeout() > 0)
        {
            ProcessStatusManager::getInstance().addListener(*this);

            //TODO: emit process launching signal

            //BEGINNING OF QUESTION
            //I don't how to do this part. 
            //I might use QT's QWaitCondition but if there is another solution in omp
            //I'd like to use it
            bool timedOut;
            SOMEHANDLE handle = Openmp::waitWithTimeout(cmd->getWaitStatusTimeout(), &timedOut);
            mWaitConditions[pid]) = handle;
            //END OF QUESTION

            if (timedOut)
            {
                ProcessStatusManager::getInstance().removeListener(*this);
                //TODO: kill process
                //TODO: emit fail signal
            }
            else
            {
                //TODO: emit process ready signal
            }
        }
        else
        {
            //TODO: emit process ready signal
        }
    }
}

void onProcessReady(DWORD sourceProcessPid)
{
    ProcessStatusManager::getInstance().removeListener(*this);
    SOMEHANDLE handle = mWaitConditions[sourceProcessPid];
    if (mWaitConditions[sourceProcessPid] != 0)
    {
        Openmp::wakeAll(handle);
    }
}

I've a for loop that will launch processes in parallel every launched process will return a response back indicating that it is ready. I want to wait for the response and I'll abort if a certain timeout is reached.

Development environment is VS2008
Here is the pseudo code:

void executeCommands(std::vector<Command*> commands)
{
    #pragma omp parallel for
    for (int i = 0; i < commands.size(); i++)
    {
        Command* cmd = commands[i];
        DWORD pid = ProcessLauncher::launchProcess(cmd->getWorkingDirectory(),  cmd->getCommandToExcecute(), cmd->params);

        //Should I wait for process to become ready?
        if (cmd->getWaitStatusTimeout() > 0)
        {
            ProcessStatusManager::getInstance().addListener(*this);

            //TODO: emit process launching signal

            //BEGINNING OF QUESTION
            //I don't how to do this part. 
            //I might use QT's QWaitCondition but if there is another solution in omp
            //I'd like to use it
            bool timedOut;
            SOMEHANDLE handle = Openmp::waitWithTimeout(cmd->getWaitStatusTimeout(), &timedOut);
            mWaitConditions[pid]) = handle;
            //END OF QUESTION

            if (timedOut)
            {
                ProcessStatusManager::getInstance().removeListener(*this);
                //TODO: kill process
                //TODO: emit fail signal
            }
            else
            {
                //TODO: emit process ready signal
            }
        }
        else
        {
            //TODO: emit process ready signal
        }
    }
}

void onProcessReady(DWORD sourceProcessPid)
{
    ProcessStatusManager::getInstance().removeListener(*this);
    SOMEHANDLE handle = mWaitConditions[sourceProcessPid];
    if (mWaitConditions[sourceProcessPid] != 0)
    {
        Openmp::wakeAll(handle);
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

羅雙樹 2024-10-30 01:22:49

正如上面的评论所指出的,Michael Suess 确实发表了一篇有关向 OpenMP 添加此功能的论文。他是最后一个提议向 OpenMP 添加某种类型的等待函数的人。 OpenMP 语言委员会已多次讨论该问题。每次它都被拒绝,因为已经有其他方法可以执行此功能。我不懂Qt,但是只要它提供的函数是线程安全的,那么你应该能够使用它们。

As the comment above pointed out, Michael Suess did present a paper on adding this functionality to OpenMP. He is the last of several people that have proposed adding some type of wait function to OpenMP. The OpenMP language committee has taken the issue up several times. Each time it has been rejected because there are other ways to do this function already. I don't know Qt, but as long as the functions it provides are thread safe, then you should be able to use them.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文