🛠️🐜 Antkeeper superbuild with dependencies included https://antkeeper.com
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

76 lines
1.9 KiB

  1. /*
  2. Copyright (C) 1997-2020 Sam Lantinga <slouken@libsdl.org>
  3. This software is provided 'as-is', without any express or implied
  4. warranty. In no event will the authors be held liable for any damages
  5. arising from the use of this software.
  6. Permission is granted to anyone to use this software for any purpose,
  7. including commercial applications, and to alter it and redistribute it
  8. freely.
  9. */
  10. /* Simple test of the SDL threading code and error handling */
  11. #include <stdio.h>
  12. #include <stdlib.h>
  13. #include "SDL.h"
  14. static int alive = 0;
  15. /* Call this instead of exit(), so we can clean up SDL: atexit() is evil. */
  16. static void
  17. quit(int rc)
  18. {
  19. SDL_Quit();
  20. exit(rc);
  21. }
  22. int SDLCALL
  23. ThreadFunc(void *data)
  24. {
  25. /* Set the child thread error string */
  26. SDL_SetError("Thread %s (%lu) had a problem: %s",
  27. (char *) data, SDL_ThreadID(), "nevermind");
  28. while (alive) {
  29. SDL_Log("Thread '%s' is alive!\n", (char *) data);
  30. SDL_Delay(1 * 1000);
  31. }
  32. SDL_Log("Child thread error string: %s\n", SDL_GetError());
  33. return (0);
  34. }
  35. int
  36. main(int argc, char *argv[])
  37. {
  38. SDL_Thread *thread;
  39. /* Enable standard application logging */
  40. SDL_LogSetPriority(SDL_LOG_CATEGORY_APPLICATION, SDL_LOG_PRIORITY_INFO);
  41. /* Load the SDL library */
  42. if (SDL_Init(0) < 0) {
  43. SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't initialize SDL: %s\n", SDL_GetError());
  44. return (1);
  45. }
  46. /* Set the error value for the main thread */
  47. SDL_SetError("No worries");
  48. alive = 1;
  49. thread = SDL_CreateThread(ThreadFunc, NULL, "#1");
  50. if (thread == NULL) {
  51. SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't create thread: %s\n", SDL_GetError());
  52. quit(1);
  53. }
  54. SDL_Delay(5 * 1000);
  55. SDL_Log("Waiting for thread #1\n");
  56. alive = 0;
  57. SDL_WaitThread(thread, NULL);
  58. SDL_Log("Main thread error string: %s\n", SDL_GetError());
  59. SDL_Quit();
  60. return (0);
  61. }