Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7790471
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T21:35:50+00:00 2026-06-01T21:35:50+00:00

I have several threads and once they are all finished working, I need to

  • 0

I have several threads and once they are all finished working, I need to call a myMergeBlock method exactly once per action. I can’t use dispatch_once because I want to be able to call myMergeBlock at a later time.

Some pseudo code looks like this but is not yet thread safe:

BOOL worker1Finished, worker2Finished, worker3Finished;

void (^mergeBlock)(void) = ^{
    if (worker1Finished && worker2Finished && worker3Finished)
        dispatch_async(queue, myMergeBlock);    // Must dispatch this only once
}

void (^worker1)(void) = ^{
    ...
    worker1Finished = YES;
    mergeBlock();
}

void (^worker2)(void) = ^{
    ...
    worker2Finished = YES;
    mergeBlock();
}

void (^worker3)(void) = ^{
    ...
    worker3Finished = YES;
    mergeBlock();
}

Also, based on the way the workers are called, I do not call them directly, but instead pass them into a function as arguments.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-01T21:35:52+00:00Added an answer on June 1, 2026 at 9:35 pm

    You want to use dispatch groups. First you create a group, schedule the three workers in the group, then add a notification block to the group.
    It should look something like this:

    //create dispatch group
    dispatch_group_t myWorkGroup = dispatch_group_create();
    
    //get one of the global concurrent queues
    dispatch_queue_t myQueue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, NULL);
    
    //submit your work blocks
    dispatch_group_async(myWorkGroup, myQueue, worker1);
    dispatch_group_async(myWorkGroup, myQueue, worker2);
    dispatch_group_async(myWorkGroup, myQueue, worker3);
    
    //set the mergeBlock to be submitted when all the blocks in the group are completed
    dispatch_group_notify(myWorkGroup, myQueue, mergeBlock);
    
    //release the group as you no longer need it
    dispatch_release(myWorkGroup);
    

    You could hang on the the group and reuse it later if you prefer. Be sure to schedule the work before the notification. If you try to schedule the notification first it will be dispatched immediately.

    I haven’t tested this code but I do use dispatch_groups in my projects.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Where is the TickCount() call? I have seen several threads on the web that
I have a long-running service with several threads calling the following method hundreds of
I have a queue structure that is being used by several pthreads. The threads
I have several doctrine models that have relationships to other models. Let's call those
There are several similar threads Q1 or Q2 to this one, but not exactly.
I have an asynchronous application executing several threads doing operations over sockets where operations
I have a WPF application that spins off several threads. I have defined a
I have a static class that creates several worker threads in its constructor. If
I have several thread pools and I want my application to handle a cancel
I have several cron jobs and I'd like to perform some calculations on them,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.