is WritePrivateProfileString thread safe?
-
Tuesday, March 06, 2012 7:27 AM
Hello,
I've searched this issue all over the internet and I found many inconsistent answers.
I have multiple threads on my MFC application.
Can I write from all the threads to an ini file using WritePrivateProfileString??
Is there any risk that the file will be locked by one thread while the other try to write to it?
Thank you for your help,
Amit
- Edited by Amit Erez Tuesday, March 06, 2012 7:27 AM
All Replies
-
Tuesday, March 06, 2012 10:53 AM
Wich version of Windows and compiler are you using because WritePrivateProfileString is an obsolet function ... compatibility with 16-bit versions of Windows ???
Delphine GARRO
-
Tuesday, March 06, 2012 11:49 AM
Thank you for your reply.
I'm working with microsoft visual studio 2005, Windows XP pro 32-bits.
-
Tuesday, March 06, 2012 11:54 AM
On 06/03/2012 08:27, Amit Erez wrote:
I have multiple threads on my MFC application.
Can I write from all the threads to an ini file using WritePrivateProfileString??
Is there any risk that the file will be locked by one thread while the other try to write to it?It seems that there is a similar thread here:
If I were you I'd do some tests, and in case of uncertainty I'd stay safe using a critical section to synchronize multi-thread access.
Unfortunately, there seems to be no explicit statement about thread safety in MSDN documentation for that API:
http://msdn.microsoft.com/en-us/library/windows/desktop/ms725501(v=vs.85).aspx
Giovanni
-
Tuesday, March 06, 2012 11:55 AM
On 06/03/2012 11:53, GARRO Delphine wrote:
Wich version of Windows and compiler are you using because WritePrivateProfileString is an obsolet function ... compatibility with 16-bit versions of Windows ???
Even if that API is marked as deprecate, I think it can be useful, and INI files can be faster (and easier to parse) than XML configuration files.
In several cases XML configuration files can be an "overkill".Giovanni
-
Thursday, March 08, 2012 5:09 AMI would think that WritePrivateProfileString is transactional, meaning you can write to it from various threads simultaneously. There is nothing that says multiple processes cannot be writing to the same .ini file at the same time, so that would imply that multiple threads in just your one process is also fine.Still, if it is a concern, you can use a critical section or reader-writer lock (Vista or later) to regulate your calls to this function. I would only do this if you notice a problem without it, as I wouldn’t expect one.-- David
Efficiently read and post to forums with newsreaders: http://communitybridge.codeplex.com -
Thursday, March 08, 2012 7:49 AM
Is there any risk that the file will be locked by one thread while the other try to write to it?
Threading is not documented in MSDN, so I would guess that synchronization is responsibility of the code. Otherwise, this guy looks like he knows what he's saying.
At any rate, you should not be writing to a config file from any old thread. What's wrong in keeping this information in-process and flushing it to disk only on exit?
If performance is your concern, then you should note that these functions open/close the file, parse it, convert any numbers to text and generally do so much that they are a total no-no if performance is an issue.
Finally... Ini files in this day and age? Just say no ;-)
-
Thursday, March 08, 2012 2:48 PMI stand corrected as far as the need to synchronize threads within the same process calling the Profile functions. :-)The main advantage of .ini files over things like the registry is that they are portable and can easily be xcopied along with other things.-- David
Efficiently read and post to forums with newsreaders: http://communitybridge.codeplex.com

