int testThreadPool() { #ifdef __LINUX__ cout << "this is linux" << endl; #endif #ifdef __WINDOWS__ cout << "this is windows" << endl; #endif ThreadPool pool; pool.Run(8); pool.AddTask(new MyTask()); MySignal si; for (int i = 0; i < 8; ++i) //while (running) { pool.AddTask(new MyTask()); cout << "add task ...." << endl; //Timer::Sleep(500); } int sec = 0; while (running) { cout << "wait sec " << sec++ << endl; Timer::Sleep(1000); } return 0; }
void __declspec(dllexport) GetSleep(LPVOID poolarg, LPVOID vectargs) { ThreadPool* pool =(ThreadPool*) poolarg; std::vector<std::wstring>* vect = (std::vector<std::wstring>*) vectargs; int count = _wtoi(vect->at(0).data()); for (int i = 0; i< count; i++) pool->AddTask(WorkerGotDream,NULL); }
void __declspec(dllexport) Vasya(LPVOID poolarg, LPVOID vectargs) { ThreadPool* pool =(ThreadPool*) poolarg; std::vector<std::wstring>* vect = (std::vector<std::wstring>*) vectargs; int* hue =(int*) malloc(sizeof(int)); *hue = _wtoi(vect->at(0).data()); for (int i=0; i< 2; i++) { pool->AddTask(Hehey,hue); } }
int main() { ThreadPool* pTheadPool = ThreadPool::GetInstace(); pTheadPool->Init(5); cout<<"main"; for(int i=0; i<100; i++) { CTask* pTask = new CTask(i); pTheadPool->AddTask(pTask); } cout<<"Task added\n"; sleep(30); //可以设置线程池结束条件,需要结束的时候释放内存 pTheadPool->Destory(); delete pTheadPool; return 0; }
DWORD WINAPI ControlFunction(CONST LPVOID lpParam) { int count; puts("Enter number of thread"); scanf_s("%d", &count); if (count < 0) { count = 4; } ThreadPool* threadPool; threadPool = new ThreadPool(count); puts("Enter number of task: 1, 2, 3"); puts("Press 'Enter' for exit"); int n = 0; while (n != 13) { n = _getch(); Task* currentTask; currentTask = (Task*)new CustomTask(); switch (n) { case '1': { currentTask = (Task*)new CustomTask(); break; } case '2': { currentTask = (Task*)new CustomTask(); break; } case '3': { currentTask = (Task*)new CustomTask(); break; } } threadPool->AddTask(currentTask); } delete threadPool; return 0; }
void RayTrace() { const uint32_t kSamples = g_width*g_height; ThreadPool threadPool; const uint32_t kTileSize = 16; const uint32_t kNumJobs = ceil(g_width / float(kTileSize)) * ceil(g_height / float(kTileSize)); vector<RayJob> jobs(kNumJobs); // create camera const Camera camera(TransformMatrix(g_camDir, g_camPos), 45.0f, 0.1f, 10000.0f, g_width, g_height); uint32_t jobIndex = 0; double startTime = GetSeconds(); // create thread jobs for (uint32_t y=0; y < g_height; y += kTileSize) { uint32_t top = y; uint32_t bottom = min(top+kTileSize, g_height); for (uint32_t x=0; x < g_width; x += kTileSize) { RayJob& job = jobs[jobIndex++]; job.m_rect = Rect(x, min(x+kTileSize, g_width), top, bottom); job.m_camera = camera; job.m_scene = g_scene; job.m_samplesPerPixel = 1; job.m_output = &g_pixels[0]; job.m_outputPitch = g_width; threadPool.AddTask(RayTraceThreadFunc, &job); } } threadPool.Run(g_numWorkers); threadPool.Wait(); // print out trace time every 10 frames double endTime = GetSeconds(); /* cout << "Nodes checked: " << g_nodesChecked << endl; cout << "Tris checked: " << g_trisChecked << endl; g_trisChecked = 0; g_nodesChecked = 0; */ ++g_iterations; Colour* presentMem = g_pixels; if (g_mode == ePathTrace) { float s = g_exposure / g_iterations; for (uint32_t i=0; i < g_width*g_height; ++i) { g_filtered[i] = LinearToSrgb(g_pixels[i] * s); } presentMem = g_filtered; } static uint32_t s_counter=0; if (s_counter % 10) { cout << "Trace took: " << (endTime-startTime)*1000.0f << "ms" << " rays/s: " << g_width*g_height/(endTime-startTime) << endl; } ++s_counter; glDisable(GL_BLEND); glDisable(GL_LIGHTING); glDisable(GL_DEPTH_TEST); glDisable(GL_CULL_FACE); glPixelZoom(float(g_windowWidth)/g_width, float(g_windowHeight)/g_height); glDrawPixels(g_width,g_height,GL_RGBA,GL_FLOAT, presentMem); }
int handle_read_event(client* c) { while(1){ switch(c->state){ case CMD_CHECK: c->cmd_end = (char*)memchr(c->readbuf, '\n', c->rpos); if(!c->cmd_end) //not a complete cmd return 0; //received a complete cmd if(*(c->cmd_end-1) != '\r') //bad format return -1; c->state = CMD_PARSE; break; case CMD_PARSE: { //parse command int npart = 0; *(c->cmd_end-1) = '\0'; //set for strtok for (char* p=c->readbuf; ; p = NULL) { char* token = strtok(p, " "); if (token == NULL) break; c->cmd_part[npart++] = token; } if(strcmp(c->cmd_part[0], "get") == 0) //get command c->state = GET_CMD; else if(strcmp(c->cmd_part[0], "del") == 0) c->state = DEL_CMD; else if(strcmp(c->cmd_part[0], "add") == 0) c->state = ADD_CMD; else if(strcmp(c->cmd_part[0], "set") == 0) c->state = SET_CMD; else return -2; //bad command break; } case GET_CMD: case DEL_CMD: { Task* p; if(c->state == GET_CMD) p = new GetTask(c); else p = new DelTask(c); deregister_event_helper(c, EPOLLIN); //stop receiving data from client to protect member variables in this client thread_pool.AddTask(p); return 1; break; } case ADD_CMD: case SET_CMD: { int bytes = atoi(c->cmd_part[2]); //bytes of data block, to do: what if byte is 0 ? if(c->readbuf+c->rpos-(c->cmd_end+1) < bytes + 2) //data not received completelly, +2 for '\r\n' return 0; Task* p; if(c->state == ADD_CMD) p = new AddTask(c); else p = new SetTask(c); deregister_event_helper(c, EPOLLIN); thread_pool.AddTask(p); return 1; break; } } } }
void __declspec(dllexport) Copy(LPVOID poolarg, LPVOID vectargs) { ThreadPool* pool =(ThreadPool*) poolarg; std::vector<std::wstring>* vect = (std::vector<std::wstring>*) vectargs; if (vect->size() != 2) { printf("Copy:Unacceptable params!\n)"); return; } WIN32_FIND_DATAW wfd; std::wstring dirFrom = std::wstring(vect->at(0)); std::wstring dirTo= std::wstring(vect->at(1)); std::wstring folderName; if (dirFrom.back() == L'/') { dirFrom.pop_back(); } folderName = dirFrom.substr(dirFrom.find_last_of(L"/")+ 1); NormalizeDirPath(&dirFrom); NormalizeDirPath(&dirTo); dirTo.pop_back();// * не нужна dirTo += folderName; dirTo+= L"/"; HANDLE hFind = FindFirstFileW(dirFrom.data(), &wfd); std::wstring fullFileName; std::wstring fullNewFileName; fullNewFileName.clear(); fullNewFileName+= dirTo; CreateDirectory(fullNewFileName.data(),NULL); std::vector<std::wstring> vasya; std::wstring* newPathTo; std::wstring* newPathFrom; std::vector<std::wstring>* newVectParams; int vas; if (INVALID_HANDLE_VALUE != hFind) { do { if (!wcscmp(wfd.cFileName,L".") || !wcscmp(wfd.cFileName,L"..")) continue; if ((vas = wfd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) != 0) { //если директория WCHAR newpath[_MAX_PATH]; newpath[0] = L'\0'; wcscat(newpath,dirTo.data()); newPathTo = new std::wstring(newpath); wcscpy(newpath,L""); wcscat(newpath,dirFrom.data()); newpath[wcslen(newpath)-1] = L'\0'; wcscat(newpath,wfd.cFileName); newPathFrom = new std::wstring(newpath); newVectParams = new std::vector<std::wstring>(); newVectParams->push_back(*newPathFrom); newVectParams->push_back(*newPathTo); pool->AddTask(Copy,newVectParams); } else { vasya.push_back(wfd.cFileName);// список файлов на копирование } } while (NULL != FindNextFileW(hFind, &wfd)); FindClose(hFind); for (int i = 0; i< vasya.size(); i++) { fullFileName.clear(); fullFileName+= dirFrom; fullFileName.pop_back(); //удаляем * fullFileName+=vasya[i]; fullNewFileName.clear(); fullNewFileName+= dirTo; fullNewFileName+=vasya[i]; int err = CopyFile(fullFileName.data(),fullNewFileName.data(),FALSE); if (err == 0) { std::wcout << L"Failed to copy "<<fullFileName.data()<<L"\n"<<std::endl; } } } delete vect; }
void FolderWalker(LPVOID poolarg, LPVOID vectargs) { ThreadPool* pool =(ThreadPool*) poolarg; std::vector<LPVOID> vect = *(std::vector<LPVOID>*) vectargs; WIN32_FIND_DATAW wfd; std::wstring* dir = (std::wstring*) vect.at(0); DefendedInt64* pdint64 = (DefendedInt64* )vect.at(1); std::vector<HANDLE>* allevents =(std::vector<HANDLE>*)vect.at(2); //!!!!!!!!!!!!!!!!!!!!!!!!!!!!! std::vector<LPVOID>* entervect = new std::vector<LPVOID>(); entervect->push_back(dir); entervect->push_back(pdint64); HANDLE myEvent = ::CreateEvent(NULL,TRUE,//без FALSE,NULL); entervect->push_back(myEvent); allevents->push_back(myEvent); //добавляем ивент этой папки в таблицу ивентов pool->AddTask(&FolderSize,(LPVOID)entervect); if (wcsncmp(&dir->back(),L"/",1)) { dir->push_back(L'//'); dir->push_back(L'*'); } else dir->push_back(L'*'); HANDLE hFind = FindFirstFileW(dir->data(), &wfd); std::vector<LPVOID>* newvect; if (INVALID_HANDLE_VALUE != hFind) { do { if (!wcscmp(wfd.cFileName,L".") || !wcscmp(wfd.cFileName,L"..")) continue; int vas; if ( (vas = wfd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) != 0) { //если директория WCHAR NewPath[ _MAX_PATH]; newvect = new std::vector<LPVOID>(); const wchar_t* dirdata = dir->data(); std::wstring* newpathstr = new std::wstring(dirdata); newpathstr->pop_back(); dirdata = newpathstr->data();///////////////////////////////////// wcscpy(NewPath,dirdata); wcscat( NewPath, wfd.cFileName); delete newpathstr; newpathstr = new std::wstring(NewPath); newvect->push_back(newpathstr); newvect->push_back(vect[1]); newvect->push_back(vect[2]); int currentrecursiondeep = *(int*)vect[3]; currentrecursiondeep++; newvect->push_back(¤trecursiondeep); FolderWalker(poolarg,newvect); } } while (NULL != FindNextFileW(hFind, &wfd)); FindClose(hFind); if (*(int*)vect[3] == 1)//корневая папка { HANDLE vas; for (int i = 0; i< allevents->size(); i++) { vas = allevents->at(i); ::WaitForSingleObject(vas,INFINITE); } Sleep(100); } } }