Multiple Thread programming with Win API
-
I am a beginner of Win API programming,and there is a strange problem that the thread1 is blocked after excuting SuspendThread() function ,but it isn't blocked every time that i run this program. anybody can help me? i'm a chinese,this is my first time here,i hope that i express my problem clearly.
#include
#include
#include
DWORD WINAPI Thread1(void *arg);
DWORD WINAPI Thread2(void *arg);int main(void)
{
DWORD threadid1,threadid2;
HANDLE handle1,handle2;
handle2 = CreateThread(NULL, 0, Thread2, (void *)handle2, 0, &threadid1);
handle1 = CreateThread(NULL, 0, Thread1, (void *)handle2, 0, &threadid2);
HANDLE hEvent;
hEvent = CreateEvent(NULL, FALSE, FALSE, "MainEvent");if(handle1 == NULL) { int err; err = GetLastError(); printf("CreateThread1 error %d\\n", err); exit(1); } else { printf("CreateThread1 success\\n"); } if(handle2 == NULL) { int err; err = GetLastError(); printf("CreateThread2 error %d\\n", err); exit(1); } else { printf("CreateThread2 success\\n"); } int priority1; int priority2; priority1 = GetThreadPriority(handle1); printf("thread1's threadid is=%d,priority is %d\\n", (int)threadid1, priority1); priority2 = GetThreadPriority(handle2); printf("thread2's threadid is=%d,priority is %d\\n\\n", (int)threadid2, priority2); /\* printf("suspending for 3s\\n"); SuspendThread(handle); Sleep(3000); printf("resume\\n"); ResumeThread(handle); \*/ WaitForSingleObject(handle1, INFINITE); DWORD exitCode; BOOL ret; ret = GetExitCodeThread(handle1, &exitCode); if(!ret) { printf("GetThread1 ExitCode,The last error is %d", (int)GetLastError()); } else printf("thread1's exit code is %d\\n", (int)exitCode); ret = GetExitCodeThread(handle2, &exitCode); if(!ret) { printf("GetThread2 ExitCode,The last error is %d", (int)GetLastError()); } else printf("thread2's exit code is %d\\n", (int)exitCode); return 0;
}
int cnt = 0;
DWORD WINAPI Thread1(void *arg)
{
HANDLE hEvent;
hEvent = OpenEvent(EVENT_ALL_ACCESS, TRUE, "MainEvent");
if(hEvent == NULL)
{
printf("in thread1, OpenEvent Error, Error No. is %d!\n", (int)GetLastError());
return 1;
}
else
{
printf("in thread1, OpenEvent success\n");
}
printf("3s later, the thread2 will be suspended!\n");
for(int i = 0; i < 3; i++)
{
Sleep(1000);
SetEvent(hEvent);
}printf("...suspending.....\\n"); int ret; **ret = SuspendThread((HA**
-
I am a beginner of Win API programming,and there is a strange problem that the thread1 is blocked after excuting SuspendThread() function ,but it isn't blocked every time that i run this program. anybody can help me? i'm a chinese,this is my first time here,i hope that i express my problem clearly.
#include
#include
#include
DWORD WINAPI Thread1(void *arg);
DWORD WINAPI Thread2(void *arg);int main(void)
{
DWORD threadid1,threadid2;
HANDLE handle1,handle2;
handle2 = CreateThread(NULL, 0, Thread2, (void *)handle2, 0, &threadid1);
handle1 = CreateThread(NULL, 0, Thread1, (void *)handle2, 0, &threadid2);
HANDLE hEvent;
hEvent = CreateEvent(NULL, FALSE, FALSE, "MainEvent");if(handle1 == NULL) { int err; err = GetLastError(); printf("CreateThread1 error %d\\n", err); exit(1); } else { printf("CreateThread1 success\\n"); } if(handle2 == NULL) { int err; err = GetLastError(); printf("CreateThread2 error %d\\n", err); exit(1); } else { printf("CreateThread2 success\\n"); } int priority1; int priority2; priority1 = GetThreadPriority(handle1); printf("thread1's threadid is=%d,priority is %d\\n", (int)threadid1, priority1); priority2 = GetThreadPriority(handle2); printf("thread2's threadid is=%d,priority is %d\\n\\n", (int)threadid2, priority2); /\* printf("suspending for 3s\\n"); SuspendThread(handle); Sleep(3000); printf("resume\\n"); ResumeThread(handle); \*/ WaitForSingleObject(handle1, INFINITE); DWORD exitCode; BOOL ret; ret = GetExitCodeThread(handle1, &exitCode); if(!ret) { printf("GetThread1 ExitCode,The last error is %d", (int)GetLastError()); } else printf("thread1's exit code is %d\\n", (int)exitCode); ret = GetExitCodeThread(handle2, &exitCode); if(!ret) { printf("GetThread2 ExitCode,The last error is %d", (int)GetLastError()); } else printf("thread2's exit code is %d\\n", (int)exitCode); return 0;
}
int cnt = 0;
DWORD WINAPI Thread1(void *arg)
{
HANDLE hEvent;
hEvent = OpenEvent(EVENT_ALL_ACCESS, TRUE, "MainEvent");
if(hEvent == NULL)
{
printf("in thread1, OpenEvent Error, Error No. is %d!\n", (int)GetLastError());
return 1;
}
else
{
printf("in thread1, OpenEvent success\n");
}
printf("3s later, the thread2 will be suspended!\n");
for(int i = 0; i < 3; i++)
{
Sleep(1000);
SetEvent(hEvent);
}printf("...suspending.....\\n"); int ret; **ret = SuspendThread((HA**
-
See the notes on this function at http://msdn.microsoft.com/en-us/library/windows/desktop/ms686345(v=vs.85).aspx[^].
Veni, vidi, abiit domum
Thank you,although it did not solve my problem... I heared that suspendThread() may cause some problems,like dead lock, it should be avoided ...
-
I am a beginner of Win API programming,and there is a strange problem that the thread1 is blocked after excuting SuspendThread() function ,but it isn't blocked every time that i run this program. anybody can help me? i'm a chinese,this is my first time here,i hope that i express my problem clearly.
#include
#include
#include
DWORD WINAPI Thread1(void *arg);
DWORD WINAPI Thread2(void *arg);int main(void)
{
DWORD threadid1,threadid2;
HANDLE handle1,handle2;
handle2 = CreateThread(NULL, 0, Thread2, (void *)handle2, 0, &threadid1);
handle1 = CreateThread(NULL, 0, Thread1, (void *)handle2, 0, &threadid2);
HANDLE hEvent;
hEvent = CreateEvent(NULL, FALSE, FALSE, "MainEvent");if(handle1 == NULL) { int err; err = GetLastError(); printf("CreateThread1 error %d\\n", err); exit(1); } else { printf("CreateThread1 success\\n"); } if(handle2 == NULL) { int err; err = GetLastError(); printf("CreateThread2 error %d\\n", err); exit(1); } else { printf("CreateThread2 success\\n"); } int priority1; int priority2; priority1 = GetThreadPriority(handle1); printf("thread1's threadid is=%d,priority is %d\\n", (int)threadid1, priority1); priority2 = GetThreadPriority(handle2); printf("thread2's threadid is=%d,priority is %d\\n\\n", (int)threadid2, priority2); /\* printf("suspending for 3s\\n"); SuspendThread(handle); Sleep(3000); printf("resume\\n"); ResumeThread(handle); \*/ WaitForSingleObject(handle1, INFINITE); DWORD exitCode; BOOL ret; ret = GetExitCodeThread(handle1, &exitCode); if(!ret) { printf("GetThread1 ExitCode,The last error is %d", (int)GetLastError()); } else printf("thread1's exit code is %d\\n", (int)exitCode); ret = GetExitCodeThread(handle2, &exitCode); if(!ret) { printf("GetThread2 ExitCode,The last error is %d", (int)GetLastError()); } else printf("thread2's exit code is %d\\n", (int)exitCode); return 0;
}
int cnt = 0;
DWORD WINAPI Thread1(void *arg)
{
HANDLE hEvent;
hEvent = OpenEvent(EVENT_ALL_ACCESS, TRUE, "MainEvent");
if(hEvent == NULL)
{
printf("in thread1, OpenEvent Error, Error No. is %d!\n", (int)GetLastError());
return 1;
}
else
{
printf("in thread1, OpenEvent success\n");
}
printf("3s later, the thread2 will be suspended!\n");
for(int i = 0; i < 3; i++)
{
Sleep(1000);
SetEvent(hEvent);
}printf("...suspending.....\\n"); int ret; **ret = SuspendThread((HA**
Not sure what you're trying to achieve by suspending the thread, but a better approach would be to signal to your thread2 that it should stop and have it stop itself instead of suspending it. When you call suspend, the state of thread2 is arbitrary (undefined) so it's probably not a good approach. When you say "sometimes the program will be blocked here", what do you mean? How do you know it's there? As far as I know, SuspendThread is non-blocking, so there should be no reason for that to be hung-up there (although not sure what happens if you call SuspendThread on a thread that no longer exists).
-
I am a beginner of Win API programming,and there is a strange problem that the thread1 is blocked after excuting SuspendThread() function ,but it isn't blocked every time that i run this program. anybody can help me? i'm a chinese,this is my first time here,i hope that i express my problem clearly.
#include
#include
#include
DWORD WINAPI Thread1(void *arg);
DWORD WINAPI Thread2(void *arg);int main(void)
{
DWORD threadid1,threadid2;
HANDLE handle1,handle2;
handle2 = CreateThread(NULL, 0, Thread2, (void *)handle2, 0, &threadid1);
handle1 = CreateThread(NULL, 0, Thread1, (void *)handle2, 0, &threadid2);
HANDLE hEvent;
hEvent = CreateEvent(NULL, FALSE, FALSE, "MainEvent");if(handle1 == NULL) { int err; err = GetLastError(); printf("CreateThread1 error %d\\n", err); exit(1); } else { printf("CreateThread1 success\\n"); } if(handle2 == NULL) { int err; err = GetLastError(); printf("CreateThread2 error %d\\n", err); exit(1); } else { printf("CreateThread2 success\\n"); } int priority1; int priority2; priority1 = GetThreadPriority(handle1); printf("thread1's threadid is=%d,priority is %d\\n", (int)threadid1, priority1); priority2 = GetThreadPriority(handle2); printf("thread2's threadid is=%d,priority is %d\\n\\n", (int)threadid2, priority2); /\* printf("suspending for 3s\\n"); SuspendThread(handle); Sleep(3000); printf("resume\\n"); ResumeThread(handle); \*/ WaitForSingleObject(handle1, INFINITE); DWORD exitCode; BOOL ret; ret = GetExitCodeThread(handle1, &exitCode); if(!ret) { printf("GetThread1 ExitCode,The last error is %d", (int)GetLastError()); } else printf("thread1's exit code is %d\\n", (int)exitCode); ret = GetExitCodeThread(handle2, &exitCode); if(!ret) { printf("GetThread2 ExitCode,The last error is %d", (int)GetLastError()); } else printf("thread2's exit code is %d\\n", (int)exitCode); return 0;
}
int cnt = 0;
DWORD WINAPI Thread1(void *arg)
{
HANDLE hEvent;
hEvent = OpenEvent(EVENT_ALL_ACCESS, TRUE, "MainEvent");
if(hEvent == NULL)
{
printf("in thread1, OpenEvent Error, Error No. is %d!\n", (int)GetLastError());
return 1;
}
else
{
printf("in thread1, OpenEvent success\n");
}
printf("3s later, the thread2 will be suspended!\n");
for(int i = 0; i < 3; i++)
{
Sleep(1000);
SetEvent(hEvent);
}printf("...suspending.....\\n"); int ret; **ret = SuspendThread((HA**
handle1 = CreateThread(NULL, 0, Thread1, (void *)handle2, 0, &threadid1);
handle2 = CreateThread(NULL, 0, Thread2, (void *)handle1, 0, &threadid2);创建Thread1的时候你调用了thread2