Thread: If Confusion

  1. #1
    Registered User
    Join Date
    Sep 2002
    Posts
    11

    Unhappy If Confusion

    I've got a problem with the following code:

    if (bLesResent == TRUE) // Set in ResendLesThread - okay
    {
    CheckSODFile(); //Problem line
    ConsolePrintf( " bStartOfDay = %d bLesResent = %d\n",
    bStartOfDay, bLesResent );
    }

    void CheckSODFile(void)
    {

    if(access( REDISTRIBUTEFILE, 0 ) != -1 )
    {
    ConsolePrintf( "%s exists\n", REDISTRIBUTEFILE );
    bStartOfDay = TRUE; // if the file is there - set to True
    }
    else
    ConsolePrintf( "Cannot find %s\n", REDISTRIBUTEFILE );

    return;
    }

    If I do CheckSODFile where it is above, it cannot find the file. If I do it before checking bLesResent - before the 1st IF, it can find the file.

    I'm a little confused? Any thoughts?

    Cheers

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Well since you mention threads, are you
    a) using thread-safe libraries
    b) using thread syncronisation primitives like semaphores and locks
    c) declaring variables which are shared between threads as volatile?
    Eg
    volatile int bLesResent;
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Sep 2002
    Posts
    11
    Salem,

    a) using thread-safe libraries
    - Not sure. This is for a Netware 3.2 NLM, and I am using standard C libraries, and the NLM libraries from Watcom10.

    b) using thread syncronisation primitives like semaphores and locks
    - There are Semaphores within this program, but I'm not sure what their purpose is.

    c) declaring variables which are shared between threads as volatile?
    Eg
    volatile int bLesResent;
    - Didn't know you could do this? Had a quick look on MSDN, and it says this is best for variables that can change at any time - particulary good for my purposes when I have several threads running at once. I'll give this a shot.

    This is an old program (1992) which I am adding new functionality to, and I'm still learning myself. Any help is greatly appreciated.

  4. #4
    Registered User
    Join Date
    Sep 2002
    Posts
    11
    Setting the variable bLesResent to volatile didn't make any difference.

    I'm trying to use fopen (FILE, "r") to check for file existence now. access appears to be a Windows function. Playing it safe with fopen.

    This is my new code:

    void CheckSODFile(void)
    {

    if( (RedisFile = fopen( REDISTRIBUTEFILE, "r" ) == NULL )
    {
    ConsolePrintf( "Cannot find %s\n", REDISTRIBUTEFILE );
    }
    else
    {
    ConsolePrintf( "%s exists\n", REDISTRIBUTEFILE );
    bStartOfDay = TRUE; // if the file is there - set to True
    }

    return;
    }

    ...
    ..
    .
    if (bLesResent == TRUE) // Set in ResendLesThread - okay
    {
    CheckSODFile(); // Still doesn't work!!!
    }

    ConsolePrintf( " bStartOfDay = %d bLesResent = %d\n",
    bStartOfDay, bLesResent );
    ...
    ..
    .


    I'm baffled. This works outside, but not within the IF.

    ?!?!?!

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > I'm trying to use fopen (FILE, "r") to check for file existence now
    I didn't see an fclose()

    You're absolutely sure that CheckSODFile(); is being called when it's inside the if statement?

    Is bStartOfDay not being set, or are you relying on your trace output from ConsolePrintf?.
    From past experience, if you try and log too much information in a short space of time, then some logging systems will drop some output, giving you the impression that something isn't happening, when in reality it is.

    Do you have any kind of debugger?

    What does this do?
    Code:
    CheckSODFile(); // works?
    if (bLesResent == TRUE) // Set in ResendLesThread - okay
    { 
        CheckSODFile(); // Still doesn't work!!!
    }
    Perhaps adding this to your code may help
    Code:
    int CheckSODFile_count=0;
    void CheckSODFile(void)
    {
        CheckSODFile_count++;
        // rest of code
    }
    Then at some point later, either using a debugger or using ConsolePrintf, look at the counter and see if that matches your expectations.
    In gcc at least, the increment of a variable like that takes one instruction, so even if its being called rapidly by multiple threads, the count should still be accurate.

    > This is an old program (1992) which I am adding new functionality to
    Ouch!
    This could be a lot trickier then.

    My first step would be to compile the original code with the new compiler and libraries and make sure it still works. This would at least eliminate some possibilities that the original code was not as thread safe as it ought to be.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  6. #6
    Registered User
    Join Date
    Sep 2002
    Posts
    11
    I didn't see an fclose()
    - I'm using fclose() and remove() later on in the code

    You're absolutely sure that CheckSODFile(); is being called when it's inside the if statement?
    Is bStartOfDay not being set, or are you relying on your trace output from ConsolePrintf?.
    - I am relying on the ConsolePrintf to tell me where I am in the code. I print out to screen when I am checking the existence of the file, whether true or not. I get the ConsolePrintf() printed to screen each time, whether true or false.

    From past experience, if you try and log too much information in a short space of time, then some logging systems will drop some output, giving you the impression that something isn't happening, when in reality it is.

    Do you have any kind of debugger?
    - No. Legacy system that is to be replaced. Half our development software went 'missing' after redundancies a while back, and I've got to 'make do'.

    What does this do?

    - Sorry, this was to illustrate what I am doing. CheckSODFile() finds the file outside the IF, but inside it doesn't.

    code:--------------------------------------------------------------------------------
    CheckSODFile(); // works?
    if (bLesResent == TRUE) // Set in ResendLesThread - okay
    {
    CheckSODFile(); // Still doesn't work!!!
    }
    --------------------------------------------------------------------------------

    Thanks for your help.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Terrible confusion with time variables
    By LowlyIntern in forum C++ Programming
    Replies: 12
    Last Post: 08-01-2008, 07:23 AM
  2. C++ Classes: Use, Misuse...Confusion.
    By Snorpy_Py in forum C++ Programming
    Replies: 4
    Last Post: 10-23-2006, 01:46 AM
  3. for loop confusion
    By Enges in forum C++ Programming
    Replies: 6
    Last Post: 04-26-2006, 08:21 AM
  4. Server-net newbie confusion
    By geek@02 in forum Windows Programming
    Replies: 1
    Last Post: 04-28-2005, 02:08 AM
  5. confusion with increment and decrement operators
    By cBegginer in forum C Programming
    Replies: 6
    Last Post: 03-19-2005, 03:45 PM