とっちゃん's Blog

WindowsInstaller に WiX はいかがですか~

目次

Blog 利用状況

ニュース

とっちゃんって?

コミュニティ

@ITの記事

CodeZineの記事

WiX チュートリアル

Windows ユーザー エクスペリエンス ガイドライン

唯一の日本語書籍

記事カテゴリ

書庫

日記カテゴリ

インストーラ関連

旧館

WriteFile における同期と非同期とは...

ネタ元:[OpenMP]分身!

WriteFile/ReadFile は同期I/Oと、非同期I/Oの2つのI/Oモードに対応したAPIです。I/Oの同期非同期と、スレッド間同期は異なる概念のものなので混同してはいけません。

ということで、ネタ元でどうしても納得できない部分がここ。

あっと、WriteConsole 関数のリファレンスに「WriteFile 関数に似た動作をします」と書いてある WriteFile 関数のリファレンスには、「この関数は、同期と非同期両方の操作を想定して設計されています。」と、書かれています。出力をバッファリングして、対象のストリームへ非同期に処理するようです。ということは、「スレッド セーフな関数を使って表示する。 Print the parameter values using thread-safe functions.」というコメントが、ヘン?この「using thread-safe functions.」というのは、「非同期処理」の意かと思われます。

リファレンスから引用した部分を文章として完結する形で改めて引っ張りなおしてみます(「」の部分です)。

WriteConsole のリファレンスは解説部分の「WriteConsole 関数は、コンソールスクリーンバッファに文字を書き込みます。WriteFile 関数に似た動作をしますが、WriteConsole 関数は Unicode( ワイド文字)と ANSI のどちらのモードでも書き込みが可能です。」を引用したと思われます。

WriteFile のリファレンスのほうは、冒頭部分「ファイルにデータを書き込みます。この関数は、同期と非同期両方の操作を想定して設計されています。ファイルポインタの現在の位置が、書き込みの開始位置になります。書き込みが完了すると、ファイルポインタの位置は、実際に書き込んだバイト数だけ進みます。FILE_FLAG_OVERLAPPED を指定してファイルを開いた場合は、この限りではありません。オーバーラップ I/O(非同期 I/O)を指定してファイルのハンドルを作成した場合は、アプリケーション側でファイルポインタを調整してください。」だと思われます。

改めて書きます。

WriteFile の冒頭にある同期、非同期は I/O の同期、非同期であって、マルチスレッドプログラムにおける同期化処理(同期制御)とは異なるものです。したがって(おそらくはPOSIX文書のコメントと思われる)、「スレッドセーフな...」という部分を当てはめることはできないのではないか?と思われます。


せっかくなので、同期I/Oと非同期I/Oについて。

本当は、Blogでのつたない説明よりも、「Win32マルチスレッドプログラミング」(ISBN4-7561-1404-0:絶賛絶版中)という書籍を読んでもらったほうが、ずっとわかりやすいし充実した内容なのですが、いかんせん絶版になって久しい書籍だし、おそらく一刷しかしてないと思うので、現在では入手そのものがほぼ不可能なのではないか?と思われます。ちなみに、出版はAddison-Wesleyです。

 

ま、ないものをねだっても仕方がないので、大雑把ですが、簡単な解説を。

最初に書いたように、Windowsのファイル操作には、同期I/Oと呼ばれるものと非同期I/Oと呼ばれるものの2種類のI/Oモードがあります。同期I/Oは、要求した処理をその場で処理するモードで、APIを呼び出したら、そのAPIから帰ってきた時点で実際の処理も終了しているというものです。多くのプログラムのファイル操作がこの同期I/Oを用いています。

これに対し、非同期I/Oは要求した処理をその場では行わず、処理の予約だけを行い、API呼び出し時点ではない別の適切なタイミングを見計らって実際のファイル操作を行うモードとなります。そのため、APIから帰ってきた時点では処理が終わっていないため、対象となるバッファや追加情報などを処理終了まで保持しておかなければならないという制約があります。

Windows ではこの非同期I/Oに2種類のモードを用意しています。一つは使い方が比較的単純な Overlapped I/O と呼ばれるもので、処理の終了通知にHANDLEか、手動イベントのいずれかを使って通知する(どちらを使うかはOVERLAPPED構造体の内容次第)仕組みで動作します。こちらは、いわゆる同期オブジェクトを用いた処理となるため、MsgWaitForMultipleObjects などを用いてメインスレッド側で適宜処理を行うなどが可能になっているため、比較的創りやすい仕組みになっています。

もう一つはAPC(非同期プロシージャコール:Asyncronous Procedure Callの略)と呼ばれる、非同期の終了通知関数を使った処理となります。こちらは、ReadFileEx/WriteFileEx というEx付きのAPIを使います。ほぼ同じ形で.NET Framework の FileStream にも実装されています(BeginRead/BeginWrite)ので、利用したことがある人もいるかもしれません。

 

