Advertisement
Ladies_Man

_beginthread multithreading magic with args

Feb 23rd, 2015
211
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.90 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. typedef struct {
  8.     int data1;
  9.     int data2;
  10. } t;
  11.  
  12. void ThreadFunc(void* data) {
  13.   // Do stuff.  This will be the first function called on the new thread.
  14.   // When this function returns, the thread goes away.  See MSDN for more details.
  15.   int j = 0;
  16.   t *args = (t*) data;
  17.   j = args->data2;
  18.   int k = args->data1;
  19.   if (k == -1) printf("+");
  20.   while (j < 1000) {
  21.     printf("(%d) ", j);
  22.     j++;
  23.   }
  24. }
  25.  
  26. int main() {
  27.     int i = 0;
  28.     while (i < 5) {
  29.         printf("C");
  30.         i++;
  31.     }
  32.     int amount = 2;
  33.     t arg;
  34.     arg.data1 = -1;
  35.     arg.data2 = 7;
  36.   HANDLE thread = (HANDLE)_beginthread(&ThreadFunc, 0, (void*) &arg);
  37.   if (thread) {
  38.     // Optionally do stuff, such as wait on the thread.
  39.   }
  40.   i = 0;
  41.   while (i < 100) {
  42.     printf("%d ", i);
  43.     i++;
  44.   }
  45. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement