两个多线程小程序

时间:2018-05-13 14:59:07
【文件属性】:
文件名称:两个多线程小程序
文件大小:17.67MB
文件格式:RAR
更新时间:2018-05-13 14:59:07
多线程 求质数 多线程 求质数 返回数组中的最大值 bool isPrime(long x) { if (x <= 1) return false; if (x == 2) return true; for (long i = 2; i <= ceil(sqrt((long double)x));i++) if (x%i == 0) return false; return true; } DWORD WINAPI findPrime(void* p) { long result=0; int l = stc(p)->lower, u = stc(p)->uper; int t_id=GetCurrentThreadId(); for (int i = l; i <= u;i++) if (isPrime(i)) { result++; } DWORD dReturn = WaitForSingleObject(mutex_mul_result_h, INFINITE); mul_result += result; ReleaseMutex(mutex_mul_result_h); //EnterCriticalSection(&gCRITICAL_SECTION_Printf); //printf("%d,%d,%d,%d\n", l, u, result,t_id); //fflush(stdout); //LeaveCriticalSection(&gCRITICAL_SECTION_Printf); return 0; } //dispatcher void dispatch() { DWORD Status; timer tm; tm.start(); //srand(time(NULL)); long step = STEP;//ceil(double(TEST/10)); handlenum = 0; for (int i = 1; i <= TEST;) { i += step; handlenum++; } handle_array=new HANDLE[handlenum]; Thread_id = new DWORD[handlenum ]; arg = new FP_ARG[handlenum]; InitializeCriticalSection(&gCRITICAL_SECTION_Printf); mutex_mul_result_h = CreateMutex(NULL, false, mutex_mul_result); handlenum = 0; for (int i = 1; i <= TEST;) { arg[handlenum].lower = i; arg[handlenum].uper = (i + step-1>TEST) ? TEST : i + step-1; handle_array[handlenum]=(HANDLE)CreateThread(NULL, 0, findPrime, &arg[handlenum], 0, &Thread_id[handlenum]); /*EnterCriticalSection(&gCRITICAL_SECTION_Printf); printf("lower:%d uper:%d thread_id:%d\n", arg[handlenum].lower, arg[handlenum].uper,Thread_id[handlenum]); LeaveCriticalSection(&gCRITICAL_SECTION_Printf);*/ i += step; handlenum++; } tm.stop(); Sleep(1000); printf("there are %d threads\n", handlenum); printf("the multithreads use %f msc\n", tm.read()); } //the number of 1-1000000 Primenumber void s_finePrime() { timer tm; long result = 0; tm.start(); for (int i = 1; i <= TEST; i++) if (isPrime(i)) result++; tm.stop(); printf("Single thread result is %d\n", result); printf("Single thread use %f msc\n", tm.read()); } int _tmain(int argc, _TCHAR* argv[]) { dispatch(); WaitForMultipleObjects(handlenum, handle_array, true, INFINITE);//不起作用 printf("the multithreads reslut is %d\n", mul_result); CloseHandle(mutex_mul_result_h); DeleteCriticalSection(&gCRITICAL_SECTION_Printf); s_finePrime(); system("pause"); return 0; }
【文件预览】:
findPrime
----ipch()
--------findprime-298bd146()
----findPrime.v12.suo(20KB)
----findPrime.sln(973B)
----findPrime()
--------timer.h(664B)
--------stdafx.h(234B)
--------findPrime.vcxproj(4KB)
--------targetver.h(236B)
--------Debug()
--------stdafx.cpp(215B)
--------ReadMe.txt(1KB)
--------findPrime.cpp(3KB)
--------findPrime.vcxproj.filters(1KB)
----Debug()
--------findPrime.exe(39KB)
--------findPrime.ilk(610KB)
--------findPrime.pdb(731KB)
findmax
----ipch()
--------thread1-da006b67()
--------thread1-1c4dcc5e()
----thread1()
--------timer.h(668B)
--------stdafx.h(273B)
--------targetver.h(236B)
--------thread1.cpp(2KB)
--------Debug()
--------stdafx.cpp(211B)
--------ReadMe.txt(1KB)
--------thread1.vcxproj(4KB)
--------thread1.vcxproj.filters(1KB)
----thread1.sln(967B)
----thread1.v12.suo(18KB)
----Debug()
--------thread1.exe(44KB)
--------thread1.ilk(725KB)
--------thread1.pdb(2.21MB)

网友评论