Laurent Crivello
Laurent Crivello

Reputation: 3931

Execute code on Main Maya Thread from Parallel Thread

Working on a Maya API plugin on MacOS, I am trying to do some heavy calculations in a parallel thread and then to store the result in OpenGL VBO.

Creation and execution of thread works fine, until I need to do GL operations, when Maya crashes.

I enabled multithreading on OpenGL using CGLEnable( CGLGetCurrentContext(), kCGLCEMPEngine); but this didn't help. My idea was therefore to do the calculations on the parallel thread and when finished to do the GL stuff in the main thread.

How can I execute some function on the main thread from the parallel thread ?

{ // Main thread
    MStatus stat = MThreadAsync::init();
    if( MStatus::kSuccess != stat ) {
        MString str = MString("Error creating MThreadAsync");
        MGlobal::displayError(str);
    }
    else
    {
        MThreadAsync::createTask(createOpenGLVBOs, NULL ,NULL);
    }
}

void createOpenGLVBOs(void *data) // PARALLEL THREAD
{
...heavy calculations...
    GLuint nb;
    glGenBuffers(1, &nb); --> CRASH
}

Upvotes: 0

Views: 312

Answers (1)

bensch128
bensch128

Reputation: 63

I am pretty sure that calling any OpenGL function in a background will crash the application.

OpenGL calls should only be invoked in the main thread.

You could populate a shared data structure with your data, execute the parallel computations, and then do all of your opengl calls afterwards on the main thread.

Alternatively, you can use std::condition_variable to signal from the background thread to the main thread and have the main thread invoke the opengl calls while the background thread does the work. You should use a spinlock or mutex to protect the shared data.

Upvotes: 1

Related Questions