WissensdatenbankPrüfungszentrum
DEDeutsch
Sprache wählen13

CKTIS / C

cnd_broadcast — C23-Condition-Variable-Funktion

`cnd_broadcast(cond)` aus `<threads.h>` entsperrt alle Threads, die zum Aufrufzeitpunkt auf dieser Condition Variable blockiert sind. Ohne Wartende geschieht nichts; Rückgabe ist `thrd_success` oder `thrd_error`, wenn die Anforderung nicht erfüllt werden kann.

Version: C23

Syntax

int cnd_broadcast(cnd_t *cond);

cnd_broadcast(cond) aus <threads.h> entsperrt alle Threads, die zum Aufrufzeitpunkt auf dieser Condition Variable blockiert sind. Ohne Wartende geschieht nichts; Rückgabe ist thrd_success oder thrd_error, wenn die Anforderung nicht erfüllt werden kann.

Im Beispiel sorgen zwei Wartende sowie der Zähler arrived und derselbe Mutex dafür, dass beide den Wartezustand erreicht haben. Der Hauptthread setzt das Prädikat open und ruft cnd_broadcast; nach Join bestätigt 1 2 Erfolg und beide durchgelassenen Threads.

Eine Benachrichtigung wird nicht gespeichert; ein Broadcast vor dem Warten hilft späteren Wartenden nicht. Jeder geweckte Thread muss den Mutex meist nacheinander neu erwerben und das Prädikat in einer Schleife prüfen, da C23 Entsperren aus unbestimmtem Grund zulässt; cond muss ein lebendes, erfolgreich initialisiertes Objekt sein.

Beispiele

#include <stdatomic.h>
#include <stdio.h>
#include <threads.h>

static mtx_t gate;
static cnd_t condition;
static atomic_int arrived;
static atomic_int awakened;
static int open;

static int waiter(void *unused) {
    (void)unused;
    if (mtx_lock(&gate) != thrd_success) return 1;
    atomic_fetch_add(&arrived, 1);
    while (!open) {
        if (cnd_wait(&condition, &gate) != thrd_success) {
            mtx_unlock(&gate);
            return 1;
        }
    }
    atomic_fetch_add(&awakened, 1);
    return mtx_unlock(&gate) == thrd_success ? 0 : 1;
}

int main(void) {
    if (mtx_init(&gate, mtx_plain) != thrd_success) return 1;
    if (cnd_init(&condition) != thrd_success) {
        mtx_destroy(&gate);
        return 1;
    }
    thrd_t first;
    thrd_t second;
    if (thrd_create(&first, waiter, NULL) != thrd_success) return 1;
    if (thrd_create(&second, waiter, NULL) != thrd_success) {
        mtx_lock(&gate);
        open = 1;
        cnd_broadcast(&condition);
        mtx_unlock(&gate);
        thrd_join(first, NULL);
        return 1;
    }
    while (atomic_load(&arrived) != 2) thrd_yield();
    if (mtx_lock(&gate) != thrd_success) return 1;
    open = 1;
    int result = cnd_broadcast(&condition);
    if (mtx_unlock(&gate) != thrd_success) return 1;
    int first_result;
    int second_result;
    thrd_join(first, &first_result);
    thrd_join(second, &second_result);
    printf("%d %d\n", result == thrd_success && first_result == 0 && second_result == 0,
           atomic_load(&awakened));
    cnd_destroy(&condition);
    mtx_destroy(&gate);
    return 0;
}

Ergebnis

1 2

Im Beispiel sorgen zwei Wartende sowie der Zähler `arrived` und derselbe Mutex dafür, dass beide den Wartezustand erreicht haben. Der Hauptthread setzt das Prädikat `open` und ruft `cnd_broadcast`; nach Join bestätigt `1 2` Erfolg und beide durchgelassenen Threads.

Quellen