Question
#include #include DWORD Sum; /*data is shared by the thread(s)*/ /* the thread runs in this separeate funtion */ DWORD WINAPI Summation(LPVOID Param) { DWORD
#include
DWORD WINAPI Summation(LPVOID Param) { DWORD Upper = *(DWORD*)Param; for (DWORD i = 0; i <= Upper; i++) Sum += i; return 0; } int main(int argc, char *argv[] ) { DWORD ThreadId; HANDLE ThreadHandle; int Param; /*perform some basic error checking*/ if (argc != 2){ fprintf(stderr, "An integer parmeter is required "); return -1; } Param = atoi(argv[1]); if (Param < 0){ fprintf(stderr, "An integer >= 0 is required "); return -1; }
//create the thread ThreadHandle = CreateThread( NULL, //Defualt security attributes 0, //default stack size Summation, //thread function &Param,//parameter to thread function 0, //default creation flags &ThreadId) ; //returns the thread identifier
if(ThreadHandle != NULL){ //now wait for thread to finish WaitForSingleObject(ThreadHandle, INFINITE);
//Close Thread handle CloseHandle(ThreadHandle); printf("sum= %d ", Sum); } }
I don't know why my program just jump right to the first error messaage, it's not displaying the summation
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started