昨今話題の並列処理、あるいは同時実行処理(並行処理)のボトルネックと呼ばれるものにファイルアクセスがあります。ですがこれにも非同期処理モードがあります。うまく活用すれば、シーケンシャルではあるものの複数の処理を分散させて行うことができるなどうまく活用することができる場面もあるかもしれません。

モバイル系OSと共有しているフレームワーク的なものの場合、利用は難しいかもしれませんが、デスクトップアプリあるいはサーバーアプリだけなら、利用を制限するようなOS(9x系OS)はすでに駆逐されたに等しい状況です。積極的に活用しても怒られない時代になったといえるでしょう。

 

 

まぁどうでもいい話題ですが、ファイルアクセスの非同期処理は、別にWindowsNTだからではなく、MS-DOSや、PC-8801mkIIなんかの時代にもあったものです。誰もが使うとか簡単に使えるというものではありませんでしたが、高速化のテクニックとしては30年近く前のPCゲームですでに使われていたものだということだけ付け加えておきます。

投稿日時 : 2010年10月26日 17:49

コメントを追加

# This is very fascinating, You are an overly professional blogger. I have joined your feed and look forward to in search of more of your magnificent post. Also, I've shared your web site in my social networks 2019/04/03 15:06 This is very fascinating, You are an overly profes

This is very fascinating, You are an overly professional blogger.

I have joined your feed and look forward to in search of more of your magnificent post.
Also, I've shared your web site in my social networks

# all the time i used to read smaller articles which as well clear their motive, and that is also happening with this article which I am reading at this time. 2019/04/08 9:19 all the time i used to read smaller articles which

all the time i used to read smaller articles which as well
clear their motive, and that is also happening with this article which I am reading at this time.

# Excellent post. I was checking continuously this weblog and I'm inspired! Extremely helpful information specially the remaining part :) I take care of such info much. I was looking for this certain info for a long time. Thanks and best of luck. 2019/04/10 19:13 Excellent post. I was checking continuously this w

Excellent post. I was checking continuously this weblog and I'm inspired!
Extremely helpful information specially the remaining part :) I take care of such
info much. I was looking for this certain info for a long time.
Thanks and best of luck.

# I am in fact thankful to the holder of this site who has shared this great post at at this time. 2019/05/02 19:53 I am in fact thankful to the holder of this site w

I am in fact thankful to the holder of this site who has shared this great post at at this time.

# Hello, I enjoy reading through your post. I like to write a little comment to support you. 2019/05/12 16:34 Hello, I enjoy reading through your post. I like t

Hello, I enjoy reading through your post. I like to write a little comment to support you.

# With havin so much content and articles do you ever run into any problems of plagorism or copyright infringement? My blog has a lot of completely unique content I've either authored myself or outsourced but it looks like a lot of it is popping it up a 2019/05/17 11:14 With havin so much content and articles do you ev

With havin so much content and articles do you ever run into
any problems of plagorism or copyright infringement?
My blog has a lot of completely unique content I've either authored myself or
outsourced but it looks like a lot of it is popping it up all
over the internet without my authorization. Do you
know any solutions to help prevent content from being stolen? I'd
really appreciate it.

# It's very simple to find out any matter on net as compared to books, as I found this article at this web page. 2019/07/01 21:49 It's very simple to find out any matter on net as

It's very simple to find out any matter on net as compared to books, as I found this article at this web page.

# Hi there just wanted to give you a quick heads up. The text in your content seem to be running off the screen in Chrome. I'm not sure if this is a format issue or something to do with web browser compatibility but I thought I'd post to let you know. The 2019/07/17 0:01 Hi there just wanted to give you a quick heads up.

Hi there just wanted to give you a quick heads up.
The text in your content seem to be running off the screen in Chrome.
I'm not sure if this is a format issue or something to do with web browser compatibility but
I thought I'd post to let you know. The layout look great though!
Hope you get the problem resolved soon. Cheers

# Hi there just wanted to give you a quick heads up. The text in your content seem to be running off the screen in Chrome. I'm not sure if this is a format issue or something to do with web browser compatibility but I thought I'd post to let you know. The 2019/07/17 0:02 Hi there just wanted to give you a quick heads up.

Hi there just wanted to give you a quick heads up.
The text in your content seem to be running off the screen in Chrome.
I'm not sure if this is a format issue or something to do with web browser compatibility but
I thought I'd post to let you know. The layout look great though!
Hope you get the problem resolved soon. Cheers

# Hi there just wanted to give you a quick heads up. The text in your content seem to be running off the screen in Chrome. I'm not sure if this is a format issue or something to do with web browser compatibility but I thought I'd post to let you know. The 2019/07/17 0:03 Hi there just wanted to give you a quick heads up.

Hi there just wanted to give you a quick heads up.
The text in your content seem to be running off the screen in Chrome.
I'm not sure if this is a format issue or something to do with web browser compatibility but
I thought I'd post to let you know. The layout look great though!
Hope you get the problem resolved soon. Cheers

