How to draw in Cinder concurrently?

I`m trying to lauch 4 standart C++ threads, each drawing a line, but an abort() gets called in runtime. I try to adhere to this example, pass over the GL drawing context, use a mutex for thread synchronization. What do I do wrong?

void drawTest(gl::ContextRef _context, vec2 p1, vec2 p2, mutex& latch)
{
    ci::ThreadSetup threadSetup;
    latch.lock();
    _context->makeCurrent();
    gl::drawLine(p1, p2);
    latch.unlock();
}

void Func()
{
   vector<thread> threads;
   mutex myMutex;
   gl::ContextRef backgroundCtx = gl::Context::create(gl::context());
   threads.push_back(thread(drawTest, backgroundCtx, vec2(100, 344), vec2(150, 344), std::ref(myMutex)));
   threads.push_back(thread(drawTest, backgroundCtx, vec2(400, 850), vec2(730, 200), std::ref(myMutex)));
   threads.push_back(thread(drawTest, backgroundCtx, vec2(302, 540), vec2(150, 905), std::ref(myMutex)));
   threads.push_back(thread(drawTest, backgroundCtx, vec2(222, 930), vec2(50, 777), std::ref(myMutex)));
}

Hi,

you’re not creating a separate context per thread. Each thread should have its own (shared) context.

There could be more problems with this code, but let’s first see what happens if you fix the first problem.

~Paul

1 Like