Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <windows.h>
- #include <cstdlib>
- #include <stdio.h>
- #include <tchar.h>
- #define MAX_SIZE = 45;
- DWORD fibArr[MAX_SIZE] = {-1}; /* data is shared by the thread(s) */
- /* the thread runs in this separate function */
- DWORD WINAPI runner(LPVOID Param){
- DWORD Upper = *(DWORD*)Param;
- unsigned long long int num1 = 0, num2 = 1, currNum = 0;
- for(DWORD i = 0; i < Upper; ++i){
- if(i == 0 || i == 1)
- fibArr[i] = i;
- else{
- currNum = num1 + num2;
- num1 = num2;
- num2 = currNum;
- fibArr[i] = currNum;
- }
- }
- }
- int main(int argc, TCHAR* argv[]){
- DWORD ThreadId;
- HANDLE ThreadHandle;
- int Param;
- if (argc != 2) {
- fprintf(stderr,"usage: a.out <integer value between 0 and 45>\n");
- return -1;
- }
- Param = atoi(argv[1]);
- if (Param < 0 || Param > 45) {
- fprintf(stderr,"Argument %d must be non-negative and less than 45\n",atoi(argv[1]));
- return -1;
- }
- ThreadHandle = CreateThread(
- NULL, // default security attributes
- 0, // default stack size
- runner, // thread function
- &Param, // parameter to thread function
- 0, // default creation flags
- &ThreadId); // returns the thread identifier
- if (ThreadHandle != NULL) {
- // now wait for the thread to finish
- WaitForSingleObject(ThreadHandle,INFINITE);
- // close the thread handle
- CloseHandle(ThreadHandle);
- }
- cout << "Displaying " << Param << " terms: \n";
- for(int i = 0; i < Param; ++i)
- cout << fibArr[i] << " ";
- cout << endl;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement