locked
Parallel For loop and UI update RRS feed

  • Question

  • Hi,

    I have an MFC based progress bar that get updated in a for loop. I'm changing the loop for doing the work to a parallel for loop and was wondering how the progress bar should be updated now that I'm using the parallel for loop.

    int loopCount = 10000;
    ProgressBar myProgressBar(0, loopCount); // Creating the progress bar GUI (MFC)
    myProgressBar.Show();

    task_group *tg = new task_group();
    tg->run_and_wait([&]{
    parallel_for(0, loopCount, [&] (int i)
    {
    _sleep(5000); // Do some work here
    // myProgressBar.StepIt(); // This block the parallel loop !!
    });

    });

    Any way to edit this logic without blocking the UI thread ?

    Thanks

    Noury




    • Edited by NouryF Tuesday, November 27, 2012 9:22 PM
    Sunday, November 25, 2012 4:16 PM

Answers

  • Hi,

    You have to be careful when updating the MFC classes from a non UI thread as they may not be thread safe. The safest way to do this is by posting messages to the UI thread with PostMessage this will not block the parallel_for, and will be safe for the non-thread safe classes. Please refer to MFC documentation to know which WM_* you need to send to the progress par to trigger the event you wish to call.

    Thanks

    • Proposed as answer by Mohamed Ameen Ibrahim Wednesday, January 16, 2013 12:23 AM
    • Marked as answer by NouryF Thursday, January 17, 2013 1:23 PM
    Monday, January 14, 2013 8:04 PM

All replies

  • Hi,

    You have to be careful when updating the MFC classes from a non UI thread as they may not be thread safe. The safest way to do this is by posting messages to the UI thread with PostMessage this will not block the parallel_for, and will be safe for the non-thread safe classes. Please refer to MFC documentation to know which WM_* you need to send to the progress par to trigger the event you wish to call.

    Thanks

    • Proposed as answer by Mohamed Ameen Ibrahim Wednesday, January 16, 2013 12:23 AM
    • Marked as answer by NouryF Thursday, January 17, 2013 1:23 PM
    Monday, January 14, 2013 8:04 PM
  • Thank you Mohamed for your response. That is indeed what I did end up doing: postmessage to update the progress bar.

    Noury

    Tuesday, January 15, 2013 12:23 PM