XOP Multithreading

Hi,

I want to use multiple threads in my XOP and each should be able to send messages to the Igor history e. g.

....

string msg;
parallel_invoke(
[&]{
msg="Thread1" + CR_STR;
XOPNotice3(msg.c_str(),"", 0);
//do something

},
[&]{
msg="Thread2" + CR_STR;
XOPNotice3(msg.c_str(),"", 0);
//do something
}
);

.....

The messages are printed to the History area, but Igor also prints " BUG: RunXOP called from a thread" there.

Who knows what to do about that?

BR
Patrick
What version of Igor/XOP Toolkit are you using?
If you want to use multithreading properly it is best to use Igor 6.20 and XOP Toolkit 6. In the XOP Toolkit Manual v6 there is also a whole chapter about multithreading and Igor.

What library is parallel_invoke(..) from? The manual states that you are only allowed to do callbacks to Igor either from its mainthread or from Igor preemptive threads but not from threads you create on your own.
Hi Thomas,

I'm using the XOP 6 Toolkit with Igor 6.2.1, sorry about not mentioning that in my previous post. You can find the parallel_invoke function in the Parallel Patterns Library (ppl.h) which is part of (Microsoft's?) Concurrency Runtime.

I've read that part in the manual but I guess I have to admit that I have no clue what is meant by Igor preemptive threads. To check I've created a new thread with __beginthreadex(...) and let it print something to the history. It works but I get the same message "BUG:...." as before. How do I get a thread out of Igors pool?

Gruess,
Patrick
The XOP Toolkit 6 manual says (page 164):
Quote:
Igor preemptive threads are created by multithreaded Igor operations like FuncFit, when the MultiThread keyword is used to introduce a waveform assignment statement, and when an Igor user-defined function calls the ThreadStart operation.


You can not do a callback to Igor from a thread created by your XOP. With Igor Pro 6.2x and XOP Toolkit 6, you can do a callback from a thread created by Igor - either the main thread or an Igor preemptive thread that you create in a user-defined function by calling ThreadCreateGroup.

If you are trying to call XOPNotice or any other callback from a thread created by your XOP, you can't do that. You will need to have your thread store messages in a buffer in memory and then have your main thread call XOPNotice when it receives the IDLE message from Igor or on some other event in the main thread, such as the user calling a function or operation that you create for the purpose of getting any messages from the thread.

The code that deals with your shared buffer needs to be written in a thread-safe way to prevent your threads from accessing it simultaneously.


hrodstein wrote:
You can not do a callback to Igor from a thread created by your XOP. With Igor Pro 6.2x and XOP Toolkit 6, you can do a callback from a thread created by Igor - either the main thread or an Igor preemptive thread that you create in a user-defined function by calling ThreadCreateGroup.


I think this point could actually be emphasized a bit stronger in the XOP manual. For instance, on XOPNotice the reference section of the manual says
Quote:
XOPNotice is thread-safe with XOP Toolkit 6 and Igor Pro 6.20 or later.

even though, according to your explanation, this function only is threadsafe within the context of Igor threads, but not within the context of XOP-created threads.

Now, about WaveType, which is threadsafe within any kind of thread, the manual says the following:
Quote:
WaveType is thread-safe with XOP Toolkit 6 and Igor Pro 6.20 or later with certain exceptions explained under Waves Passed to Threads as Parameters on page 165.


From this hint one would certainly expect that XOPNotice is "more" threadsafe than WaveType. But it isn't! The manual seems to mix up two different meanings of thread-safety, and I think it would be good to make this more clear.

e.g.:
Quote:
XOPNotice is threadsafe when called from different Igor preemptive threads with XOP Toolkit 6 and Igor Pro 6.20 or later. It is not threadsafe when called from XOP-created threads.
Hi,

Thanks for the answer. Ok, so I guess there won't be any nifty workaround. I've now settled for letting Igor freeze while running the XOP

Cheers,
Patrick
Quote:
I think this point could actually be emphasized a bit stronger in the XOP manual.


I see your point. I will make it more explicit the next time I work on the XOP Toolkit manual.