开发者

How to wait on a Mutex with OpenMP

开发者 https://www.devze.com 2023-02-18 12:12 出处:网络
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 tim

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);
            mWaitConditio开发者_运维知识库ns[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);
    }
}


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号