threadprog.c 798 B

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. #if defined _WIN32
  2. #include<windows.h>
  3. #include<stdio.h>
  4. DWORD WINAPI thread_func(LPVOID ignored) {
  5. printf("Printing from a thread.\n");
  6. return 0;
  7. }
  8. int main(int argc, char **argv) {
  9. DWORD id;
  10. HANDLE th;
  11. printf("Starting thread.\n");
  12. th = CreateThread(NULL, 0, thread_func, NULL, 0, &id);
  13. WaitForSingleObject(th, INFINITE);
  14. printf("Stopped thread.\n");
  15. return 0;
  16. }
  17. #else
  18. #include<pthread.h>
  19. #include<stdio.h>
  20. void* main_func(void* ignored) {
  21. printf("Printing from a thread.\n");
  22. return NULL;
  23. }
  24. int main(int argc, char** argv) {
  25. pthread_t thread;
  26. int rc;
  27. printf("Starting thread.\n");
  28. rc = pthread_create(&thread, NULL, main_func, NULL);
  29. rc = pthread_join(thread, NULL);
  30. printf("Stopped thread.\n");
  31. return rc;
  32. }
  33. #endif