madiha malik
madiha malik

Reputation: 977

Start of thread changes already set global variable

I have ran into very strange behavior of my code, the basic flow of code is

main () parses a file and sets global variables accordingly.. such as

int frame_size, version;
  typedef struct//file parsing variables
    {
        int frame,
        int version; } configuration;
***//the function init_parse calls***
    static int handler(void* user, const char* section, const char* name,
                       const char* value)
    {
        configuration* pconfig = (configuration*)user;

    #define MATCH(s, n) strcmp(section, s) == 0 && strcmp(name, n) == 0
    if (MATCH("protocol", "version")) {
        pconfig->version = atoi(value);
    } 
    else if (MATCH("basic", "frames")) {
            pconfig->frames= atoi(value);
            frame_size=pconfig->frames;
                        }
    else {
        return 0;  /* unknown section/name, error */
    }
    return 1;
}

main (){
        configuration config;
    if (ini_parse("test.ini", handler, &config) < 0) {
                printf("Can't load 'test.ini'\n");
                getchar();
iret = pthread_create(&hThread,NULL, pcapreader, NULL);
    if(iret)
    {
        fprintf(stderr,"Error - pthread_create() return code: %d\n",iret);
        exit(EXIT_FAILURE);
    }

        }

Now, the line followed by main()'s parsing line, everything seems set, but as soon as thread is started , the value frame_size changes to something 6345720:/

I have double checked code for possible replicated variable. thread only uses frame_size in for loop to check the limit.

Upvotes: 0

Views: 56

Answers (2)

madiha malik
madiha malik

Reputation: 977

the only problem was with initialization, once initialized, everything worked like a charm :)

Upvotes: 1

Ilho Yu
Ilho Yu

Reputation: 28

I think it might never initialize the frame_size variable and never reached MATCH("basic", "frames") statement too.

Upvotes: 0

Related Questions