Threads reading from the same file!

Everything else that doesn't fall into one of the other PB categories.
srod
PureBasic Expert
PureBasic Expert
Posts: 10589
Joined: Wed Oct 29, 2003 4:35 pm
Location: Beyond the pale...

Threads reading from the same file!

Post 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.
I may look like a mule, but I'm not a complete ass.
User avatar
ts-soft
Always Here
Always Here
Posts: 5756
Joined: Thu Jun 24, 2004 2:44 pm
Location: Berlin - Germany

Post by ts-soft »

If you use different IDs (#PB_Any) and the exe is Threadsafe this should
not a problem.
PureBasic 5.73 | SpiderBasic 2.30 | Windows 10 Pro (x64) | Linux Mint 20.1 (x64)
Old bugs good, new bugs bad! Updates are evil: might fix old bugs and introduce no new ones.
Image
srod
PureBasic Expert
PureBasic Expert
Posts: 10589
Joined: Wed Oct 29, 2003 4:35 pm
Location: Beyond the pale...

Post 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.

:)
I may look like a mule, but I'm not a complete ass.
freak
PureBasic Team
PureBasic Team
Posts: 5962
Joined: Fri Apr 25, 2003 5:21 pm
Location: Germany

Post by freak »

If the file isn't too big, why not just load it entirely into memory ?
quidquid Latine dictum sit altum videtur
srod
PureBasic Expert
PureBasic Expert
Posts: 10589
Joined: Wed Oct 29, 2003 4:35 pm
Location: Beyond the pale...

Post 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! :)
I may look like a mule, but I'm not a complete ass.
srod
PureBasic Expert
PureBasic Expert
Posts: 10589
Joined: Wed Oct 29, 2003 4:35 pm
Location: Beyond the pale...

Post by srod »

Right, confirmed a GDI problem. Don't know what is causing it though? :)
I may look like a mule, but I'm not a complete ass.
srod
PureBasic Expert
PureBasic Expert
Posts: 10589
Joined: Wed Oct 29, 2003 4:35 pm
Location: Beyond the pale...

Post 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.
I may look like a mule, but I'm not a complete ass.
freak
PureBasic Team
PureBasic Team
Posts: 5962
Joined: Fri Apr 25, 2003 5:21 pm
Location: Germany

Post by freak »

Are you expecting some kind of answer here or are you just talking to yourself ? :P

If you want some interaction, better tell us what you are actually doing GDI-wise in your thread/main program ;)
quidquid Latine dictum sit altum videtur
User avatar
netmaestro
PureBasic Bullfrog
PureBasic Bullfrog
Posts: 8453
Joined: Wed Jul 06, 2005 5:42 am
Location: Fort Nelson, BC, Canada

Post 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.
BERESHEIT
srod
PureBasic Expert
PureBasic Expert
Posts: 10589
Joined: Wed Oct 29, 2003 4:35 pm
Location: Beyond the pale...

Post 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. :)
I may look like a mule, but I'm not a complete ass.
srod
PureBasic Expert
PureBasic Expert
Posts: 10589
Joined: Wed Oct 29, 2003 4:35 pm
Location: Beyond the pale...

Post 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! :wink:

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.
I may look like a mule, but I'm not a complete ass.
User avatar
netmaestro
PureBasic Bullfrog
PureBasic Bullfrog
Posts: 8453
Joined: Wed Jul 06, 2005 5:42 am
Location: Fort Nelson, BC, Canada

Post by netmaestro »

I'll bet my ass that this same code works fine on XP
Not sure I'd want the prize... :shock: Not using Vista here I can't test anything, so I won't be able to help, sorry.
BERESHEIT
srod
PureBasic Expert
PureBasic Expert
Posts: 10589
Joined: Wed Oct 29, 2003 4:35 pm
Location: Beyond the pale...

Post 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!
I may look like a mule, but I'm not a complete ass.
Ollivier
Enthusiast
Enthusiast
Posts: 281
Joined: Mon Jul 23, 2007 8:30 pm
Location: FR

Post by Ollivier »

I'll bet my ass that this same code works fine on XP
What's an ass? A creation?
Ollivier
Enthusiast
Enthusiast
Posts: 281
Joined: Mon Jul 23, 2007 8:30 pm
Location: FR

Post by Ollivier »

Found. It's a reproductive jackass. Again a catch...
Post Reply