# Hi there just wanted to give you a quick heads up. The text in your content seem to be running off the screen in Chrome. I'm not sure if this is a format issue or something to do with web browser compatibility but I thought I'd post to let you know. The 2019/07/17 0:04 Hi there just wanted to give you a quick heads up.

Hi there just wanted to give you a quick heads up.
The text in your content seem to be running off the screen in Chrome.
I'm not sure if this is a format issue or something to do with web browser compatibility but
I thought I'd post to let you know. The layout look great though!
Hope you get the problem resolved soon. Cheers

# Undeniably believe that which you stated. Your favorite justification appeared to be on the internet the easiest thing to be aware of. I say to you, I certainly get irked while people think about worries that they plainly do not know about. You managed 2019/08/23 20:31 Undeniably believe that which you stated. Your fav

Undeniably believe that which you stated. Your favorite justification appeared to be on the internet the easiest thing to be aware of.

I say to you, I certainly get irked while people think about worries that they plainly
do not know about. You managed to hit the nail upon the top and also defined out the whole thing without having side effect , people can take a signal.
Will likely be back to get more. Thanks

# Undeniably believe that which you stated. Your favorite justification appeared to be on the internet the easiest thing to be aware of. I say to you, I certainly get irked while people think about worries that they plainly do not know about. You managed 2019/08/23 20:32 Undeniably believe that which you stated. Your fav

Undeniably believe that which you stated. Your favorite justification appeared to be on the internet the easiest thing to be aware of.

I say to you, I certainly get irked while people think about worries that they plainly
do not know about. You managed to hit the nail upon the top and also defined out the whole thing without having side effect , people can take a signal.
Will likely be back to get more. Thanks

# Undeniably believe that which you stated. Your favorite justification appeared to be on the internet the easiest thing to be aware of. I say to you, I certainly get irked while people think about worries that they plainly do not know about. You managed 2019/08/23 20:33 Undeniably believe that which you stated. Your fav

Undeniably believe that which you stated. Your favorite justification appeared to be on the internet the easiest thing to be aware of.

I say to you, I certainly get irked while people think about worries that they plainly
do not know about. You managed to hit the nail upon the top and also defined out the whole thing without having side effect , people can take a signal.
Will likely be back to get more. Thanks

# Undeniably believe that which you stated. Your favorite justification appeared to be on the internet the easiest thing to be aware of. I say to you, I certainly get irked while people think about worries that they plainly do not know about. You managed 2019/08/23 20:34 Undeniably believe that which you stated. Your fav

Undeniably believe that which you stated. Your favorite justification appeared to be on the internet the easiest thing to be aware of.

I say to you, I certainly get irked while people think about worries that they plainly
do not know about. You managed to hit the nail upon the top and also defined out the whole thing without having side effect , people can take a signal.
Will likely be back to get more. Thanks

# If you are going for finest contents like me, only pay a visit this web page all the time since it presents quality contents, thanks 2019/09/03 16:52 If you are going for finest contents like me, only

If you are going for finest contents like me, only pay a visit
this web page all the time since it presents quality contents, thanks

# If you are going for finest contents like me, only pay a visit this web page all the time since it presents quality contents, thanks 2019/09/03 16:53 If you are going for finest contents like me, only

If you are going for finest contents like me, only pay a visit
this web page all the time since it presents quality contents, thanks

# If you are going for finest contents like me, only pay a visit this web page all the time since it presents quality contents, thanks 2019/09/03 16:54 If you are going for finest contents like me, only

If you are going for finest contents like me, only pay a visit
this web page all the time since it presents quality contents, thanks

# If you are going for finest contents like me, only pay a visit this web page all the time since it presents quality contents, thanks 2019/09/03 16:55 If you are going for finest contents like me, only

If you are going for finest contents like me, only pay a visit
this web page all the time since it presents quality contents, thanks

# ロレックス 偽物 修理 vaio 2022/11/29 19:27 iwswmbmixq@softbank.ne.jp

限定SALE!
人気限定モデル!
全国無料店舗!
大得価セールス中!
正規品人気定番!
【新商品!】送料込☆セール!
新作大SALE☆【送料無料!】
※激安人気新品※
人気直営店、超激安!
超格安、送料無料,5☆大好評!
【新商品!】制作精巧!
新作極上本革バッグ!
【最新入荷】男女兼用
大売り出しSALE!
日本正規専門店,激安販売中!

# Hi just wanted to give you a brief heads up and let you know a few of the images aren't loading properly. I'm not sure why but I think its a linking issue. I've tried it in two different internet browsers and both show the same results. 2024/04/03 5:34 Hi just wanted to give you a brief heads up and le

Hi just wanted to give you a brief heads up and let
you know a few of the images aren't loading properly. I'm not sure why but I think its
a linking issue. I've tried it in two different internet browsers and both show the same
results.

タイトル
名前
URL
コメント