开发者

OpenGL with OpenMP always segfault

I have in my program a loop that will fill an 3D cube with pixels (GL_POINTS), so to speed up things a little I thought i could use OpenMP and separate this for loop in my multi-core processor.

The problem is that any time I use OpenMP in the loop the program segfaults, here is the code of the loop:

glBegin(GL_POINTS);
#pragma omp parallel for
for (int a = 0; a < m_width * m_height; a++)
{
    uint8_t r, g, b;
    r = m_data[a * m_channels];
    g = m_data[a * m_channels + 1];
    b = m_data[a * m_channels + 2];

    glColor3ub(r, g, b);
    glVertex3f(r / 255.0 - 0.5, g / 255.0 - 0.5, b / 255.0 - 0.5);
}
glEnd();

As you can see, the code just get some information from m_data array and then call glColor3ub and g开发者_StackOverflowlVertex3f with it, if I run this code without the #pragma the code runs great.

The gdb shows me that the program segfaults when it reach the glColor3ub, making clear that the problem is something with openGL, maybe the function is not thread-safe? Can I make something to correct the code?


Don't mess with a single OpenGL context and multithreading, or guard every use of OpenGL by a critical section (which won't buy you anything performance-wise). What you can probably do is use vertex arrays/buffers (which will be faster anyway) and fill their data using multiple threads before drawing it in a single thread.

What should happen if one thread sets the current color and gets unscheduled before it draws the vertex? But what definitely will happen is that the driver gets interrupted in the middle of some operation and its internal data gets totally messed up.

OpenGL is definitely not thread safe.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