menuTicketing

Ticketing C/C++ Library

GitHub

Not yet registered in the official vcpkg registry — the submission requirements are strict and registration is still in progress. For now, clone the Github repo above and build it from source.

Repository

Example

Basic Example

A blocking C API. Connect to a single server, acquire a key, and release it right away.

c
#include "ticketing/ticketing.h"

const char* addrs[1] = {"127.0.0.1:5225"};
ticketing_broker* broker = ticketing_connect(addrs, 1);
ticketing_wait_ready(broker, 2000);

ticketing_ticket* ticket = NULL;
ticketing_result r = ticketing_acquire(broker, "key", 5, 30, &ticket);

bool held = false;
ticketing_release(ticket, &held);
ticketing_ticket_close(ticket);

printf("pass\n");
ticketing_broker_close(broker);

Concurrency Example (Distributed Lock)

An example that verifies that the balance always returns to its starting value even when several pthreads repeat deposits/withdrawals while mutually excluding each other via a per-account lock.

c
static void* transaction(void* arg) {
    op_t* op = (op_t*)arg;
    char key[32];
    snprintf(key, sizeof(key), "account-%d", op->account);

    ticketing_ticket* ticket = NULL;
    ticketing_result r = ticketing_acquire(g_broker, key, WAIT, LEASE, &ticket);
    if (r != TICKETING_OK) {
        fprintf(stderr, "account %d acquire failed: %s\n", op->account, ticketing_strerror(r));
        return NULL;
    }

    int64_t before = balances[op->account];
    sched_yield(); // critical section safe only while holding the lock
    int64_t after = before + op->delta;
    balances[op->account] = after;

    ticketing_ticket_close(ticket); // released automatically in the background

    const char* action = op->delta < 0 ? "withdraw" : "deposit";
    fprintf(stderr, "account %d %s $%" PRId64 " -> $%" PRId64 "\n", op->account, action, op->delta, after);
    return NULL;
}

See the full examples in example_test.c, bank_example_test.c, bench_test.c.