Page 1 of 2
Threads reading from the same file!
Posted: Fri May 01, 2009 1:56 pm
by srod
Hi,
I have a multithreaded application which generally runs fine.
However, I now have the main process and a separate thread both reading from the same binary file at the same time. I am experiencing problems with this - nothing major, but enough to suspect that there is a problem here and thus I am wondering if this is actually possible? It is probably not advisable, but that's not what I am after here!
Is it possible for two threads to read from the same file simultaneously under Windows?
The program is so complex that I am not 100% sure the problem is due to reading from the file; it could be a gdi problem between the threads etc.
Anyone have any experience with this?
I will probably switch to using a timer and have all file accesses performed within the main process; I suspect this will fix my problem!
Thanks.
Posted: Fri May 01, 2009 2:03 pm
by ts-soft
If you use different IDs (#PB_Any) and the exe is Threadsafe this should
not a problem.
Posted: Fri May 01, 2009 2:06 pm
by srod
Yes that's what I thought.
It is a strange problem for sure; either a file access problem or a corruption of gdi resources.
If a timer does not fix the problem then it is a gdi issue.

Posted: Fri May 01, 2009 2:18 pm
by freak
If the file isn't too big, why not just load it entirely into memory ?
Posted: Fri May 01, 2009 2:27 pm
by srod
It could be huge!!!!

We're talking 20 MB plus!
A timer fixed the problem.
That means it can still be some clash with the file access or a gdi corruption between the threads.
Must admit that I'd rather use a thread in this instance and so will look very carefully at the gdi routines!

Posted: Fri May 01, 2009 2:39 pm
by srod
Right, confirmed a GDI problem. Don't know what is causing it though?

Posted: Fri May 01, 2009 2:58 pm
by srod
No, I see nothing in the code which will cause the problems I am seeing; nothing which would explain the thread issue and the apparent GDI corruption.
I see instances in which the threads may attempt to load the same image into different DC's at the same time, which is a bug, but I have confirmed that this is not the cause of the problem in this case.
Very strange indeed!
Guess I may have to stick with the timer instead.
Posted: Fri May 01, 2009 3:20 pm
by freak
Are you expecting some kind of answer here or are you just talking to yourself ?
If you want some interaction, better tell us what you are actually doing GDI-wise in your thread/main program

Posted: Fri May 01, 2009 3:26 pm
by netmaestro
We're talking 20 MB plus!
I'd consider that easily manageable in memory by today's standards, unless you're looking to support older platforms without much memory. Almost anything today has at least 1 gig, most have more.
Posted: Fri May 01, 2009 3:35 pm
by srod
Just talking to myself really Timo! It's not the first time!
@Netmaestro : I am now pretty sure that it is nothing to do with the simultaneous accesses to the file in question as both threads seem to be pulling out all of the correct data. So whether I load the file into memory or not is pretty much immaterial right now I reckon. I feel more comfortable with this particular file remaining on disc anyhow.

Posted: Fri May 01, 2009 5:25 pm
by srod
@Freak, Netmaestro : I have proved that the problem is not inherently GDI, but to do with EnhancedMetafiles on Vista (and not the first such problem I have encountered!)
Do either of you have any experience with this?
Basically, I have the main process and a thread both calling the same routine which is absolutely threadsafe! The rountine creates a EMF, draws to it and then returns the appropriate handle.
As it is, the EMF's are showing slight corruptions; wrong fonts beind used and so on.
If I alter the routine in question so that rather than direct all GDI operations to a EMF, I instead direct it to an EMF for one thread and to a non-EMF hdc for another thread then everything runs fine; no corruption. The point being that the same GDI commands are still running in both threads and not corrupting one another!
Conclusion : Vista + threads + EMF's do not mix!!!
Have either of you encountered anything on your travels which might suggest a solution? Freak, I get the impression that there are not many coding 'nooks and crannies' on the web that you haven't visited at least once!
I have my timer workaround of course. Presumably I could employ a mutex to protect the routine in question, but still, I wouldn't mind avoiding these if I can!
Still, knowing exactly where the problem lies is a load off my mind!

I hate not knowing why something fails to work, and this is the third instance of EMF's not working properly on Vista which I have found. I'll bet my ass that this same code works fine on XP!
Thanks.
Posted: Fri May 01, 2009 5:32 pm
by netmaestro
I'll bet my ass that this same code works fine on XP
Not sure I'd want the prize...

Not using Vista here I can't test anything, so I won't be able to help, sorry.
Posted: Fri May 01, 2009 5:38 pm
by srod
Aye, not sure I'd want that prize either even if it came with $100000000 and a free holiday to Tenerife with Claudia Shiffer thrown in as my personal luggage carrier and sun-cream applicator!
Oh ho... cold shower time!
Posted: Sat May 02, 2009 12:57 am
by Ollivier
I'll bet my ass that this same code works fine on XP
What's an ass? A creation?
Posted: Sat May 02, 2009 1:10 am
by Ollivier
Found. It's a reproductive jackass. Again a catch...