int main(int argc, const char* argv[]) { threadpool thpool; struct __workq WorkQ; int i; begin: srand(time(NULL)); WorkQ.cnt = 0; PCHECK(pthread_mutex_init(&WorkQ.lock, NULL)); PCHECK(pthread_cond_init(&WorkQ.got_consumer_cond, NULL)); PCHECK(pthread_cond_init(&WorkQ.got_producer_cond, NULL)); TAILQ_INIT(&WorkQ.head); if (!(thpool = thpool_init(PRODUCER_THREAD_NUM + CONSUMER_THREAD_NUM))) { errx(EXIT_FAILURE, "thpool_init() error.\n"); } /* 消费者线程 */ for (i = 0; i < CONSUMER_THREAD_NUM; i++) { thpool_add_work(thpool, consumer, &WorkQ); } sleep(2); /* 生产者线程 */ for (i = 0; i < PRODUCER_THREAD_NUM; i++) { thpool_add_work(thpool, producer, &WorkQ); } end: thpool_wait(thpool); thpool_destroy(thpool); exit(EXIT_SUCCESS); }
char getBestMove(char subBoard[], char superBoard[], char superBoardSpot, char opPlayer, char levels) { long best = -9999999999; char move; char start, end; char j = 0; char lastSuperBoardState; if (superBoardSpot == -1) { //search all spots on the board start = 0; end = SUB_BOARD_SIZE; } else { start = superBoardSpot * 9; end = start + 9; } threadpool thpool = thpool_init(THREADS); //search within the superboard for (char i = start; i < end; i++) { if (isOpenSpot(subBoard, superBoard, i)) { //Take the winning move is available lastSuperBoardState = superBoard[(i - (i % 9)) / 9]; char newSuperBoardSpot = doMove(subBoard, superBoard, opPlayer, i); if (superBoardWon(superBoard) == opPlayer) { undoMove(subBoard, superBoard, i, lastSuperBoardState); return i; } undoMove(subBoard, superBoard, i, lastSuperBoardState); ThreadData data; data.subBoard = copyBoard(subBoard, SUB_BOARD_SIZE); data.superBoard = copyBoard(superBoard, SUPER_BOARD_SIZE); data.opPlayer = opPlayer; data.levels = levels; data.move = i; tData[j] = data; thpool_add_work(thpool, (void*)threadStarter, j++); } } numThreads = j; thpool_wait(thpool); for (char i = 0; i < numThreads; i++) { if (trData[i].score > best) { best = trData[i].score; move = trData[i].move; } } return move; }
int main() { int level = 14; //time stuff struct timeval t1, t2; double elapsedTime1, elapsedTime2; printf("Starting none threaded\n"); // start timer gettimeofday(&t1, NULL); levels(level); // stop timer gettimeofday(&t2, NULL); elapsedTime1 = (t2.tv_sec - t1.tv_sec); threadpool thpool = thpool_init(2); // start timer gettimeofday(&t1, NULL); level--; for (int i = 0; i < level; i++) { thpool_add_work(thpool, (void*)levels, level); } thpool_wait(thpool); // stop timer gettimeofday(&t2, NULL); elapsedTime2 = (t2.tv_sec - t1.tv_sec); printf("done\n"); printf("None threaded seconds: %f\n", elapsedTime1); printf("Threaded seconds: %f\n", elapsedTime2); }
int main(int argc, char *argv[]){ char* p; if (argc != 3){ puts("This testfile needs excactly two arguments"); exit(1); } int num_jobs = strtol(argv[1], &p, 10); int num_threads = strtol(argv[2], &p, 10); threadpool thpool = thpool_init(num_threads); int n; for (n=0; n<num_jobs; n++){ thpool_add_work(thpool, (void*)increment, NULL); } thpool_wait(thpool); printf("%d\n", sum); return 0; }
int main() { FILE *pFile = NULL; int lineCnt = 0; int i = 0; ipList_t* ipList = NULL; /*thread pool 갯수 지정 */ threadpool thpool = thpool_init(THREAD_CNT); /* 읽을 파일의 라인수를 체크 */ lineCnt = lineCount(CHECKLIST_FILE_PATH); printf("lineCnt :[%d] \n",lineCnt); ipList = (ipList_t*)malloc(sizeof(ipList_t)*(lineCnt+10)); memset(ipList,0x00, sizeof(ipList_t)*(lineCnt+10)); pFile = fopen( CHECKLIST_FILE_PATH, "r" ); if( pFile != NULL ) { char strTemp[255]; char *pStr; while( !feof( pFile ) ) { char* ss = NULL; pStr = fgets( strTemp, sizeof(strTemp), pFile ); printf( "pStr:[%s] \n", pStr ); printf( "strTemp:[%s] \n", strTemp ); if(pStr != NULL) { pStr = rtrim(pStr); char* trimStr; //strncpy((ipList+i)->ip, pStr, STR_LEN); //ipList+0 번째가 깨진다. strcpy((ipList+i)->ip, pStr); i++; } } fclose( pFile ); } for(i =0 ;i < lineCnt;i++) { printf("ipList[%d]:[%s] \n",i,(ipList+i)->ip); thpool_add_work(thpool, (void*)pingChk, (ipList+i)->ip); } thpool_wait(thpool); thpool_destroy(thpool); free(ipList); #if 0 pthread_t p_thread[THREAD_CNT]; int thr_id; //쓰레드ID int status; //char p1[] = "thread_1"; // 1번 쓰레드 이름 //char p2[] = "thread_2"; // 2번 쓰레드 이름 //char pM[] = "thread_m"; // 메인 쓰레드 이름 // 파일 읽기 if(fRead() == ERROR) { printf("FRead Error \n"); } Fread(); sleep(2); // 2초 대기후 쓰레드 생성 sys // ① 1번 쓰레드 생성 // 쓰레드 생성시 함수는 t_function // t_function 의 매개변수로 p1 을 넘긴다. thr_id = pthread_create(&p_thread[0], NULL, t_function, (void *)p1); // pthread_create() 으로 성공적으로 쓰레드가 생성되면 0 이 리턴됩니다 if (thr_id < 0) { perror("thread create error : "); exit(0); } // ② 2번 쓰레드 생성 thr_id = pthread_create(&p_thread[1], NULL, t_function, (void *)p2); if (thr_id < 0) { perror("thread create error : "); exit(0); } // ③ main() 함수에서도 쓰레드에서 돌아가고 있는 동일한 함수 실행 //t_function((void *)pM); // 쓰레드 종료를 기다린다. pthread_join(p_thread[0], (void **)&status); pthread_join(p_thread[1], (void **)&status); printf("언제 종료 될까요?\n"); #endif return SUCCESS; }
void cppadcg_thpool_wait() { if(cppadcg_pool != NULL) { thpool_wait(cppadcg_pool); } }
void obf_wait(obf_state_t *s) { thpool_wait(s->thpool); }
int main(void) { _MM_SET_FLUSH_ZERO_MODE(_MM_FLUSH_ZERO_ON); _MM_SET_DENORMALS_ZERO_MODE(_MM_DENORMALS_ZERO_ON); int width = 800, height = 600; GLFWwindow* window; glfwSetErrorCallback(error_callback); if (!glfwInit()) exit(EXIT_FAILURE); window = glfwCreateWindow(width, height, "cpu-voxels", NULL, NULL); if (!window) { glfwTerminate(); return 1; } glfwMakeContextCurrent(window); glfwSwapInterval(0); glfwSetKeyCallback(window, key_callback); glfwSetMouseButtonCallback(window, mouse_button_callback); glfwSetCursorPosCallback(window, mouse_move_callback); glfwSetKeyCallback(window, key_callback); vec3 eye = vec3_create(0.0f, 0.0f, VOXEL_BRICK_SIZE * 4); vec3 center = vec3f(0.0f); vec3 up = vec3_create(0.0, 1.0, 0.0 ); orbit_camera_init(eye, center, up); // TODO: handle resize int dw, dh; glfwGetFramebufferSize(window, &dw, &dh); int stride = 3; int total = dw*dh*stride; uint8_t *data = malloc(total); vec3 ro; //, rd; mat4 m4inverted, view; mat4 projection; mat4_perspective( projection, M_PI/4.0, (float)width/(float)height, 0.1, 1000.0 ); GLuint texture[1]; #ifdef ENABLE_THREADS screen_area areas[TOTAL_THREADS]; threadpool thpool = thpool_init(TOTAL_THREADS); #else screen_area areas[1]; #endif glGenTextures(1, texture); float start = glfwGetTime(); int fps = 0; voxel_brick my_first_brick = voxel_brick_create(); // TODO: make this work when the brick lb corner is not oriented at 0,0,0 voxel_brick_position(my_first_brick, vec3f(0.0f)); voxel_brick_fill(my_first_brick, &brick_fill); while (!glfwWindowShouldClose(window)) { if (glfwGetKey(window, GLFW_KEY_LEFT) == GLFW_PRESS) { orbit_camera_rotate(0, 0, -.1, 0); } if (glfwGetKey(window, GLFW_KEY_RIGHT) == GLFW_PRESS) { orbit_camera_rotate(0, 0, .1, 0); } if (glfwGetKey(window, GLFW_KEY_UP) == GLFW_PRESS) { orbit_camera_rotate(0, 0, 0, .1); } if (glfwGetKey(window, GLFW_KEY_DOWN) == GLFW_PRESS) { orbit_camera_rotate(0, 0, 0, -.1); } glfwGetFramebufferSize(window, &width, &height); float now = glfwGetTime(); if (now - start > 1) { unsigned long long total_rays = (fps * width * height); printf("fps: %i (%f Mrays/s)@%ix%i - %i threads\n", fps, total_rays/1000000.0, width, height, TOTAL_THREADS); start = now; fps = 0; } fps++; orbit_camera_view(view); ro = mat4_get_eye(view); mat4_mul(m4inverted, projection, view); mat4_invert(m4inverted, m4inverted); // compute 3 points so that we can interpolate instead of unprojecting // on every point vec3 rda, rdb, planeYPosition, dcol, drow; vec3 t0 = vec3_create(0, 0, 0), tx = vec3_create(1, 0, 0), ty = vec3_create(0, 1, 0); vec4 viewport = { 0, 0, width, height }; rda = orbit_camera_unproject(t0, viewport, m4inverted); rdb = orbit_camera_unproject(tx, viewport, m4inverted); planeYPosition = orbit_camera_unproject(ty, viewport, m4inverted); dcol = planeYPosition - rda; drow = rdb - rda; int i=0, bh = height; #ifdef ENABLE_THREADS bh = (height/TOTAL_THREADS); for (i; i<TOTAL_THREADS; i++) { #endif areas[i].dcol = dcol; areas[i].drow = drow; areas[i].pos = planeYPosition; areas[i].ro = ro; areas[i].x = 0; areas[i].y = i*bh; areas[i].width = width; areas[i].height = areas[i].y + (int)(bh); areas[i].screen_height = (int)(height); areas[i].stride = stride; areas[i].data = data; areas[i].render_id = i; areas[i].brick = my_first_brick; #ifdef ENABLE_THREADS thpool_add_work(thpool, (void *)render_screen_area, (void *)(&areas[i])); } thpool_wait(thpool); #else render_screen_area((void *)(&areas[i])); #endif #ifdef RENDER glViewport(0, 0, width, height); glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glDisable(GL_CULL_FACE); glMatrixMode(GL_PROJECTION); glLoadIdentity(); glMatrixMode(GL_MODELVIEW); glLoadIdentity(); glMatrixMode(GL_TEXTURE); glLoadIdentity(); glScalef(1.0f, -1.0f, 1.0f); glEnable(GL_TEXTURE_2D); glBindTexture(GL_TEXTURE_2D, texture[0]); glTexImage2D(GL_TEXTURE_2D, 0, 3, width, height, 0, GL_RGB, GL_UNSIGNED_BYTE, data); glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR); glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR); glBegin(GL_QUADS); glTexCoord2f(0.0f, 0.0f); glVertex2f( -1, -1); glTexCoord2f(1.0f, 0.0f); glVertex2f( 1, -1); glTexCoord2f(1.0f, 1.0f); glVertex2f( 1, 1); glTexCoord2f(0.0f, 1.0f); glVertex2f( -1, 1); glEnd(); glfwSwapBuffers(window); glDeleteTextures(1, &texture[0]); #endif glfwPollEvents(); } glfwDestroyWindow(window); glfwTerminate(); exit(EXIT_SUCCESS); }