Build shader outside the render thread cause slowed drawing by chance

I’m using glfw to draw some geometric figures (such as a lot of bars in different orientations) for researching vision system. My program is written in VC++ MFC. This program opens a dialog window on the primary monitor, and GLFW window was opened on a second monitor. I organized the code for drawing different geometric patterns into a class. The functions of the class were called in a thread also built in the dialog program. An instance of the pattern class was created in the program as a static variable to be used in the thread. All the code about building VAO VBO and shaders were packed in the class.

It generally worked properly. But in about 1/10 chance when I ran the program, the glDrawArrays ran obervious slower, and could not finished in one frame. The really weird thing is, if this bug showed, the slow running kept until the program was closed. If the bug did not show, it would not shown at least until the program was closed in this running.

This bug perplexed me about one week. I tried different ways, and finally I found the shaders should not be built in the class. If the shader was built in the thread (i.e. a variable in the thread, not a static variable outside the thread), the bug is gone. (I used the shader class in the tutorial on

But how can this happened? Is there big difference between a variable in a thread and a static variable?

I find I was wrong about the situation. It is not about whether the shader was defined inside or outside the thread, it is actually because the first run of the shader. When I tested the rendering speed, I always deleted the shader and rebuild it, so that the time was always the time of the first frame. So if I just draw something with all the shaders in the initialize stage, the bug is fixed.