// マルチスレッド セマフォー #include "stdafx.h" #include "conio.h" #include "process.h" #include "windows.h" int mx=1, numP=2; void P(int *S){ while(*S<=0); (*S)--;} void V(int *S){ (*S)++;} void f(void *a){ P(&mx); for(int i=0;i<3;i++){ printf("\n %d", (int) a); Sleep(10); } V(&mx); numP--; } int _tmain(int argc, _TCHAR* argv[]) { int A=1111, B=2222; while(getch()!=EOF){ mx=1;numP=2; _beginthread(f, 0, (void *)A); _beginthread(f, 0, (void *)B); while(numP!=0); printf("\n *** Process end\n"); } return 0; }