I authored a module for Apache HTTP server and discovered strange behavior. I have assumed that static variables are initialized only one time, however i authored the code below making two demands to Apache the output was:

test_handler: isInit=0
test_handler: isInit=1

test_handlere: isInit=0
test_handlere: isInit=1

The exam code:

static int isInit = 0;

static int test_handler( request_rec *r ) {
    fprintf(stderr,"\n\natest_handler: isInit=%d", isInit );
    if( !isInit ) {
        isInit = 1;
    fprintf(stderr,"\natest_handler: isInit=%d", isInit );
    return DECLINED;

static void register_hooks(apr_pool_t *p) {
    ap_hook_translate_name(test_handler, NULL, NULL, APR_HOOK_FIRST);    
    fprintf(stdout,"register_hooks done\n");

module AP_MODULE_DECLARE_DATA test_module = {
    NULL,                  /* create per-dir    config structures */
    NULL,                  /* merge  per-dir    config structures */
    NULL,                  /* create per-server config structures */
    NULL,                  /* merge  per-server config structures */
    NULL,                  /* table of config file commands       */
    register_hooks  /* register hooks                      */

The problem associated with threads, for how long I actually do 10 demands to Apache I see isInit=1, isInit=1 in some instances and isInit=0, isInit=1 in other people.

My real question is, the way i can define a flexible, that'll be available in test_handler() and can preserve its value between calls towards the function?

I believe, I discovered the issue. Apache Server for Linux produces several 'child' servers for everyone paralel demands. Every virtual sever loads configuration, including module instantces, therefore if ApacheServer produces 8 child server proccesses, you 8 copies of isInit variable. You are able to configure Apache to produce obly one server ( not suggested - performance ). One other way would be to configure Apache Server to make use of other Multi-Proccess technologies, I just read abot prefork and worker. Based on Apache2 documentation on Home windows the server uses Home windows API, so that you can migrate to Home windows or write module that may act as several paralel instances.

I believe that it may be related to a race condition that happens when several threads runs concurrently. The keyword static is just limits the scope from the variable, therefore it is not really a solution -- use something similar to mutexes with regards to the race condition exclusion. Talking about keeping some variable from a function calls inside a connection you will need to store this variable in connection-related structures (see request_rec->notes or request_rec->connection->notes for instance).