windows 少数线程并发 监听多个目录

windows 少数线程并发 监听多个目录

图中的watch_thread可以开多个,不过效果暂时没有详测,对于不是特频繁(可能是100个线程同时更新?)的文件变化,单个watch_thread应该就够了


关键sys_api:

windows:

1.createfile(dirPath, flag_overlap),flag_overlap表示异步操作

2.createiocompletionstatus(dirHandle, port, key),生成一个共用的port,关联多个key(不同dirHandle需要不同的key),监控io操作的完成

3.getqueuedcompletionstatus(port, &key),通过监听port,来监听io操作完成后返回的结果

4.readdirectorychangesw(dirHandle, buf), 异步请求io操作(读取目录变化),完成后getqueuedcompletionstatus接口就会结果


有些注意的细节:

       http://blog.sina.com.cn/s/blog_62b4e3ff0100nu84.html getQueuedCompletionStatus返回true但lpNumberOfBytes为0??


其他方案(个人想法):

1.just use overlap, read https://github.com/emcrisostomo/fswatch/blob/master/libfswatch/src/libfswatch/c%2B%2B/windows
this way it will loop on one thread always, and the interval is not good to decide i think.
2.use qt#waitformultipleobjects like qfilesystemwatcher
this #waitformultixx has a limit of less than 64 objects, so i want to forget it.
3.use this lib https://github.com/Enseed/CDirectoryChangeWatcher
it doesn't support watching on multiple dirs, though it's realization is nearly the same as my module described above.


该方案的原因:

1.it can create several threads to concurrently handle multiple io completions from #readdirectorychangesw,
which may be better than when there is only one thread to handle them.
2.do work only when there is need, avoid waste loop on any thread, let app get more resources to run, to perform better.