Advertisement
Ladies_Man

_beginthread multithreading magic

Feb 23rd, 2015
213
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.67 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <windows.h>
  4. #include <process.h>
  5. #include <iostream>
  6.  
  7. void ThreadFunc(void* data) {
  8.   // Do stuff.  This will be the first function called on the new thread.
  9.   // When this function returns, the thread goes away.  See MSDN for more details.
  10.   int j = 0;
  11.   while (j < 1000) {
  12.     printf("(%d) ", j);
  13.     j++;
  14.   }
  15. }
  16.  
  17. int main() {
  18.     int i = 0;
  19.     while (i < 5) {
  20.         printf("C");
  21.         i++;
  22.     }
  23.   HANDLE thread = (HANDLE)_beginthread(&ThreadFunc, 0, 0);
  24.   if (thread) {
  25.     // Optionally do stuff, such as wait on the thread.
  26.   }
  27.   i = 0;
  28.   while (i < 100) {
  29.     printf("%d ", i);
  30.     i++;
  31.   }
  32. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement