WriteFile without blocking Critical priority thread

Posted on 2005-05-11
Medium Priority
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
  • 2

Expert Comment

ID: 13995808
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

ID: 14030412
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"

Accepted Solution

rcarlan earned 2000 total points
ID: 14030785
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.


Featured Post

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

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++ (http://www.experts-exchange.com/viewArticle.jsp?aid=1193) I described how to encrypt text and recommended that the encrypted text be stored as a series of hexadecimal digits -- because cyphertext may…
Are you ready to place your question in front of subject-matter experts for more timely responses? With the release of Priority Question, Premium Members, Team Accounts and Qualified Experts can now identify the emergent level of their issue, signal…
Whether it be Exchange Server Crash Issues, Dirty Shutdown Errors or Failed to mount error, Stellar Phoenix Mailbox Exchange Recovery has always got your back. With the help of its easy to understand user interface and 3 simple steps recovery proced…
Suggested Courses
Course of the Month13 days, 9 hours left to enroll

749 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