
XOP Multithreading

pfilz0
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
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.
February 9, 2011 at 02:43 am - Permalink
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
February 9, 2011 at 09:13 am - Permalink
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.
February 9, 2011 at 09:55 am - Permalink
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
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:
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.:
February 9, 2011 at 12:19 pm - Permalink
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
February 9, 2011 at 12:38 pm - Permalink
I see your point. I will make it more explicit the next time I work on the XOP Toolkit manual.
February 9, 2011 at 05:11 pm - Permalink