2 3 NULL HANDLE Create Thread 0 PSECURITYATTRIBUTES
2. 3 创建线程 NULL HANDLE Create. Thread( 0 PSECURITY_ATTRIBUTES psa, DWORD cb. Stack, 函数地址 PTHREAD_START_ROUTINE p. Start. Addr, PVOID pv. Param, 函数参数NULL DWORD fdw. Create, 控制创建线程标志 PDWORD pdw. Thread. Id); CREATE_SUSPENDED 0 NULL
#include "windows. h" #include <iostream> using namespace std; DWORD WINAPI Thread. Func(PVOID pv. Param) { cout<<"Created thread says 'hello World!'"<<endl; return 0; } int main() { HANDLE Thread. Handle = Create. Thread(NULL, 0, Thread. Func, NULL, 0, NULL); Sleep(100); cout<<"Main thread says 'Hello World!"<<endl; getchar(); return 0; }
2. 5 终止线程 ④ 进程终止运行时撤销线程 n Exit. Process 和 Terminate. Process函数将终止进程中的 所有线程; n Exit. Process只能强制本进程的退出; n Terminate. Process在一个进程中强制结束其他进程; n 进程所使用的资源被清除; n C++对象撤销函数没有被调用。 VOID Exit. Process(UINT u. Exit. Code); BOOL Terminate. Process(HANDLE h. Process, UINT u. Exit. Code);
#include <windows. h> #include <iostream> using namespace std; DWORD WINAPI Fun. One(LPVOID param) { int* p=(int*)param; cout<<(*p)<<endl; while(true) { Sleep(1000); cout<<"hello! "; } return 0; }
DWORD WINAPI Fun. Two(LPVOID param) { int* p=(int*)param; cout<<(*p)<<endl; while(true) { Sleep(1000); cout<<"world! "; } return 0; }
int main(int argc, _TCHAR* argv[]) { int input=100; int input 1=100; int input 2=200; HANDLE hand 1=Create. Thread (NULL, 0, Fun. One, (void*)&input 1, CREATE_SUSPENDED, NULL); input=200; HANDLE hand 2=Create. Thread (NULL, 0, Fun. Two, (void*)&input 2, CREATE_SUSPENDED, NULL);
while(true){ cin>>input; switch (input) { case 1 : Resume. Thread(hand 1); Resume. Thread(hand 2); case 2: Suspend. Thread(hand 1); Suspend. Thread(hand 2); case 3: Terminate. Thread(hand 1, 1); Terminate. Thread(hand 2, 1); default: return 0; } } return 0; }
2. 5 等待函数 n 等待函数:使线程进入等待状态, 直到一个对象或多个对 象变为已通知状态。 DWORD Wait. For. Single. Object( HANDLE h. Handle, DWORD dw. Milliseconds ); DWORD Wait. For. Multiple. Object( DWORD dw. Count, CONST HANDLE* ph. Handle, BOOL f. Wait. All, DWORD dw. Milliseconds ); 参数dw. Milliseconds有两个特殊值: • 0,则该函数立即返回; • INFINITE,则线程被挂起,直到h. Handle所指向的对 象变为已通知状态。
2. 5 等待函数 #include <windows. h> #include <iostream> using namespace std; DWORD WINAPI Fun. One(LPVOID param) { cout<<"hello world! "; return 0; }
int main(int argc, _TCHAR* argv[]) { HANDLE hd[2]; for (int i=0; i<2; i++) { hd[i]=Create. Thread (NULL, 0, Fun. One, 0, 0, NULL); } for (int i=0; i<2; i++) { Wait. For. Single. Object(h[i], INFINITE); Close. Handle(h[i]); } getchar(); }
int main(int argc, _TCHAR* argv[]) { HANDLE hd[2]; for (int i=0; i<2; i++) { hd[i]=Create. Thread (NULL, 0, Fun. One, 0, 0, NULL); } Wait. For. Multiple. Object(2, h, TRUE, INFINITE); for (int i=0; i<2; i++) { Close. Handle(h[i]); } getchar(); }
int main( ) { int i, j, k, n; printf("'水仙花数'是: "); for(n=100; n<1000; n++) { i=n/100; j=n/10 -i*10; k=n%10; if(i*100+j*10+k==i*i*i+j*j*j+k*k*k) { printf("%d ", n); } } printf("n"); getchar(); return 0; }
struct bound { int low; int high; }; DWORD WINAPI Thread 1(PVOID pv. Param) { int i, j, k, n; bound* bou=(bound*)pv. Param; int low = bou->low; int high = bou->high; for(n=low; n<high; n++) { i=n/100; j=n/10 -i*10; k=n%10; if(i*100+j*10+k==i*i*i+j*j*j+k*k*k) { printf("%d ", n); } } return 0; }
int main( ) { printf("'水仙花数'是: "); bound qw 1, qw 2; qw 1. low=100; qw 1. high =500; HANDLE Thread. Handle 1= Create. Thread(NULL, 0, Thread 1, &qw 1, 0, NULL); qw 2. low=500; qw 2. high=1000; HANDLE Thread. Handle 2 = Create. Thread(NULL, 0, Thread 1, &qw 2, 0, NULL); HANDLE Thread. Handles[2]= {Thread. Handle 1, Thread. Handle 2}; Wait. For. Multiple. Objects(2, Thread. Handles, TRUE, INFINITE); return 0; }
3. 1 互锁函数 Long g_x = 0; //全局� 量 DWORD WINAPI Thread. Func 1 (PVOID pv. Param) { g_x++; return 0; } DWORD WINAPI Thread. Func 2 (PVOID pv. Param) { g_x++; MOV EAX, [g_x] return 0; INC EAX } MOV [g_x], EAX
3. 1 互锁函数 Long g_x=0; //全局变量 DWORD WINAPI Thread. Func 1 (PVOID pv. Param) { Interlocked. Exchange. Add(&g_x, 1); return 0; } DWORD WINAPI Thread. Func 2 (PVOID pv. Param) { Interlocked. Exchange. Add(&g_x, 1); return 0; }
例: 10000个 2相加 int main( ) { int sum=0; for(int i=1; i<=10000; i++) { sum=sum+2; } printf("10000个 2相加之和是 %d", sum); getchar(); return 0; }
#include "windows. h" long sum=0; DWORD WINAPI Thread 1(PVOID pv. Param) { for(int i=1; i<=5000; i++) { Interlocked. Exchange. Add(&sum, 2); // sum=sum+2; } return 0; }
DWORD WINAPI Thread 2(PVOID pv. Param) { for(int i=5001; i<=10000; i++) { Interlocked. Exchange. Add(&sum, 2); // sum=sum+2; } return 0; }
int main() { HANDLE Thread. Handle 1 = Create. Thread(NULL, 0, Thread 1, NULL, 0, NULL); HANDLE Thread. Handle 2 = Create. Thread(NULL, 0, Thread 2, NULL, 0, NULL); HANDLE Thread. Handles[2]={Thread. Handle 1, Thread. Handle 2}; Wait. For. Multiple. Objects(2, Thread. Handles, TRUE, INFINITE); printf("10000个 2相加之和是 %d", sum); getchar(); return 0; }
3. 2 临界区 首先定义一个临界区对象(通常全局变量) CRITICAL_SECTION cs n 临界区对象初始化 Initialize. Critical. Section (&cs) n 进入临界区 Enter. Critical. Section (&cs) n 离开临界区 Leave. Critical. Section (&cs) n 释放临界区对象 Delete. Critical. Section (&cs) n
#include <windows. h> #include <fstream> fstream file; 临界区 例1 DWORD WINAPI Thread. Func 1(PVOID param) { for(int i=1; i<=1000; i++) { file<<"Thread. Func 1 Output"<<i<<endl; } return 0; } DWORD WINAPI Thread. Func 2(PVOID param) { for(int i=1; i<=1000; i++) { file<<"Thread. Func 2 Output"<<i<<endl; } return 0; }
int main() { file. open("data. txt", ios: : out); HANDLE Thread. Handle 1 = Create. Thread(NULL, 0, Thread. Func 1, NULL, 0, NULL); HANDLE Thread. Handle 2 = Create. Thread(NULL, 0, Thread. Func 2, NULL, 0, NULL); HANDLE h. Thread[2] = {Thread. Handle 1, Thread. Handle 2}; Wait. For. Multiple. Objects(2, h. Thread, TRUE, INFINITE); file. close(); return 0; }
#include <windows. h> #include <fstream> fstream file; CRITICAL_SECTION cs; DWORD WINAPI Thread. Func 1(PVOID param) { for(int i=1; i<=1000; i++) { Enter. Critical. Section (&cs); file<<"Thread. Func 1 Output"<<i<<endl; Leave. Critical. Section (&cs); } return 0; } 加 上 临 界 区
DWORD WINAPI Thread. Func 2(PVOID param) { for(int i=1; i<=1000; i++) { Enter. Critical. Section (&cs); file<<"Thread. Func 2 Output"<<i<<endl; Leave. Critical. Section (&cs); } return 0; }
#include “windows. h“ 例2 CRITICAL_SECTION g_cs; char g_c. Array[10]; // 共享� 源 DWORD WINAPI Thread. Proc 1(PVOID p. Param) { Enter. Critical. Section(&g_cs); for (int i = 0; i < 10; i++) { g_c. Array[i] = 'a'; // � 共享� 源� 行写入操作 Sleep(1); } Leave. Critical. Section(&g_cs); return 0; }
DWORD WINAPI Thread. Proc 2(PVOID p. Param) { Enter. Critical. Section(&g_cs); for (int i = 0; i < 10; i++) { g_c. Array[10 - i - 1] = 'b'; // � 共享� 源� 行写操 作 Sleep(1); } Leave. Critical. Section(&g_cs); return 0; }
int main() { Initialize. Critical. Section(&g_cs); HANDLE Thread. Handle 1 = Create. Thread(NULL, 0, Thread. Proc 1, NULL, 0, NULL); HANDLE Thread. Handle 2 = Create. Thread(NULL, 0, Thread. Proc 2, NULL, 0, NULL); HANDLE Thread. Handles[2]= {Thread. Handle 1, Thread. Handle 2}; Wait. For. Multiple. Objects(2, Thread. Handles, TRUE, INFINITE); Delete. Critical. Section(&g_cs); printf(g_c. Array); getchar(); return 0;
3. 2 临界区 使线程休眠再唤醒线程非常耗时。 n 避免让线程休眠:Try. Enter. Critical. Section() while (counter<100) { while(! Try. Enter. Critical. Section(&cs)) int number=counter++; Leave. Critical. Section(&cs); } n
3. 3 事件 n 创建事件内核对象,返回句柄。 HANDLE Create. Event( PSECURITY_ATTRIBUTES psa, BOOL f. Manual. Reset, BOOL f. Initial. State, PCTSTR psz. Name ); // 安全属性 //复位方式 // 初始状态 // 对象名称
3. 3 事件 n 打开一个已经存在的命名事件对象 HANDLE Open. Event( DWORD fdw. Access, BOOL f. Inherit, PCTSTR psz. Name ); • EVENT_ALL_ACCESS 要求对事件对象进行完全访问 • EVENT_MODIFY_STATE 允许Set. Event 和 Reset. Event函数 • SYNCHRONIZE 允许事件对象的使用同步
#include <windows. h> 例1:用户在主线程输入命令, #include <iostream> 控制子线程的运行。 #include <string> using namespace std; CRITICAL_SECTION cs; DWORD WINAPI Thread. Func(PVOID param) { Enter. Critical. Section (&cs); cout<<"Create Thread: Create thread is started"<<endl; cout<<"Create Thread: Create thread is waiting continue command. . "<<endl; Leave. Critical. Section (&cs); HANDLE ph. Event = Open. Event(EVENT_ALL_ACCESS, TRUE, "Continue. Command"); Wait. For. Single. Object(ph. Event, INFINITE); cout<<"Create Thread: Recieved continue command. "<<endl; cout<<"Create Thread: Thread runs again. "<<endl; Sleep(2000); cout<<"Create Thread: Thread finished. "<<endl; return 0;
int main() { Initialize. Critical. Section (&cs); HANDLE h. Event = Create. Event(NULL, FALSE, "Continue. Command"); cout<<"Main Thread: Creating new thread. "<<endl; HANDLE Thread. Handle = Create. Thread(NULL, 0, Thread. Func, NULL, CREATE_SUSPENDED, NULL); cout<<"Main Thread: New thread created. "<<endl; Resume. Thread(Thread. Handle); string input; while(TRUE) { Enter. Critical. Section (&cs); cout<<"Main Thread: input command, please"<<endl; Leave. Critical. Section (&cs); cout<<">";
cin>>input; if(input=="continue") { cout<<"Main Thread: Let thread continue run"<<endl; Set. Event(h. Event); break; } } Wait. For. Single. Object(Thread. Handle, INFINITE); cout<<"Main Thread: Create thread finished"<<endl; Delete. Critical. Section (&cs); Close. Handle(h. Event); return 0; }
#include <windows. h> HANDLE g_h. Event; void Open. File. And. Read. Contents. Into. Memory() { printf("Open File and Read contents into memoryn"); } DWORD WINAPI Word. Count(PVOID pv. Param) { Wait. For. Single. Object(g_h. Event, INFINITE); printf("0: word countn"); Set. Event(g_h. Event); //自� return(0); }
DWORD WINAPI Spell. Check(PVOID pv. Param) { Wait. For. Single. Object(g_h. Event, INFINITE); printf("1: Spell checkn"); //Access the memory block. Set. Event(g_h. Event); //自� return(0); } DWORD WINAPI Grammar. Check(PVOID pv. Param) { Wait. For. Single. Object(g_h. Event, INFINITE); printf("2: Grammar checkn"); //Access the memory block. Set. Event(g_h. Event); //自� return(0); }
int main() { // g_h. Event = Create. Event(NULL, TRUE, FALSE, NULL); //人 复位 g_h. Event = Create. Event(NULL, FALSE, NULL); //自� 复位 HANDLE h. Thread[3]; DWORD dw. Thread. ID[3]; h. Thread[0] = Create. Thread(NULL, 0, Word. Count, NULL, 0, &dw. Thread. ID[0]); h. Thread[1] = Create. Thread(NULL, 0, Spell. Check, NULL, 0, &dw. Thread. ID[1]); h. Thread[2] = Create. Thread(NULL, 0, Grammar. Check, NULL, 0, &dw. Thread. ID[2]);
Open. File. And. Read. Contents. Into. Memory(); //Allow all 3 threads to access the memory. Set. Event(g_h. Event); Wait. For. Multiple. Objects (3, h. Thread, TRUE, INFINITE); printf("main thread exitn"); getchar(); return 0; }
HANDLE ev. Read, ev. Finish; DWORD WINAPI Read. Thread(PVOID param) { Wait. For. Single. Object (ev. Read , INFINITE); cout<<"Reading"<<endl; Set. Event (ev. Finish); } DWORD WINAPI Write. Thread(PVOID param) { cout<<"Writing"<<endl; Set. Event (ev. Read); }
int main() { ev. Read = Create. Event (NULL , FALSE , NULL) ; ev. Finish = Create. Event (NULL , FALSE , NULL) ; Create. Thread(NULL, 0, Read. Thread, NULL, 0, NULL); Create. Thread(NULL, 0, Write. Thread, NULL, 0, NULL); Wait. For. Single. Object (ev. Finish, INFINITE) ; cout<<"The Program is End"<<endl; return 0 ; }
3. 4 互斥量 n 打开一个已经存在的命名互斥对象 HANDLE Open. Mutex( DWORD fdw. Access, // access BOOL b. Inherit. Handle, // inheritance option PCTSTR psz. Name // object name ); • MUTEX_ALL_ACCESS 请求对互斥对象的完全访问 • MUTEX_MODIFY_STATE 允许使用 Release. Mutex 函数 • SYNCHRONIZE 允许使用互斥对象同步
#include <windows. h> #include <fstream> using namespace std; fstream file; DWORD WINAPI Thread. Func 1(PVOID param) { HANDLE *ph. Mutex=(HANDLE *)param; for(int i=1; i<=100; i++) { Wait. For. Single. Object(*ph. Mutex, INFINITE); file<<"Thread. Func 1 Output"<<i<<endl; Release. Mutex(*ph. Mutex); } return 0; } 例1
DWORD WINAPI Thread. Func 2(PVOID param) { HANDLE *ph. Mutex=(HANDLE *)param; for(int i=1; i<=100; i++) { Wait. For. Single. Object(*ph. Mutex, INFINITE); file<<"Thread. Func 2 Output"<<i<<endl; Release. Mutex(*ph. Mutex); } return 0; }
int main() { file. open("data. txt", ios: : out); HANDLE h. Mutex = Create. Mutex(NULL, FALSE, "Display. Mutex"); HANDLE Thread. Handle 1 = Create. Thread(NULL, 0, Thread. Func 1, &h. Mutex, 0, NULL); HANDLE Thread. Handle 2 = Create. Thread(NULL, 0, Thread. Func 2, &h. Mutex, 0, NULL); HANDLE h. Thread[2] = {Thread. Handle 1, Thread. Handle 2}; Wait. For. Multiple. Objects(2, h. Thread, TRUE, INFINITE); Close. Handle(h. Mutex); file. close(); return 0;
#include "windows. h" CRITICAL_SECTION cs; int a[ 5]; DWORD WINAPI Thread(PVOID p. Params ) { int i, num = 0; while( TRUE ) { Enter. Critical. Section( &cs ); for ( i = 0; i < 5; i++ ) a[ i ] = num; Leave. Critical. Section( &cs ); num++; } return 0; } 临界区
临界区 int main() { Initialize. Critical. Section( &cs ); Create. Thread( NULL, 0, Thread, NULL, 0, NULL ); while( TRUE ) { Enter. Critical. Section( &cs ); printf("%d %d %dn", a[ 0 ], a[ 1 ], a[ 2 ], a[ 3 ], a[ 4 ] ); Leave. Critical. Section( &cs ); } return 0; }
#include <windows. h> HANDLE h. Mutex; int a[ 5 ]; DWORD WINAPI Thread(PVOID p. Params ) { int i, num = 0; while( TRUE ) { Wait. For. Single. Object( h. Mutex, INFINITE ); for ( i = 0; i < 5; i++ ) a[ i ] = num; Release. Mutex( h. Mutex ); num++; } return 0; 互斥量
互斥量 int main() { h. Mutex = Create. Mutex( NULL, FALSE, NULL ); Create. Thread( NULL, 0, Thread, NULL, 0, NULL ); while( TRUE ) { Wait. For. Single. Object( h. Mutex, INFINITE ); printf("%d %d %dn", a[ 0 ], a[ 1 ], a[ 2 ], a[ 3 ], a[ 4 ] ); Release. Mutex( h. Mutex ); } return 0; }
#include <windows. h> HANDLE h. Event 1, h. Event 2; int a[ 5 ]; DWORD WINAPI Thread(PVOID p. Params ) { int i, num = 0; while( TRUE ) { Wait. For. Single. Object( h. Event 2, INFINITE ); for ( i = 0; i < 5; i++ ) a[ i ] = num; Set. Event( h. Event 1 ); num++; } return 0; 事件
int main() 事件 { h. Event 1 = Create. Event( NULL, FALSE, TRUE, NULL ); h. Event 2 = Create. Event( NULL, FALSE, NULL ); Create. Thread( NULL, 0, Thread, NULL, 0, NULL ); while( TRUE ) { Wait. For. Single. Object( h. Event 1, INFINITE ); printf("%d %d %dn", a[ 0 ], a[ 1 ], a[ 2 ], a[ 3 ], a[ 4 ] ); Set. Event( h. Event 2 ); } return 0; }
3. 5 信号量 n 创建信号量 HANDLE Create. Semaphore( PSECURITY_ATTRIBUTES psa, LONG l. Initial. Count, // initial count LONG l. Maximum. Count, // maximum count PCTSTR psz. Name // object name );
3. 5 信号量 n 打开一个已经存在的命名信号量对象。 HANDLE Open. Semaphore( DWORD fdw. Access, BOOL b. Inherit. Handle, // inheritance option PCTSTR psz. Name // object name ); • SEMAPHORE_ALL_ACCESS 要求对信号量的完全访问; • SEMAPHORE_MODIFY_STATE 允许使用Release. Semaphore函数; • SYNCHRONIZE 允许使用信号量同步。
3. 5 信号量 n 释放信号量 Release. Semaphore( HANDLE h. Sem, LONG l. Release. Count, PLONG pl. Previous. Count ); n 等待信号量 DWORD Wait. For. Single. Object( HANDLE h. Handle, DWORD dw. Milliseconds );
HANDLE hsem 1=Create. Semaphore(NULL, 5, 10, "sem 1"); HANDLE hsem 2=Create. Semaphore(NULL, 5, 10, "sem 2"); int i 1=0; int i 2=0; DWORD WINAPI Thread. Func 1(PVOID param) { for(int i=1; i<=100; i++) { Wait. For. Single. Object(hsem 1, INFINITE); Release. Semaphore(hsem 2, 1, NULL); i 1++; file<<" i 1="<<i 1<<" i 2="<<i 2<<endl; } return 0; }
DWORD WINAPI Thread. Func 2(PVOID param) { for(int i=1; i<=100; i++) { Wait. For. Single. Object(hsem 2, INFINITE); Release. Semaphore(hsem 1, 1, NULL); i 2++; file<<" i 1="<<i 1<<" i 2="<<i 2<<endl; } return 0; }
//生� 者函数 DWORD WINAPI Producer(PVOID p. Param) { int i = 0; pointer = 0; while( i < 100 ) { Wait. For. Single. Object(h. Empty, INFINITE); Enter. Critical. Section( &cs. Array); array[(pointer++)%5] = i + 1; Leave. Critical. Section( &cs. Array); Release. Semaphore(h. Full, 1, NULL); i++; } return 0; }
//消� 者函数A DWORD WINAPI Consumer. A(LPVOID lp. Param) { while(1) { Wait. For. Single. Object(h. Full, INFINITE); Enter. Critical. Section( &cs. Array); sum += array[(pointerget ++ )%5]; printf("Consumer. A get %d n", array[(pointerget -1)%5]); if(pointerget == 100) printf("The sum is %d", sum); Leave. Critical. Section( &cs. Array); Release. Semaphore(h. Empty, 1, NULL); } return 0; }
//消� 者函数B DWORD WINAPI Consumer. B(LPVOID lp. Param) { while(1) { Wait. For. Single. Object(h. Full, INFINITE); Enter. Critical. Section( &cs. Array); sum += array[(pointerget ++ )%5]; printf("Consumer. B get %d n", array[(pointerget -1 )%5]); if(pointerget == 100) printf("The sum is %d", sum); Leave. Critical. Section( &cs. Array); Release. Semaphore(h. Empty, 1, NULL); } return 0; }
void main() { HANDLE h. Thread. Producer, h. Thread. Consumer. A, h. Thread. Comsumer. B; sum = 0; pointerget = 0; Initialize. Critical. Section( &cs. Array); h. Full = Create. Semaphore(NULL, 0, 5, NULL); h. Empty = Create. Semaphore(NULL, 5, 5, NULL); h. Thread. Producer = Create. Thread(NULL, 0, Producer, NULL, 0, NULL); h. Thread. Consumer. A = Create. Thread(NULL, 0, Consumer. A, NULL, 0, NULL); h. Thread. Comsumer. B = Create. Thread(NULL, 0, Consumer. B, NULL, 0, NULL); getchar(); }
#include <windows. h> const int PHILOSOPHERS = 5; //哲学家人数 const int TIME_EATING = 50; //吃� 需要的�� 毫秒 HANDLE event[PHILOSOPHERS]; //主� 程同 作� 程保持 同步的句柄数� HANDLE mutex[PHILOSOPHERS]; //mutex数� ,� 里相当 于公共� 源筷子 CRITICAL_SECTION cs; //控制打印的� 界区� 量
DWORD WINAPI Thread. Func(PVOID arg) { int num = *((int*)arg); DWORD ret = 0; while (1) { ret = Wait. For. Multiple. Objects(2, mutex, TRUE, 1000); if (ret == WAIT_TIMEOUT) { Sleep(100); continue; } Enter. Critical. Section(&cs); printf( "philosopher %d eattingn" , num ); Leave. Critical. Section(&cs); Sleep(TIME_EATING); break; } Set. Event(event[num]); //� 置��� 有信号 return 1; }
int main() { HANDLE h. Thread; Initialize. Critical. Section(&cs); for (int i = 0; i < PHILOSOPHERS; i++) { mutex[i] = Create. Mutex(NULL, FALSE, NULL); event[i] = Create. Event(NULL, TRUE, FALSE, NULL); h. Thread = Create. Thread(NULL, 0, Thread. Func, (void*)&i, 0, NULL); if (h. Thread == 0) { printf("create thread %d failed with code: %dn" , i , Get. Last. Error()); Delete. Critical. Section(&cs); return -1; } Close. Handle(h. Thread); }
//等待所有的哲学家吃�� 束 DWORD ret = Wait. For. Multiple. Objects (PHILOSOPHERS, event, TRUE, INFINITE); if (ret == WAIT_OBJECT_0) { printf( "all the philosophers had a dinner!n" ); } else { printf( "Wait. For. Multiple. Objects failed with code: %dn" , Get. Last. Error() ); } Delete. Critical. Section(&cs); for (int j = 0; j < PHILOSOPHERS; j++) { Close. Handle(mutex[j]); } getchar(); return 1;
4 设置线程的优先级 线程优先级 idle 标志 THREAD_PRIORITY_IDLE LOWEST THREAD_PRIORITY_LOWEST Below THREAD_PRIORITY_BELOW_NORMAL normal THREAD_PRIORITY_NORMAL Above THREAD_PRIORITY_ABOVE_NORMAL high THREAD_PRIORITY_HIGHEST CRITICAL THREAD_PRIORITY_TIME_CRITICAL 线程优先级
4 设置线程的优先级 DWORD WINAPI fastthread(PVOID arg) { double d=0. 0; cout<<“Fast thread startedn”; Set. Thread. Priority(Get. Current. Thread(), THREAD_PRIORITY_ABOVE_NORMAL); for (int i=0; i<10000000; i++) { d+=d; } cout<<“Fast thread finishedn”; return 0; }
4 设置线程的优先级 DWORD WINAPI slowthread(PVOID arg) { double d=0. 0; cout<<“Slow thread startedn”; Set. Thread. Priority(Get. Current. Thread(), THREAD_PRIORITY_BELOW_NORMAL); for (int i=0; i<10000000; i++) { d+=d; } cout<<“Slow thread finishedn”; return 0; }
4 设置线程的优先级 int main() { HANDLE hfast, hslow; hfast = Create. Thread(NULL, 0, fastthread, NULL, 0, NULL); hslow = Create. Thread(NULL, 0, slowthread, NULL, 0, NULL); Wait. For. Single. Object(hfast, INFINITE); Wait. For. Single. Object(hslow, INFINITE); Close. Handle(hfast); Close. Handle(hslow); return 0; }
- Slides: 107