threads.c
Go to the documentation of this file.
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 
31 #include "tinycthread.h"
32 
33 #include <glad/glad.h>
34 #include <GLFW/glfw3.h>
35 
36 #include <stdio.h>
37 #include <stdlib.h>
38 #include <math.h>
39 
40 typedef struct
41 {
43  const char* title;
44  float r, g, b;
46 } Thread;
47 
48 static volatile int running = GLFW_TRUE;
49 
50 static void error_callback(int error, const char* description)
51 {
52  fprintf(stderr, "Error: %s\n", description);
53 }
54 
55 static void key_callback(GLFWwindow* window, int key, int scancode, int action, int mods)
56 {
57  if (key == GLFW_KEY_ESCAPE && action == GLFW_PRESS)
59 }
60 
61 static int thread_main(void* data)
62 {
63  const Thread* thread = data;
64 
67 
68  while (running)
69  {
70  const float v = (float) fabs(sin(glfwGetTime() * 2.f));
71  glClearColor(thread->r * v, thread->g * v, thread->b * v, 0.f);
72 
74  glfwSwapBuffers(thread->window);
75  }
76 
78  return 0;
79 }
80 
81 int main(void)
82 {
83  int i, result;
84  Thread threads[] =
85  {
86  { NULL, "Red", 1.f, 0.f, 0.f, 0 },
87  { NULL, "Green", 0.f, 1.f, 0.f, 0 },
88  { NULL, "Blue", 0.f, 0.f, 1.f, 0 }
89  };
90  const int count = sizeof(threads) / sizeof(Thread);
91 
93 
94  if (!glfwInit())
95  exit(EXIT_FAILURE);
96 
98 
99  for (i = 0; i < count; i++)
100  {
101  threads[i].window = glfwCreateWindow(200, 200,
102  threads[i].title,
103  NULL, NULL);
104  if (!threads[i].window)
105  {
106  glfwTerminate();
107  exit(EXIT_FAILURE);
108  }
109 
110  glfwSetKeyCallback(threads[i].window, key_callback);
111 
112  glfwSetWindowPos(threads[i].window, 200 + 250 * i, 200);
113  glfwShowWindow(threads[i].window);
114  }
115 
116  glfwMakeContextCurrent(threads[0].window);
119 
120  for (i = 0; i < count; i++)
121  {
122  if (thrd_create(&threads[i].id, thread_main, threads + i) !=
123  thrd_success)
124  {
125  fprintf(stderr, "Failed to create secondary thread\n");
126 
127  glfwTerminate();
128  exit(EXIT_FAILURE);
129  }
130  }
131 
132  while (running)
133  {
134  glfwWaitEvents();
135 
136  for (i = 0; i < count; i++)
137  {
138  if (glfwWindowShouldClose(threads[i].window))
140  }
141  }
142 
143  for (i = 0; i < count; i++)
144  glfwHideWindow(threads[i].window);
145 
146  for (i = 0; i < count; i++)
147  thrd_join(threads[i].id, &result);
148 
149  exit(EXIT_SUCCESS);
150 }
151 
GLboolean GLboolean GLboolean b
GLboolean GLboolean g
The header of the GLFW 3 API.
GLFWAPI GLFWglproc glfwGetProcAddress(const char *procname)
Returns the address of the specified function for the current context.
Definition: context.c:741
static int thread_main(void *data)
Definition: threads.c:61
void *(* GLADloadproc)(const char *name)
GLFWAPI void glfwHideWindow(GLFWwindow *window)
Hides the specified window.
Definition: window.c:781
int thrd_create(thrd_t *thr, thrd_start_t func, void *arg)
Definition: tinycthread.c:361
GLFWAPI GLFWerrorfun glfwSetErrorCallback(GLFWerrorfun cbfun)
Sets the error callback.
Definition: init.c:309
#define GLFW_FALSE
Zero.
Definition: glfw3.h:287
static volatile int running
Definition: threads.c:48
GLuint64 key
Definition: glext.h:8966
GLFWAPI int glfwInit(void)
Initializes the GLFW library.
Definition: init.c:198
GLdouble f
#define GL_COLOR_BUFFER_BIT
thrd_t id
Definition: threads.c:45
static void error_callback(int error, const char *description)
Definition: threads.c:50
GLFWAPI void glfwSwapInterval(int interval)
Sets the swap interval for the current context.
Definition: context.c:658
#define GLFW_KEY_ESCAPE
Definition: glfw3.h:412
#define glClear
GLFWAPI void glfwSwapBuffers(GLFWwindow *window)
Swaps the front and back buffers of the specified window.
Definition: context.c:641
GLFWAPI void glfwMakeContextCurrent(GLFWwindow *window)
Makes the context of the specified window current for the calling thread.
Definition: context.c:611
int thrd_join(thrd_t thr, int *res)
Definition: tinycthread.c:432
GLFWwindow * window
Definition: threads.c:42
action
Definition: enums.py:62
GLAPI int gladLoadGLLoader(GLADloadproc)
Definition: glad/glad.c:1697
static const textual_icon exit
Definition: model-views.h:254
GLFWAPI void glfwSetWindowPos(GLFWwindow *window, int xpos, int ypos)
Sets the position of the client area of the specified window.
Definition: window.c:531
GLFWAPI void glfwSetWindowShouldClose(GLFWwindow *window, int value)
Sets the close flag of the specified window.
Definition: window.c:486
GLFWAPI void glfwWaitEvents(void)
Waits until events are queued and processes them.
Definition: window.c:1078
int main(void)
Definition: threads.c:81
float g
Definition: threads.c:44
GLFWAPI GLFWwindow * glfwCreateWindow(int width, int height, const char *title, GLFWmonitor *monitor, GLFWwindow *share)
Creates a window and its associated context.
Definition: window.c:151
#define GLFW_TRUE
One.
Definition: glfw3.h:279
GLFWAPI double glfwGetTime(void)
Returns the value of the GLFW timer.
Definition: input.c:1275
GLFWAPI void glfwShowWindow(GLFWwindow *window)
Makes the specified window visible.
Definition: window.c:755
GLint GLsizei count
GLFWAPI void glfwTerminate(void)
Terminates the GLFW library.
Definition: init.c:243
static void key_callback(GLFWwindow *window, int key, int scancode, int action, int mods)
Definition: threads.c:55
#define GLFW_VISIBLE
Window visibility window hint and attribute.
Definition: glfw3.h:780
#define glClearColor
GLFWAPI GLFWkeyfun glfwSetKeyCallback(GLFWwindow *window, GLFWkeyfun cbfun)
Sets the key callback.
Definition: input.c:791
float r
Definition: threads.c:44
pthread_t thrd_t
Definition: tinycthread.h:317
float b
Definition: threads.c:44
#define NULL
Definition: tinycthread.c:47
int i
const char * title
Definition: threads.c:43
GLboolean * data
#define GLFW_PRESS
The key or mouse button was pressed.
Definition: glfw3.h:304
GLuint64EXT * result
Definition: glext.h:10921
GLdouble v
#define thrd_success
Definition: tinycthread.h:172
struct GLFWwindow GLFWwindow
Definition: threads.c:40
GLFWAPI void glfwWindowHint(int hint, int value)
Sets the specified window hint to the desired value.
Definition: window.c:291
Definition: parser.hpp:150
GLFWAPI int glfwWindowShouldClose(GLFWwindow *window)
Checks the close flag of the specified window.
Definition: window.c:477


librealsense2
Author(s): Sergey Dorodnicov , Doron Hirshberg , Mark Horn , Reagan Lopez , Itay Carpis
autogenerated on Mon May 3 2021 02:50:11