threadprog.cpp 890 B

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. /* On Windows not all versions of VS support C++11 and
  2. * some (most?) versions of mingw don't support std::thread,
  3. * even though they do support c++11. Since we only care about
  4. * threads working, do the test with raw win threads.
  5. */
  6. #if defined _WIN32
  7. #include<windows.h>
  8. #include<stdio.h>
  9. DWORD WINAPI thread_func(LPVOID) {
  10. printf("Printing from a thread.\n");
  11. return 0;
  12. }
  13. int main(int, char**) {
  14. printf("Starting thread.\n");
  15. HANDLE th;
  16. DWORD id;
  17. th = CreateThread(NULL, 0, thread_func, NULL, 0, &id);
  18. WaitForSingleObject(th, INFINITE);
  19. printf("Stopped thread.\n");
  20. return 0;
  21. }
  22. #else
  23. #include<thread>
  24. #include<cstdio>
  25. void main_func() {
  26. printf("Printing from a thread.\n");
  27. }
  28. int main(int, char**) {
  29. printf("Starting thread.\n");
  30. std::thread th(main_func);
  31. th.join();
  32. printf("Stopped thread.\n");
  33. return 0;
  34. }
  35. #endif