mailing list of musl libc
 help / color / mirror / code / Atom feed
From: William Tang <>
Subject: [musl] Spurious wake up in musl
Date: Sun, 3 Jul 2022 00:38:05 +0800	[thread overview]
Message-ID: <> (raw)


According to the about page, the spurious wake up should not be possible:

musl was the first Linux libc to have ..., the first to have condvars
where newly-arrived waiters can't steal wake events from previous

However, when I use the following code to test spurious wake up:
#include <stdbool.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>

#include <pthread.h>

#define COUNT_MAX 1000000

static int64_t counter = 0;
static pthread_cond_t cond_var = PTHREAD_COND_INITIALIZER;
static pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;

static void *thread_func(void *arg)
    while (true) {

        if (counter <= 0)
            pthread_cond_wait(&cond_var, &mutex);

        if (counter <= 0) {
            printf("[worker %p] Spurious wakeup occurred! Counter is
%lld!\n", pthread_self(), counter);

        if (--counter == 0)
            printf("[worker %p] No more work need to be done!\n",


int main()
    pthread_t thread_1, thread_2;
    pthread_create(&thread_1, NULL, thread_func, NULL);
    pthread_create(&thread_2, NULL, thread_func, NULL);

    printf("[main] Started working threads: %p, %p\n", thread_1, thread_2);

    for (size_t i = 0; i < COUNT_MAX; ++i) {

    printf("Finished counting to %u\n", COUNT_MAX);

    pthread_join(thread_1, NULL);
    pthread_join(thread_2, NULL);

    return 0;

And compile with command "musl-gcc -static main.c", it outputs:
[main] Started working threads: 0x7efc7f212f38, 0x7efc7f1eff38
[worker 0x7efc7f212f38] No more work need to be done!
[worker 0x7efc7f1eff38] No more work need to be done!
[worker 0x7efc7f1eff38] No more work need to be done!
[worker 0x7efc7f1eff38] No more work need to be done!
[worker 0x7efc7f212f38] Spurious wakeup occurred! Counter is 0!


             reply	other threads:[~2022-07-02 16:38 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-07-02 16:38 William Tang [this message]
2022-07-02 17:56 ` Rich Felker

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to='' \ \ \

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).