VS C++スレッド編の5スレッドの状態
6173 ワード
スレッドの保留
スレッド保留中の数はマクロMAXIMUM_SUSPEND_COUNT決定.スレッドが停止した後、ブロック状態に入るスレッドは、深層的に停止するだけでなく、連続的に複数回停止することができます.何度掛けても何度も回復しなければならない.注意:現在のスレッドを一時停止すると、スタック操作が存在すると、スタックがロックされ、他のスレッドはスタックを操作できません.
保留後のリカバリ
回復したら準備完了
スレッドスリープ
dwMillisecondsパラメータ
0:現在のタイムスライスを放棄して準備が整い、オペレーティングシステムに決定権を渡す
INFINITE:常にブロック状態に入る
スレッド切り替え
Sleep(0)は、同等の優先度を扱うスレッドである
準備状態が実行状態に入り、オペレーティングシステムの呼び出しによって決定されます.
スレッドコンテキストの取得(フィールド)
この構造体は、プロセッサレジスタによるデータを含む
スレッドフィールドの取得手順
インスタンス1:スレッドの保留とリカバリ
実行結果
インスタンス2:スレッドスリープ切り替え
実行結果
インスタンス3:優先度によってスレッドのスリープが切り替えられない
実行結果
インスタンス4:異なる優先度、スレッド切り替え
実行結果
スレッド保留中の数はマクロMAXIMUM_SUSPEND_COUNT決定.スレッドが停止した後、ブロック状態に入るスレッドは、深層的に停止するだけでなく、連続的に複数回停止することができます.何度掛けても何度も回復しなければならない.注意:現在のスレッドを一時停止すると、スタック操作が存在すると、スタックがロックされ、他のスレッドはスタックを操作できません.
DWORD WINAPI SuspendThread(
__in HANDLE hThread
);
保留後のリカバリ
回復したら準備完了
DWORD WINAPI ResumeThread(
__in HANDLE hThread
);
スレッドスリープ
VOID WINAPI Sleep(
__in DWORD dwMilliseconds //
);
実行後、スレッドはブロック状態に入り、睡眠時間後に準備状態に入るdwMillisecondsパラメータ
0:現在のタイムスライスを放棄して準備が整い、オペレーティングシステムに決定権を渡す
INFINITE:常にブロック状態に入る
スレッド切り替え
BOOL WINAPI SwitchToThread(void);
この関数の機能は、自分の現在のタイムスライスを放棄し、オペレーティングシステムに優先度の低いスレッドを処理することです.Sleep(0)は、同等の優先度を扱うスレッドである
準備状態が実行状態に入り、オペレーティングシステムの呼び出しによって決定されます.
スレッドコンテキストの取得(フィールド)
BOOL WINAPI GetThreadContext(
__in HANDLE hThread,
__in_out LPCONTEXT lpContext
);
typedef struct _CONTEXT {
...
} CONTEXT, *LPCONTEXT;
この構造体は、プロセッサレジスタによるデータを含む
スレッドフィールドの取得手順
CONTEXT &Context;
SuspendThread(hThread)
GetThreadContext(hThread, &Context)
インスタンス1:スレッドの保留とリカバリ
#include
#include
#include
DWORD WINAPI ThreadProFunc(LPVOID lpParam);
int main(int argc, char **argv)
{
int suspendCount = 0;
int resumSuspendCount = 0;
HANDLE hThread;
DWORD dwThreadId;
hThread = CreateThread( NULL, NULL, ThreadProFunc, NULL, 0, &dwThreadId);
suspendCount = SuspendThread(hThread); // ,
printf("suspendCount = %d
", suspendCount);
suspendCount = SuspendThread(hThread);
printf("suspendCount = %d
", suspendCount);
suspendCount = SuspendThread(hThread);
suspendCount = SuspendThread(hThread);
printf("suspendCount = %d
", suspendCount);
//
for(int i = 0; i <= suspendCount; i++) {
resumSuspendCount = ResumeThread(hThread); // ,
printf("resumSuspendCount = %d
", resumSuspendCount);
}
CloseHandle(hThread); //
system("pause");
return 0;
}
DWORD WINAPI ThreadProFunc(LPVOID lpParam)
{
for(int i = 0; i < 4; i++) {
printf("hello
");
}
return 0;
}
実行結果
suspendCount = 0
suspendCount = 1
suspendCount = 3
resumSuspendCount = 4
resumSuspendCount = 3
resumSuspendCount = 2
resumSuspendCount = 1
hello
hello
hello
hello
. . .
インスタンス2:スレッドスリープ切り替え
#include
#include
#include
DWORD WINAPI ThreadProFuncA(LPVOID lpParam);
DWORD WINAPI ThreadProFuncB(LPVOID lpParam);
int main(int argc, char **argv)
{
HANDLE hThreadA;
HANDLE hThreadB;
DWORD dwThreadIdA;
DWORD dwThreadIdB;
hThreadA = CreateThread( NULL, NULL, ThreadProFuncA, NULL, 0, &dwThreadIdA);
hThreadB = CreateThread( NULL, NULL, ThreadProFuncB, NULL, 0, &dwThreadIdB);
CloseHandle(hThreadA);
CloseHandle(hThreadB);
system("pause");
return 0;
}
DWORD WINAPI ThreadProFuncA(LPVOID lpParam)
{
for(int i = 0; i < 4; i++) {
printf("hello
");
Sleep(500);
}
return 0;
}
DWORD WINAPI ThreadProFuncB(LPVOID lpParam)
{
for(int i = 0; i < 4; i++) {
printf("world
");
Sleep(500);
}
return 0;
}
実行結果
hello
world
. . . hello
world
hello
world
hello
world
インスタンス3:優先度によってスレッドのスリープが切り替えられない
#include
#include
#include
DWORD WINAPI ThreadProFuncA(LPVOID lpParam);
DWORD WINAPI ThreadProFuncB(LPVOID lpParam);
int main(int argc, char **argv)
{
HANDLE hThreadA;
HANDLE hThreadB;
DWORD dwThreadIdA;
DWORD dwThreadIdB;
hThreadA = CreateThread( NULL, NULL, ThreadProFuncA, NULL, 0, &dwThreadIdA);
SetThreadPriority(hThreadA, THREAD_PRIORITY_HIGHEST);
hThreadB = CreateThread( NULL, NULL, ThreadProFuncB, NULL, 0, &dwThreadIdB);
SetThreadPriority(hThreadB, THREAD_PRIORITY_LOWEST);
CloseHandle(hThreadA);
CloseHandle(hThreadB);
system("pause");
return 0;
}
DWORD WINAPI ThreadProFuncA(LPVOID lpParam)
{
for(int i = 0; i < 4; i++) {
printf("hello
");
Sleep(0); //
}
return 0;
}
DWORD WINAPI ThreadProFuncB(LPVOID lpParam)
{
for(int i = 0; i < 4; i++) {
printf("world
");
Sleep(0);
}
return 0;
}
実行結果
hello
hello
hello
hello
world
world
world
world
. . .
インスタンス4:異なる優先度、スレッド切り替え
#include
#include
#include
DWORD WINAPI ThreadProFuncA(LPVOID lpParam);
DWORD WINAPI ThreadProFuncB(LPVOID lpParam);
int main(int argc, char **argv)
{
HANDLE hThreadA;
HANDLE hThreadB;
DWORD dwThreadIdA;
DWORD dwThreadIdB;
hThreadA = CreateThread( NULL, NULL, ThreadProFuncA, NULL, 0, &dwThreadIdA);
SetThreadPriority(hThreadA, THREAD_PRIORITY_HIGHEST);
hThreadB = CreateThread( NULL, NULL, ThreadProFuncB, NULL, 0, &dwThreadIdB);
SetThreadPriority(hThreadB, THREAD_PRIORITY_LOWEST);
CloseHandle(hThreadA);
CloseHandle(hThreadB);
system("pause");
return 0;
}
DWORD WINAPI ThreadProFuncA(LPVOID lpParam)
{
for(int i = 0; i < 4; i++) {
printf("hello
");
SwitchToThread(); //
}
return 0;
}
DWORD WINAPI ThreadProFuncB(LPVOID lpParam)
{
for(int i = 0; i < 4; i++) {
printf("world
");
SwitchToThread();
}
return 0;
}
実行結果
hello
hello
hello
world
hello
world
world
world
. . .