# Gl Create Vertex Arrays in thread does not generate buffer/array

**URL:** <https://discourse.glfw.org/t/gl-create-vertex-arrays-in-thread-does-not-generate-buffer-array/2758>\
**Category:** support\
**Created:** [October 4, 2024, 7:50pm UTC](https://discourse.glfw.org/t/gl-create-vertex-arrays-in-thread-does-not-generate-buffer-array/2758 "2024-10-04T19:50:28Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![kamenevk08090](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.glfw.org/kamenevk08090/32/974_2.png) [@kamenevk08090](https://discourse.glfw.org/u/kamenevk08090)\
**Post date:** [October 4, 2024, 7:50pm UTC](https://discourse.glfw.org/t/gl-create-vertex-arrays-in-thread-does-not-generate-buffer-array/2758/1 "2024-10-04T19:50:28Z")

</div>

Hi, I am creating my own graphics engine and noticed that vertex/element buffers are not created in a separate thread, example:  
Everything is going well here:

```auto
Personaj* persik = new Personaj(igra.VzyatOsnovShaderProgId(), versh, inds, "biblioteka/ya.png");
obnov += persik;
otrisovat += persik;
etotIgrok.pers = persik;

```

But when I create a character in a separate thread, gl Create Vertex Arrays and glGenBuffers do not create buffers with arrays

```auto
potok = std::thread([&]() {
  Personaj* persik = new Personaj(igra.VzyatOsnovShaderProgId(), versh, inds, "biblioteka/ya.png");
  obnov += persik;
  otrisovat += persik;
  etotIgrok.pers = persik;
});

```

The function of generating buffers and arrays for a new character

```auto
void DDModel::ObnovitVershini(GLuint& proga, Massiv<Vershina>& vershini_per, Massiv<GLuint>& indeksi_per)
{
    vershini = vershini_per;
    indeksi = indeksi_per;
 
    if (VAO)
        glDeleteVertexArrays(1, &VAO);
    if (VBO)
        glDeleteBuffers(1, &VBO);
    if (EBO)
        glDeleteBuffers(1, &EBO);
 
    glCreateVertexArrays(1, &VAO);
    glBindVertexArray(VAO);
 
    glGenBuffers(1, &VBO);
    glBindBuffer(GL_ARRAY_BUFFER, VBO);
    glBufferData(GL_ARRAY_BUFFER, vershini.VzyatRazmer() * sizeof(Vershina), vershini.VzyatMas(), GL_STATIC_DRAW);
    glGenBuffers(1, &EBO);
    glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, EBO);
    glBufferData(GL_ELEMENT_ARRAY_BUFFER, indeksi.VzyatRazmer() * sizeof(GLuint), indeksi.VzyatMas(), GL_STATIC_DRAW);
 
    GLuint attribMest = glGetAttribLocation(proga, "mesto_vershini");
    glVertexAttribPointer(attribMest, 3, GL_FLOAT, GL_FALSE, sizeof(Vershina), (GLvoid*)offsetof(Vershina, mesto));
    glEnableVertexAttribArray(attribMest);
 
    attribMest = glGetAttribLocation(proga, "cvetKoordi_vershini");
    glVertexAttribPointer(attribMest, 2, GL_FLOAT, GL_FALSE, sizeof(Vershina), (GLvoid*)offsetof(Vershina, cvetKoordi));
    glEnableVertexAttribArray(attribMest);
 
    glBindVertexArray(0);
}

```

---

<div class="post-metadata">

**Author:** ![mmozeiko](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.glfw.org/mmozeiko/32/29_2.png) [@mmozeiko](https://discourse.glfw.org/u/mmozeiko)\
**Post date:** [October 4, 2024, 8:06pm UTC](https://discourse.glfw.org/t/gl-create-vertex-arrays-in-thread-does-not-generate-buffer-array/2758/2 "2024-10-04T20:06:49Z")

</div>

OpenGL functions work only when [OpenGL Context](https://www.khronos.org/opengl/wiki/OpenGL_Context) is active in the thread. In glfw opengl context is activated with [glfwMakeContextCurrent](https://www.glfw.org/docs/3.3/group__context.html#ga1c04dc242268f827290fe40aa1c91157) function. Only then you are allowed to call GL functions. See [GLFW: Context guide](https://www.glfw.org/docs/3.3/context_guide.html#context_current).

Ideally you want to keep all your GL calls in single rendering thread. You can do whatever other data loading & processing things on threads. But just always give control to main thread when you actually need to call GL functions.

There are ways to create multiple GL contexts, one for each thread and then share data between them, but that part always was very buggy in OpenGL drivers. I would not recommend going down that path.
