| // thread1.cpp #include #include #include // The function to run in a thread void HonkThread(DWORD iter) { DWORD i; for (i=0; i < iter; i++) { Beep(200, 50); Sleep(1000); } } void main(void) { HANDLE honkHandle; DWORD threadID; DWORD iterations; CHAR iterStr[100]; cout << "Enter the number of beeps to produce: "; cin.getline(iterStr, 100); // convert string into integer iterations=atoi(iterStr); // create a thread which // executes the "HonkThread" function honkHandle=CreateThread(0, 0, (LPTHREAD_START_ROUTINE) HonkThread, (VOID *) iterations, 0, &threadID); // wait until the thread has finished int count=0; while ( WaitForSingleObject(honkHandle, 0)== WAIT_TIMEOUT) { cout<< "waiting for the thread to finish " << count++ << endl; } } |
| #include #include #include typedef struct { DWORD frequency; DWORD duration; DWORD iterations; } honkParams; void HonkThread(honkParams *params) { DWORD i; for (i=0; i < params->iterations; i++) { Beep(params->frequency, params->duration); Sleep(1000); } } void main(void) { HANDLE honkHandle; DWORD threadID; honkParams params; CHAR freqStr[100]; CHAR durStr[100]; CHAR iterStr[100]; cout << "Enter the beep frequency to produce: "; cin.getline(freqStr, 100); params.frequency=atoi(freqStr); cout << "Enter the beep duration to produce: "; cin.getline(durStr, 100); params.duration=atoi(durStr); cout << "Enter the number of beeps to produce: "; cin.getline(iterStr, 100); params.iterations=atoi(iterStr); // create a thread and pass it the address of file://the "params" structure honkHandle=CreateThread(0, 0, (LPTHREAD_START_ROUTINE) HonkThread, ms, 0, &threadID); WaitForSingleObject(honkHandle, INFINITE); } |