In my plugin each Iop launches a listener thread to communicate with a
central data server, similar to the example that Jonathan posted a few
months back. In an infinite loop, it does the communication, possibly
updates the Iop, then sleeps for a second:
static void server_callback(unsigned index, unsigned threads, void* p) {
static_cast<MyIop*>(p)->check_server();
}
void MyIop::check_server() {
while( m_keep_running )
{
m_lock.lock();
// do cool stuff
if ( coolStuffHappened ) update();
m_lock.unlock();
sleepFor(1.0);
}
}
void MyIop::_validate(for_real) {
...
...
if (m_interactive && DD::Image::Application::gui && m_server_status
< SERVER_LISTENING) {
m_lock.lock();
if (m_server_status < SERVER_LISTENING) {
Thread::spawn(server_callback, 1, this);
m_thread = Thread::thisThread();
}
m_lock.unlock();
}
}
Now since I have one thread for each instance of MyIop (there could be
many), I want to terminate the thread when the node is deleted, but I'm not
sure how to do that safely. Instinct says I want to set m_keep_running in
~MyIop() which would cause check_server() to fall out of the while loop, but
then I also need to have the destructor wait until check_server() has
exited, and I don't know how to do that part.
Apologies if this is really basic multi-threading stuff, but it's making my
head hurt :)
Anders
_______________________________________________
Nuke-dev mailing list
[email protected], http://forums.thefoundry.co.uk/
http://support.thefoundry.co.uk/cgi-bin/mailman/listinfo/nuke-dev