testthread.c 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  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 */
  11. #include <stdio.h>
  12. #include <stdlib.h>
  13. #include <signal.h>
  14. #include "SDL.h"
  15. #include "SDL_thread.h"
  16. static SDL_TLSID tls;
  17. static int alive = 0;
  18. /* Call this instead of exit(), so we can clean up SDL: atexit() is evil. */
  19. static void
  20. quit(int rc)
  21. {
  22. SDL_Quit();
  23. exit(rc);
  24. }
  25. int SDLCALL
  26. ThreadFunc(void *data)
  27. {
  28. SDL_TLSSet(tls, "baby thread", NULL);
  29. SDL_Log("Started thread %s: My thread id is %lu, thread data = %s\n",
  30. (char *) data, SDL_ThreadID(), (const char *)SDL_TLSGet(tls));
  31. while (alive) {
  32. SDL_Log("Thread '%s' is alive!\n", (char *) data);
  33. SDL_Delay(1 * 1000);
  34. }
  35. SDL_Log("Thread '%s' exiting!\n", (char *) data);
  36. return (0);
  37. }
  38. static void
  39. killed(int sig)
  40. {
  41. SDL_Log("Killed with SIGTERM, waiting 5 seconds to exit\n");
  42. SDL_Delay(5 * 1000);
  43. alive = 0;
  44. quit(0);
  45. }
  46. int
  47. main(int argc, char *argv[])
  48. {
  49. SDL_Thread *thread;
  50. /* Enable standard application logging */
  51. SDL_LogSetPriority(SDL_LOG_CATEGORY_APPLICATION, SDL_LOG_PRIORITY_INFO);
  52. /* Load the SDL library */
  53. if (SDL_Init(0) < 0) {
  54. SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't initialize SDL: %s\n", SDL_GetError());
  55. return (1);
  56. }
  57. tls = SDL_TLSCreate();
  58. SDL_assert(tls);
  59. SDL_TLSSet(tls, "main thread", NULL);
  60. SDL_Log("Main thread data initially: %s\n", (const char *)SDL_TLSGet(tls));
  61. alive = 1;
  62. thread = SDL_CreateThread(ThreadFunc, "One", "#1");
  63. if (thread == NULL) {
  64. SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't create thread: %s\n", SDL_GetError());
  65. quit(1);
  66. }
  67. SDL_Delay(5 * 1000);
  68. SDL_Log("Waiting for thread #1\n");
  69. alive = 0;
  70. SDL_WaitThread(thread, NULL);
  71. SDL_Log("Main thread data finally: %s\n", (const char *)SDL_TLSGet(tls));
  72. alive = 1;
  73. signal(SIGTERM, killed);
  74. thread = SDL_CreateThread(ThreadFunc, "Two", "#2");
  75. if (thread == NULL) {
  76. SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't create thread: %s\n", SDL_GetError());
  77. quit(1);
  78. }
  79. raise(SIGTERM);
  80. SDL_Quit(); /* Never reached */
  81. return (0); /* Never reached */
  82. }