WriteFile without blocking Critical priority thread

Posted on 2005-05-11
Last Modified: 2012-05-05

Hi, I have a CRITICAL_PRIORITY thread that must absolutely perform work every 20ms.
From this thread, I try to write a 400 byte message to disk.  However if the disk subsystem is bogged down or other, this can take longer than the acceptable amount of time.

Is there a safe way to write to sume buffered thread that will then write to disk?
But I can't have the Critical thread be interrupted for anything... so how can I pass the data to the other thread properly?

Do I have to use somewhat experimental "lockless" data structures?
Question by:jyukes
    LVL 9

    Expert Comment

    Open the file with FILE_FLAG_OVERLAPPED and use WriteFileEx to write to it. This will give you async write to file (i.e. WriteFileEx will return immediately).


    Author Comment

    Okay I think there are problems with FILE_FLAG_OVERLAPPED though...

    "Asynchronous Disk I/O Appears as Synchronous on Windows NT, Windows 2000, and Windows XP";en-us;156932
    LVL 9

    Accepted Solution

    Interesting article. Very pertinent analysis.

    What it basically says is that under certain circumstances asynchronous requests may be processed synchronously, and your code should be written in a way that can cope with both synchronous and asynchronous execution.

    In some of these circumstances, the synchronous execution is desired, as it’s the fastest way to execute the command (e.g. when the data is available in the cache). Even for a real time program this shouldn’t be a real concern, as the synchronous execution is almost instantaneous (i.e. it would basically take just as long, if not longer, to set up the background execution).

    Others circumstances forcing synchronous execution are a result of certain limitations in the kernel and cache manager (i.e. dealing with compressed files, increasing the file size, number of background threads in the cache manager). Out of these, the one that surprises me is the files size limitation. The others are sort of logical / explainable.

    Anyway, you’ll have to decide for yourself to what extend these limitations are a concern for your program. You could always implement your own pool of background threads for asynchronous execution of file operations. You can simply follow the FILE_FLAG_OVERLAPPED model or you can design your own system.

    AFAIK, FILE_FLAG_OVERLAPPED is the only support the O/S provides. If it doesn’t deliver according to your expectations / requirements, you’re on your own :-)  You may want to have a look at the Boost library, but if you’re so concerned with performance due to your real time requirements, you may be better off doing the hard work yourself.

    Good luck.


    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    How to improve team productivity

    Quip adds documents, spreadsheets, and tasklists to your Slack experience
    - Elevate ideas to Quip docs
    - Share Quip docs in Slack
    - Get notified of changes to your docs
    - Available on iOS/Android/Desktop/Web
    - Online/Offline

    The following diagram presents a diamond class hierarchy: As depicted, diamond inheritance denotes when two classes (e.g., CDerived1 and CDerived2), separately extending a common base class (e.g., CBase), are sub classed simultaneously by a fourt…
    In Easy String Encryption Using CryptoAPI in C++ ( I described how to encrypt text and recommended that the encrypted text be stored as a series of hexadecimal digits -- because cyphertext may…
    Sending a Secure fax is easy with eFax Corporate ( First, Just open a new email message.  In the To field, type your recipient's fax number You can even send a secure international fax — just include t…
    This video discusses moving either the default database or any database to a new volume.

    760 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    10 Experts available now in Live!

    Get 1:1 Help Now