Multiple threads

A forum for questions or concerns related to the PDF-XChange Core API SDK

Moderators: TrackerSupp-Daniel, Tracker Support, Vasyl-Tracker Dev Team, Chris - Tracker Supp, Sean - Tracker, Tracker Supp-Stefan

Forum rules
DO NOT post your license/serial key, or your activation code - these forums, and all posts within, are public and we will be forced to immediately deactivate your license.

When experiencing some errors, use the IAUX_Inst::FormatHRESULT method to see their description and include it in your post along with the error code.
Post Reply
pschlan
User
Posts: 10
Joined: Thu Nov 15, 2018 10:14 pm

Multiple threads

Post by pschlan »

Hi,

we're using the PDFCore API (license with username bytesart) to process PDF files. Our application has multiple independent worker threads. So far we use CoInitialize(), i.e. we have a single-threaded apartment. In every thread we create our own IPXC_Inst and we do *not* pass it across thread boundaries.

Now assume we have two threads working in parallel, each one using its own IPXC_Inst instance. What we observe is that thread A crashes while calling IPXC_Document::New() on a IPXC_Document created by its own (thread A's) IPXC_Inst as soon as thread B calls IPXC_Inst::Finalize() on its own (thread B's) IPXC_Inst. As soon as we remove the call to Finalize, the crash seems to disappear, but we fear that memory will leak or this will have other side effects.

Can you advise us on how to use the SDK in a multi-threaded environment, especially if we're supposed to call Init and Finalize on every thread's instance? Is there anything to pay attention to when working with multiple threads? Again, each thread is using its own instances.

Thanks a lot!

Patrick
Sasha - Tracker Dev Team
User
Posts: 5522
Joined: Fri Nov 21, 2014 8:27 am
Contact:

Re: Multiple threads

Post by Sasha - Tracker Dev Team »

Hello pschlan,

Can you please give us a small sample project that can recreate this behavior?

Cheers,
Alex
Subscribe at:
https://www.youtube.com/channel/UC-TwAMNi1haxJ1FX3LvB4CQ
pschlan
User
Posts: 10
Joined: Thu Nov 15, 2018 10:14 pm

Re: Multiple threads

Post by pschlan »

Hi Alex,

I've put together a minimal example and attached it. I can observe the following issues:

1. With a high probability it crashes in the NewDocument() as described in the initial post (crash01)
crash01.PNG
2. Sometimes it also crashes in PXC_Inst::Init() (crash02)
crash02.PNG
3. Sometimes it hangs in PXC_Inst::Init() (not returning)

I'm not too familiar with COM threading concepts. I can't imagine this to be a problem in the SDK, but I really don't get what I'm doing wrong.

Any help is highly appreciated.

Thanks,

Patrick
Attachments
PDFThreads.zip
(5.85 KiB) Downloaded 138 times
Sasha - Tracker Dev Team
User
Posts: 5522
Joined: Fri Nov 21, 2014 8:27 am
Contact:

Re: Multiple threads

Post by Sasha - Tracker Dev Team »

Hello Patrick,

For correct work, please create the IPXC_Inst once, Initialize it and Shutdown it once. Once initialized, pass it to the needed thread for work.

Cheers,
Alex
Subscribe at:
https://www.youtube.com/channel/UC-TwAMNi1haxJ1FX3LvB4CQ
pschlan
User
Posts: 10
Joined: Thu Nov 15, 2018 10:14 pm

Re: Multiple threads

Post by pschlan »

Hi Alex,

thanks, but could you elaborate a bit more on this, especially since the threading behaviour seems not to be documented at all?

We need to process many PDF files on a multi core system and the goal is to run ~ 4 jobs in parallel, each in its own thread.

As far as I understand the single threaded apartment (STA) model, creating one IPXC_Inst object in the main thread and then using it in the worker threads is not allowed:

"Every object should live on only one thread (within a single-threaded apartment)." / "While multiple objects can live on a single thread, no apartment model object can live on more than one thread.", source https://docs.microsoft.com/en-us/window ... apartments

Hence we would need to switch to a different threading model. Which one is recommended here and what do we need to pay attention to, in order to not run into threading issues? Is a free running / multi threaded appartment fully supported?

Thanks,

Patrick
Sasha - Tracker Dev Team
User
Posts: 5522
Joined: Fri Nov 21, 2014 8:27 am
Contact:

Re: Multiple threads

Post by Sasha - Tracker Dev Team »

Hello Patrick,

From what I've checked with our lead devs, the IPXC_Inst is a singleton and should be created and used in threads afterwards. There are no restrictions for STA.
If you try to create another IPXC_Inst it will have the same pointer as the other ones just +1 ref. This shutdowning it and using other instances will lead to exceptions.

Cheers,
Alex
Subscribe at:
https://www.youtube.com/channel/UC-TwAMNi1haxJ1FX3LvB4CQ
Post Reply