Advertisement
Guest User

Untitled

a guest
Mar 20th, 2018
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.51 KB | None | 0 0
  1. #include <iostream>
  2. #include <windows.h>
  3. #include <cstdlib>
  4. #include <stdio.h>
  5. #include <tchar.h>
  6.  
  7. #define MAX_SIZE = 45;
  8.  
  9. DWORD fibArr[MAX_SIZE] = {-1}; /* data is shared by the thread(s) */
  10. /* the thread runs in this separate function */
  11.  
  12. DWORD WINAPI runner(LPVOID Param){
  13.     DWORD Upper = *(DWORD*)Param;
  14.     unsigned long long int num1 = 0, num2 = 1, currNum = 0;
  15.    
  16.  
  17.     for(DWORD i = 0; i < Upper; ++i){
  18.         if(i == 0 || i == 1)
  19.             fibArr[i] = i;
  20.         else{  
  21.             currNum = num1 + num2;
  22.             num1 = num2;
  23.             num2 = currNum;
  24.             fibArr[i] = currNum;
  25.          }
  26.     }
  27.  
  28. }
  29.  
  30. int main(int argc, TCHAR* argv[]){
  31.  
  32.     DWORD ThreadId;
  33.     HANDLE ThreadHandle;
  34.     int Param;
  35.  
  36.     if (argc != 2) {
  37.         fprintf(stderr,"usage: a.out <integer value between 0 and 45>\n");
  38.         return -1;
  39.     }
  40.  
  41.     Param = atoi(argv[1]);
  42.    
  43.     if (Param < 0 || Param > 45) {
  44.         fprintf(stderr,"Argument %d must be non-negative and less than 45\n",atoi(argv[1]));
  45.         return -1;
  46.     }
  47.  
  48.     ThreadHandle = CreateThread(
  49.         NULL, // default security attributes
  50.         0, // default stack size
  51.         runner, // thread function
  52.         &Param, // parameter to thread function
  53.         0, // default creation flags
  54.         &ThreadId); // returns the thread identifier
  55.  
  56.     if (ThreadHandle != NULL) {
  57.         // now wait for the thread to finish
  58.         WaitForSingleObject(ThreadHandle,INFINITE);
  59.         // close the thread handle
  60.         CloseHandle(ThreadHandle);
  61.     }
  62.     cout << "Displaying " << Param << " terms: \n";
  63.  
  64.     for(int i = 0; i < Param; ++i)
  65.         cout << fibArr[i] << " ";
  66.  
  67.  
  68.     cout << endl;
  69.     return 0;
  70.  
  71. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement