LabWindows/CVI

cancel
Showing results for 
Search instead for 
Did you mean: 

Multithreading

Hi !
 
I've got a little problem in an application using multithreading.
 
The question is : How can I assure, that certain function calls run always in the same thread ?
 
The Background is as follows :
 - I use a print engine that can run only single threaded.
 - Before I use the print engine first I have to execute an initialization.
 - at (random) times in my app I call the engine to print reports (using a separate thread than the main program).
 
All access to the print engine has to run in the same thread. I created a thread pool and eyery call to the engine is
wrapped in functions that are called by CmtScheduleFunction (), giving this thread ID.
 
It seemed to work fine, but after my application ran some days without problems it crashed because the printing command
didn't have the corrrect init - what means the printing function ran at last in another thread than the init.
 
Now - the question is : When I define my thread pool the way, that only 1 thread can be executed at a time and I wait previous function executions finished (there's also a Cmt- Function), can I be sure that it is always the same thread or not ?
 
I hope you understand, what my problem is Smiley Happy
  
0 Kudos
Message 1 of 4
(3,442 Views)

Hello Tomsaw,

If you can only run one thread in the thread pool, you can be sure it it always the same thread your function runs in.

You can also make sure that your function is running in this thread by getting the thread's ID (CmtGetThreadID).

0 Kudos
Message 2 of 4
(3,434 Views)

Hello Tomsaw.

The method proposed by Wim S will certainly work, and is likely the most efficient way to solve your problem.

However, if you prefer not to create a new thread pool, and assuming that you have an available thread in the default thread pool, you could try this:

·         Call CmtScheduleThreadPoolFunction(), specifying DEFAULT_THREAD_POOL_HANDLE

·         In the scheduled function, do your initialization, then call ProcessSystemEvents() in a loop

·         Call PostDeferredCallToThread() each time you want to print a report

Of course, this will also work for any thread pool your app creates.

Regards,

Colin.

Message 3 of 4
(3,422 Views)
Thank you for the hints ! This helps me a lot to solve my problem.
0 Kudos
Message 4 of 4
(3,407 Views)