threads.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. //========================================================================
  2. // Multi-threading test
  3. // Copyright (c) Camilla Löwy <elmindreda@glfw.org>
  4. //
  5. // This software is provided 'as-is', without any express or implied
  6. // warranty. In no event will the authors be held liable for any damages
  7. // arising from the use of this software.
  8. //
  9. // Permission is granted to anyone to use this software for any purpose,
  10. // including commercial applications, and to alter it and redistribute it
  11. // freely, subject to the following restrictions:
  12. //
  13. // 1. The origin of this software must not be misrepresented; you must not
  14. // claim that you wrote the original software. If you use this software
  15. // in a product, an acknowledgment in the product documentation would
  16. // be appreciated but is not required.
  17. //
  18. // 2. Altered source versions must be plainly marked as such, and must not
  19. // be misrepresented as being the original software.
  20. //
  21. // 3. This notice may not be removed or altered from any source
  22. // distribution.
  23. //
  24. //========================================================================
  25. //
  26. // This test is intended to verify whether the OpenGL context part of
  27. // the GLFW API is able to be used from multiple threads
  28. //
  29. //========================================================================
  30. #include "tinycthread.h"
  31. #include <glad/gl.h>
  32. #define GLFW_INCLUDE_NONE
  33. #include <GLFW/glfw3.h>
  34. #include <stdio.h>
  35. #include <stdlib.h>
  36. #include <math.h>
  37. typedef struct
  38. {
  39. GLFWwindow* window;
  40. const char* title;
  41. float r, g, b;
  42. thrd_t id;
  43. } Thread;
  44. static volatile int running = GLFW_TRUE;
  45. static void error_callback(int error, const char* description)
  46. {
  47. fprintf(stderr, "Error: %s\n", description);
  48. }
  49. static void key_callback(GLFWwindow* window, int key, int scancode, int action, int mods)
  50. {
  51. if (key == GLFW_KEY_ESCAPE && action == GLFW_PRESS)
  52. glfwSetWindowShouldClose(window, GLFW_TRUE);
  53. }
  54. static int thread_main(void* data)
  55. {
  56. const Thread* thread = data;
  57. glfwMakeContextCurrent(thread->window);
  58. glfwSwapInterval(1);
  59. while (running)
  60. {
  61. const float v = (float) fabs(sin(glfwGetTime() * 2.f));
  62. glClearColor(thread->r * v, thread->g * v, thread->b * v, 0.f);
  63. glClear(GL_COLOR_BUFFER_BIT);
  64. glfwSwapBuffers(thread->window);
  65. }
  66. glfwMakeContextCurrent(NULL);
  67. return 0;
  68. }
  69. int main(void)
  70. {
  71. int i, result;
  72. Thread threads[] =
  73. {
  74. { NULL, "Red", 1.f, 0.f, 0.f, 0 },
  75. { NULL, "Green", 0.f, 1.f, 0.f, 0 },
  76. { NULL, "Blue", 0.f, 0.f, 1.f, 0 }
  77. };
  78. const int count = sizeof(threads) / sizeof(Thread);
  79. glfwSetErrorCallback(error_callback);
  80. if (!glfwInit())
  81. exit(EXIT_FAILURE);
  82. glfwWindowHint(GLFW_VISIBLE, GLFW_FALSE);
  83. for (i = 0; i < count; i++)
  84. {
  85. threads[i].window = glfwCreateWindow(200, 200,
  86. threads[i].title,
  87. NULL, NULL);
  88. if (!threads[i].window)
  89. {
  90. glfwTerminate();
  91. exit(EXIT_FAILURE);
  92. }
  93. glfwSetKeyCallback(threads[i].window, key_callback);
  94. glfwSetWindowPos(threads[i].window, 200 + 250 * i, 200);
  95. glfwShowWindow(threads[i].window);
  96. }
  97. glfwMakeContextCurrent(threads[0].window);
  98. gladLoadGL(glfwGetProcAddress);
  99. glfwMakeContextCurrent(NULL);
  100. for (i = 0; i < count; i++)
  101. {
  102. if (thrd_create(&threads[i].id, thread_main, threads + i) !=
  103. thrd_success)
  104. {
  105. fprintf(stderr, "Failed to create secondary thread\n");
  106. glfwTerminate();
  107. exit(EXIT_FAILURE);
  108. }
  109. }
  110. while (running)
  111. {
  112. glfwWaitEvents();
  113. for (i = 0; i < count; i++)
  114. {
  115. if (glfwWindowShouldClose(threads[i].window))
  116. running = GLFW_FALSE;
  117. }
  118. }
  119. for (i = 0; i < count; i++)
  120. glfwHideWindow(threads[i].window);
  121. for (i = 0; i < count; i++)
  122. thrd_join(threads[i].id, &result);
  123. exit(EXIT_SUCCESS);
  124. }