I have multi threaded application, and everythreat logging some error messages and logging process but, I dont know how to log multiple threat's working in a text file. Could you help me.
Thanx you in advance...
I have multi threaded application, and everythreat logging some error messages and logging process but, I dont know how to log multiple threat's working in a text file. Could you help me.
Thanx you in advance...
Easiest solution is to have a logging framework that queues writes for handling by a single thread (or synchronises write operations so only one thread at a time can write).
If you don't do that you're running the very real risk of simultaneous write attempts causing multiple threads to attempt writes at the same filepointer position, leading to (at best) loss of data, deadlock, and/or crashes.
Yes its risky task more than just open a file and write in it. Is there any available framework for the logging purpose? or may I achieve success by just using semaphores or something similar.
Thanx u in advance
Why not use mutexes?
"The Internet treats censorship as damage and routes around it." - John Gilmore
Use boost::thread and boost::mutex
Ok. I will create critical section and monitor resources. Another thing make me confused is print screen. Do multiple threats print something to the screen without conflicting? is there any queue mechanism for display or I have to apply same mechanism to prevent that as well?
create a mutex. then every cout/cerr operation should be enclosed like this:
I do it like this and it prevents terminal output goulashCode:lock stdoutmutex cout << ... << endl; unlock stdoutmutex
I apreciate your help. Thanks a lot, and pheres It really works I love it. thanx again
I believe that the threaded library at least on MS Visual studio can cope with multiple threads writing to the same file - at least at the basic level of "not crashing" - your output may be scrambled if you use things like fprintf or fputc - but if you use one fputs or fwrite to output the data, then it should be fine.
As to whether other compiler/C-libraries cope with this OK or not is a different story.
--
Mats