0

I am currently testing to open a SDL window in a thread but every time, it crashes. Here is the code :

#include <stdlib.h>
#include <pthread.h>
#include <unistd.h>
#include <stdint.h>
#include <sys/types.h>
#include <SDL2/SDL.h>
#include <sys/mman.h>
#include <sys/shm.h>
#include <fcntl.h>

void *test(void *arg) {
    SDL_Window *screen = NULL;
    SDL_Init(SDL_INIT_EVERYTHING);
    screen = SDL_CreateWindow("Test", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, 800, 600, 0);
    if (screen == NULL)
        printf("Error\n");
    SDL_Quit();
    return NULL;
}

int main() {
    pthread_t thread;
    pthread_create(&thread, NULL, test, NULL);
    pthread_join(thread, NULL);
}

Here is what I get when trying to run it :

Error
Assertion failed: (NSViewIsCurrentlyBuildingLayerTreeForDisplay() != currentlyBuildingLayerTree), function NSViewSetCurrentlyBuildingLayerTreeForDisplay, file NSView.m, line 13477.
make: *** [run] Trace/BPT trap: 5

I am working on MacOs. I downloaded the SDL library, the program is working whenever I dont use thread. Any ideas to why it happens ?

Makik
  • 9
  • 3
  • Check the return value of `SDL_Init` and call `SDL_GetError`. I'm pretty sure this is because the rendering API must be used on the main thread. – Nelfeal Dec 04 '22 at 15:25

0 Answers0