testerror.c 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. /*
  2. Copyright (C) 1997-2014 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 <signal.h>
  14. #include "SDL.h"
  15. #include "SDL_thread.h"
  16. static int alive = 0;
  17. /* Call this instead of exit(), so we can clean up SDL: atexit() is evil. */
  18. static void
  19. quit(int rc)
  20. {
  21. SDL_Quit();
  22. exit(rc);
  23. }
  24. int SDLCALL
  25. ThreadFunc(void *data)
  26. {
  27. /* Set the child thread error string */
  28. SDL_SetError("Thread %s (%lu) had a problem: %s",
  29. (char *) data, SDL_ThreadID(), "nevermind");
  30. while (alive) {
  31. SDL_Log("Thread '%s' is alive!\n", (char *) data);
  32. SDL_Delay(1 * 1000);
  33. }
  34. SDL_Log("Child thread error string: %s\n", SDL_GetError());
  35. return (0);
  36. }
  37. int
  38. main(int argc, char *argv[])
  39. {
  40. SDL_Thread *thread;
  41. /* Enable standard application logging */
  42. SDL_LogSetPriority(SDL_LOG_CATEGORY_APPLICATION, SDL_LOG_PRIORITY_INFO);
  43. /* Load the SDL library */
  44. if (SDL_Init(0) < 0) {
  45. SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't initialize SDL: %s\n", SDL_GetError());
  46. return (1);
  47. }
  48. /* Set the error value for the main thread */
  49. SDL_SetError("No worries");
  50. alive = 1;
  51. thread = SDL_CreateThread(ThreadFunc, NULL, "#1");
  52. if (thread == NULL) {
  53. SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't create thread: %s\n", SDL_GetError());
  54. quit(1);
  55. }
  56. SDL_Delay(5 * 1000);
  57. SDL_Log("Waiting for thread #1\n");
  58. alive = 0;
  59. SDL_WaitThread(thread, NULL);
  60. SDL_Log("Main thread error string: %s\n", SDL_GetError());
  61. SDL_Quit();
  62. return (0);
  63. }