lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


Asko,

On Thu, Jun 11, 2009 at 11:54 PM, Asko Kauppi<askok@dnainternet.net> wrote:
>
> The re-entrant gmtime_r() and localtime_r() are not ANSI C, I believe. You
> can provide a patch but inclusion of non-ANSI C features has seemed to be a
> 'no'. Maybe this will change, eventually.
>
> "The asctime_r(), ctime_r(), gmtime_r(), and localtime_r() functions are
> expected to conform to ISO/IEC 9945-1:1996 (``POSIX.1'')"
>
> Can you post a case where using such functions really presents a hazard, in
> a multithreaded application?

Sorry for the delay,

gneill@blackfoot:~/lua-dev$ cat localtime.c
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <pthread.h>

void *onef(void *arg)
{
  time_t t = (time_t)0;
  struct tm * stm;

  stm = localtime(&t);

  fprintf(stdout, "one localtime [%p]\n", stm);

  return (void*)NULL;
}

void *twof(void *arg)
{
  time_t t = (time_t)0;
  struct tm * stm;

  stm = localtime(&t);

  fprintf(stdout, "two localtime [%p]\n", stm);

  return (void*)NULL;
}

void *threef(void *arg)
{
  time_t t = (time_t)0;
  struct tm * stm;

  stm = gmtime(&t);

  fprintf(stdout, "three gmtime [%p]\n", stm);

  return (void*)NULL;
}

int main(int argc, char *argv[])
{
  pthread_t one;
  pthread_t two;
  pthread_t three;

  pthread_create(&one, NULL, onef, NULL);
  pthread_create(&two, NULL, twof, NULL);
  pthread_create(&three, NULL, threef, NULL);

  pthread_join(one, NULL);
  pthread_join(two, NULL);
  pthread_join(three, NULL);

  return 0;
}

gneill@blackfoot:~/lua-dev$ !gcc
gcc -lpthread localtime.c

gneill@blackfoot:~/lua-dev$ ./a.out
one localtime [0xb805b380]
two localtime [0xb805b380]
three gmtime [0xb805b380]

The example only proves the three threads have been given back the
same memory address.  But I think it's fair to say that it is not a
good idea to use in a threaded application (without some locking).

HTH,
George