Waiting for file availability

This is a discussion on Waiting for file availability within the C++ Programming forums, part of the General Programming Boards category; Is there any portable way to wait until a file becomes available (assuming it exists)? For example, I thought of ...

  1. #1
    Kiss the monkey. CodeMonkey's Avatar
    Join Date
    Sep 2001
    Posts
    907

    Waiting for file availability

    Is there any portable way to wait until a file becomes available (assuming it exists)? For example, I thought of this:
    Code:
    //assuming some other app is using "file"...
    //wait until "file" is available, then delete it
    while( !std::fstream("file") );
    std::system("del file");
    Of course, speaking of portability, the contents of system() are not. Still, then I realized how inefficient my method waswas. Something more along the lines of this was in order:
    Code:
    while( !std::fstream("file") )   //check every 5 seconds
    {
       Sleep(5000);
    }
    std::system("del file");
    But then Sleep() is not portable! Am I out of luck? Is this os-specific?
    Last edited by CodeMonkey; 01-07-2007 at 10:25 PM.
    "If you tell the truth, you don't have to remember anything"
    -Mark Twain

  2. #2
    Kiss the monkey. CodeMonkey's Avatar
    Join Date
    Sep 2001
    Posts
    907
    well, there's always something like
    Code:
    namespace my_util
    {
       //with <ctime>
       inline const std::clock_t sleep(int duration,bool in_seconds = true)
       { 
          std::clock_t start = std::clock(NULL);
          while( std::clock(NULL) < start+duration*(in_seconds ? std::CLOCKS_PER_SEC : 1) );
          return std::CLOCKS_PER_SEC;
       }
    }
    But what about deleting a file?
    Last edited by CodeMonkey; 01-07-2007 at 11:09 PM. Reason: return type
    "If you tell the truth, you don't have to remember anything"
    -Mark Twain

  3. #3
    Registered User
    Join Date
    May 2006
    Posts
    903
    It seems remove() is the solution.

  4. #4
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    Quote Originally Posted by CodeMonkey
    well, there's always something like
    Code:
    namespace my_util
    {
       //with <ctime>
       inline const std::clock_t sleep(int duration,bool in_seconds = true)
       { 
          std::clock_t start = std::clock(NULL);
          while( std::clock(NULL) < start+duration*(in_seconds ? std::CLOCKS_PER_SEC : 1) );
          return std::CLOCKS_PER_SEC;
       }
    }
    Have you measured the CPU usage in your solution?
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  5. #5
    Kiss the monkey. CodeMonkey's Avatar
    Join Date
    Sep 2001
    Posts
    907
    It's, well..... absolutely gigantic. BTW, thanks Desolation.
    "If you tell the truth, you don't have to remember anything"
    -Mark Twain

  6. #6
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    I'd put something like
    Code:
    #ifdef WIN32
       Sleep(duration);
    #elif _UNIX
       sleep(duration/1000);
    #else
       ...
    #endif
    in my code to support different OSes when to go with the solution that eats my CPU time even if it is universal
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  7. #7
    Registered User
    Join Date
    May 2006
    Posts
    903
    Small variation on vart's code, just in case you want to use the timer more than once.

    Code:
    #ifdef WIN32
        #define SLEEP_MS(a) Sleep(a)
    #elif _UNIX
        #define SLEEP_MS(a) sleep( (a) / 1000 )
    #elif
        ...
    #endif

  8. #8
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    Quote Originally Posted by Desolation
    Small variation on vart's code, just in case you want to use the timer more than once.

    Code:
    #ifdef WIN32
        #define SLEEP_MS(a) Sleep(a)
    #elif _UNIX
        #define SLEEP_MS(a) sleep( (a) / 1000 )
    #elif
        ...
    #endif
    I prefer to create function named SLEEP_MS than macro.
    Didn't mean that my code should go in the place where we need to call Sleep,
    Instead it should be wrapped in some utility function and place in the file containing all OS adapters.
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  9. #9
    Kiss the monkey. CodeMonkey's Avatar
    Join Date
    Sep 2001
    Posts
    907
    Yes, that's the solution I've been finding online. Thank you.
    "If you tell the truth, you don't have to remember anything"
    -Mark Twain

  10. #10
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    Here An alternative wait function?
    CornedBee suggested even better solution that uses the same idea
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  11. #11
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    However, the idea of this thread is not portable in the first place. Unless the application that currently has the file open locked it explicitly, you can simply delete it in Linux (and probably most Unices). The immediate and strong locking that Windows does isn't the situation everywhere. In other words, deleting the file would succeed immediately, leaving the other application with a phantom file. Strictly speaking, you're only removing the symbolic name of the file from the directory - hence the term "unlink". The actual file, identified by its inode number, is still there, and stays there as long as anything holds a reference. And the open file descriptor of that other application is a reference. The file will be deleted from disk the moment the other application closes its file descriptor.
    The waiting is thus only necessary in Windows in the first place, which suggests implementing a platform-specific proper waiting method there. I have to admit, though, that I'm not sure what the right method is. LockFile(Ex) require an open handle, and I'm not sure if a statistics handle (the only that can be opened on an already locked file) is enough.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 7
    Last Post: 11-21-2009, 12:02 AM
  2. signal sending and waiting
    By eva69 in forum C Programming
    Replies: 1
    Last Post: 10-03-2008, 12:03 PM
  3. simultaneously waiting for data on FIFO and UDP using select call
    By yogesh3073 in forum Networking/Device Communication
    Replies: 2
    Last Post: 01-05-2007, 08:53 AM
  4. pclose without waiting for child
    By Largo in forum C Programming
    Replies: 1
    Last Post: 11-26-2005, 04:13 AM
  5. Getting input without waiting for keypress
    By Anon48 in forum C Programming
    Replies: 2
    Last Post: 04-08-2005, 02:18 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21