Advertisement
Ladies_Man

CreateThread multithreading magic

Feb 23rd, 2015
203
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 <iostream>
  5.  
  6. DWORD WINAPI ThreadFunc(void* data) {
  7.   // Do stuff.  This will be the first function called on the new thread.
  8.   // When this function returns, the thread goes away.  See MSDN for more details.
  9.   int j = 0;
  10.   while (j < 1000) {
  11.     printf("(%d) ", j);
  12.     j++;
  13.   }
  14.   return 0;
  15. }
  16.  
  17. int main() {
  18.     int i = 0;
  19.     while (i < 5) {
  20.         printf("C");
  21.         i++;
  22.     }
  23.   HANDLE thread = CreateThread(NULL, 0, ThreadFunc, NULL, 0, NULL);
  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