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 6119925
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T15:36:33+00:00 2026-05-23T15:36:33+00:00

The documentation for dispatch_semaphore_wait says that it waits in FIFO order for a signal.

  • 0

The documentation for dispatch_semaphore_wait says that it “waits in FIFO order for a signal”. But it doesn’t seem to in this example– can someone please explain?

Example:

#include <dispatch/dispatch.h>
#include <stdio.h>

dispatch_queue_t q1, q2;
dispatch_semaphore_t sem;
int g_call;

void do_work(void)
{
    int s = 0;
    int i;
    for (i = 0; i < 100000000; ++i)
        ++s;
}

void f1(int call)
{
__block int waited = 0;
    dispatch_async(q1, ^{
        while (dispatch_semaphore_wait(sem, dispatch_time(DISPATCH_TIME_NOW, NSEC_PER_SEC/1000)))
            waited = 1;
        printf("1:%d %s\n", call, waited ? "waited" : "");
        do_work();
        dispatch_semaphore_signal(sem);
    });
}

void f2(int call)
{
    __block int waited = 0;
    dispatch_async(q2, ^{
        while (dispatch_semaphore_wait(sem, dispatch_time(DISPATCH_TIME_NOW, NSEC_PER_SEC/1000)))
            waited = 1;
        printf("\t\t2:%d %s\n", call, waited ? "waited" : "");
        do_work();
        dispatch_semaphore_signal(sem);
    });
}

int main(int argc, char **argv)
{
    q1 = dispatch_queue_create(NULL, NULL);
    q2 = dispatch_queue_create(NULL, NULL);
    sem = dispatch_semaphore_create(1);
    g_call = 0;

    dispatch_queue_t q_global = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
    dispatch_source_t timer = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0, q_global);
    const uint64_t DELAY = 10;
    dispatch_source_set_event_handler(timer, ^{
        f1(g_call);
        f2(g_call);
        ++g_call;
        dispatch_source_set_timer(timer, dispatch_time(DISPATCH_TIME_NOW, DELAY), 0, 0);
    });
    dispatch_source_set_timer(timer, dispatch_time(DISPATCH_TIME_NOW, DELAY), 0, 0);
    dispatch_resume(timer);

    sleep(3);
}

Expected output:

1:0
        2:0
1:1
        2:1
1:2
        2:2
...

Actual output (one example):

1:0
1:1
...
1:14
        2:0 waited
        2:1
        ...

Edit: Actual output if, instead of being serial queues, q1 and q2 are set to the global queue:

1:0 
        2:8 waited
1:3 waited
1:4 waited
        2:3 waited
1:6 waited
1:9 waited
        2:9 waited
        2:21 
1:28 waited

(Sometimes it works perfectly, but sometimes it’s weird like this.)

  • 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-05-23T15:36:34+00:00Added an answer on May 23, 2026 at 3:36 pm

    dispatch_queue_create creates a serial queue, and then the serial queue creates a pthread thread (I’m not so sure…).

    And dispatch_semaphore_wait uses spin locks for acquiring a semaphore for performance. It means that it is not a context switching point as pthread_mutex_lock. It doesn’t switch context so frequently.

    If you use the global queue, your code would output as your expected (however it is not exactly same). Because the global queue uses pthread workqueue. The behavior of switching context is different from a pthread thread’s one.

    q1 = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
    q2 = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
    

    EDITED:

    The global queue executes the given tasks sequentially, but these tasks are executing concurrently, the output order may vary by context switching. Moreover, the timer of your code fires every 10 nanoseconds, too many tasks are executing concurrently.

    Another simple example,

    dispatch_queue_t queue =
        dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
    dispatch_apply(10, queue, ^(size_t index) {
        printf("%zu\n", index);
    });
    

    On my 8core MacBook Pro:

    4
    2
    0
    6
    3
    1
    5
    8
    9
    7
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Documentation can be found here It says in the example: onDrop: Called whenever a
Documentation says that waypoints limit is 8 points. But I have to draw a
The documentation says to use 1.6.4, but we're up to 1.7.2 now. Can I
Documentation says Dictionary keys order is unspecified. I guess it means the first added
The documentation lists the tags that are allowed/removed by default: http://www.feedparser.org/docs/html-sanitization.html But it doesn't
The documentation doesn't say anything about that. When an old object is removed and
The documentation implies that you can either have 3D rendering or framebuffer mode, and
Documentation states that interface delegation is available for Win32 only. Currently I can't test
The documentation says Checks whether the TTS engine is busy speaking. But I just
Documentation says: The Grails team discourages the embedding of core application logic inside controllers,

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.