简体   繁体   中英

Multiple Threads, one at a time read/write

There are two QFile-pointer pointing to the same file, they belong different threads. One writes data to the file, the other reads. But I want to have only one operation going on, read or write.

if(!this->file->exists()) {
   qDebug()<<"The file is not exists";
}
if(this->file->isOpen()) {
   qDebug()<<"The file is open";
}

Let me put it another way: how to check whether the file in opened?

I test file.isOpen() ? It doesn't work. How can I do that?

Sounds like to me you're trying to use isOpen to see if another thread has the same file open. AFAIK isOpen just tells you if the current QFile instance has a file open, not other QFile instances.

The only way I'm aware of being able to prevent different threads from reading and/or writing the same file is with file locking.

Not too sure if Qt has a way of doing this without having to delve into the more OS dependent APIs.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM