Hi there...
I'm new to this forum so please correct me if this is not the right place to post this...
I have created two threads in the following code, and was expecting the threads to behave quite differently than they actually did...
can someone please explain why this happens? I'd be more than grateful.
Why does the program output look like this?Code:DWORD ThreadFunc(LPVOID p) { int n; n = *(int *)p; for (int i=0; i<5; i++) { printf("%d\n", n); sleep(10); } return 0; } int _tmain(int argc, _TCHAR* argv[]) { int n=2; printf("Two Threads\n"); HANDLE hThread = CreateThread(NULL, NULL, (LPTHREAD_START_ROUTINE)ThreadFunc, (LPVOID)&n, NULL, NULL); // CloseHandle(hThread); for (int i = 0; i<5; i++) { printf("----1\n"); sleep(10); } WaitForSingleObject(hThread,INFINITE); char ch = getchar(); return 0; }
Thanks in advance.




Reply With Quote
