// CreateThread.cpp : Defines the entry point for the console application. // #include "stdafx.h" #include #include using namespace std; HANDLE S; DWORD WINAPI fce ( void * param ) { int n = (int) param; for (int i = 1; i <= n; i++) { WaitForSingleObject (S, INFINITE); // DOwn cout << "Moje vlakno " << i << "," << n << endl; ReleaseSemaphore (S, 1, NULL); // Up // Sleep (1000); } return 0; } int _tmain(int argc, _TCHAR* argv[]) { S = CreateSemaphore (NULL, // attributes 1, // init 1, // max NULL // name ); if (S != NULL) cout << "Semaphore created" << endl; else cout << "Error creating semaphore" << endl; HANDLE h1; h1 = CreateThread (NULL, // LPSECURITY_ATTRIBUTES lpThreadAttributes 0, // SIZE_T dwStackSize, fce, // LPTHREAD_START_ROUTINE lpStartAddress, (void *) 10, // LPVOID lpParameter, CREATE_SUSPENDED, // DWORD dwCreationFlags, NULL // LPDWORD lpThreadId ); HANDLE h2; h2 = CreateThread (NULL, // LPSECURITY_ATTRIBUTES lpThreadAttributes 0, // SIZE_T dwStackSize, fce, // LPTHREAD_START_ROUTINE lpStartAddress, (void *) 20, // LPVOID lpParameter, CREATE_SUSPENDED, // DWORD dwCreationFlags, NULL // LPDWORD lpThreadId ); SetPriorityClass (GetCurrentProcess(), REALTIME_PRIORITY_CLASS); int n = GetPriorityClass (GetCurrentProcess()); cout << "Priority " << n << endl; SetThreadPriority (h1, THREAD_PRIORITY_TIME_CRITICAL); SetThreadPriority (h2, THREAD_PRIORITY_IDLE); SuspendThread (h1); ResumeThread (h1); ResumeThread (h1); ResumeThread (h2); cout << "Konec programu" << endl; char buf [10]; cin.getline(buf, sizeof (buf)-1); return 0; }