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

Pages: 1-

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:55

It's a nice day for a #FFFFFF wedding.

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;
}

Name: Anonymous 2012-02-28 4:26

>>1
you dont! thats the fun with threads
the system takes care of everything!

Name: Anonymous 2012-02-28 9:18

Just put the pending messages in a synchronous queue.

Name: Anonymous 2012-02-28 9:40

Threads are good for two things: submitting audio behind a UI and parallelising computation. Every other use, such as >>1's, is an unecessary complication.

Name: Anonymous 2012-03-01 2:11

>>3
// Leaks memory if not freed
Doesn't call free()

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