Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon. Entire thread

Thread-safe dubs checking

Name: Anonymous 2012-02-28 0:51

I'm writing a program for recognizing and validating a specific set of integer values called ``dubs''. If the message handler is ready for dubs, it sends a CHECK_EM message to the thread. When it receives a CHECK_EM, it replies with a NICE_DUBS message. How can I avoid a TOCTOU (Time-of-check Time-of-use) race condition by making sure that no other messages are sent to the thread between the time it enters the dubs ready state and the time the dubs are checked?

Name: Anonymous 2012-02-28 0:56

#include <stdio.h>
#include <stdlib.h>
#include <time.h>

const time_t seconds = 1,
      minutes = 60 * seconds,
      hours = 60 * minutes,
      days = 24 * hours;

char *saydate(time_t when) {
    struct tm *td = localtime(&when);
    char *ret;
    asprintf(&ret, "%d/%d/%d",
        td->tm_mon + 1,
        td->tm_mday,
        td->tm_year + 1900);

    // Leaks memory if not freed
    return ret;
}

int main(int argc, char **argv) {
    time_t when = time(NULL),
           start = 1239610380,
           offset = 612;

    if (argc > 1) offset = atoi(argv[1]);

    printf("%s + %ld days = %s\n",
        saydate(start), offset,
        saydate(start + offset * days));

    return 0;
}

Newer Posts
Don't change these.
Name: Email:
Entire Thread Thread List