gpt4 book ai didi

c++ - 在写入或读取 CFile 对象的过程中文件指针可以更改吗

转载 作者:太空宇宙 更新时间:2023-11-04 12:28:24 25 4
gpt4 key购买 nike

我有一个 CFile 对象,可以被多个线程访问。有可能一个线程正在将数据写入此文件,而另一个线程正在从文件中读取数据。我想知道这个政策有没有不安全的地方?文件指针能否在写入或读取过程完成之前更改?答案是肯定的,如何避免多线程同时读写的不安全?

非常感谢!

最佳答案

CFile 对象不是线程安全的。如果您需要从多个线程访问它们,则需要执行您自己的同步。

来自 http://msdn.microsoft.com/en-us/library/aa270950.aspx :

Accessing Objects from Multiple Threads

For size and performance reasons, MFC objects are not thread-safe at the object level, only at the class level. This means that you can have two separate threads manipulating two different CString objects, but not two threads manipulating the same CString object. If you absolutely must have multiple threads manipulating the same object, protect such access with appropriate Win32 synchronization mechanisms, such as critical sections. For more information on critical sections and other related objects, see in the Win32 SDK.

The class library uses critical sections internally to protect global data structures, such as those used by the debug memory allocation.

关于c++ - 在写入或读取 CFile 对象的过程中文件指针可以更改吗,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/724347/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com