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); }
int main(void) { struct thpool *pool = thpool_init(10, 20); thpool_add_job(pool, work, "1"); thpool_add_job(pool, work, "2"); thpool_add_job(pool, work, "3"); thpool_add_job(pool, work, "4"); thpool_add_job(pool, work, "5"); thpool_add_job(pool, work, "6"); thpool_add_job(pool, work, "7"); thpool_add_job(pool, work, "8"); thpool_add_job(pool, work, "9"); thpool_add_job(pool, work, "10"); thpool_add_job(pool, work, "11"); thpool_add_job(pool, work, "12"); thpool_add_job(pool, work, "13"); thpool_add_job(pool, work, "14"); thpool_add_job(pool, work, "15"); thpool_add_job(pool, work, "16"); thpool_add_job(pool, work, "17"); thpool_add_job(pool, work, "18"); thpool_add_job(pool, work, "19"); thpool_add_job(pool, work, "20"); thpool_add_job(pool, work, "21"); thpool_add_job(pool, work, "22"); thpool_add_job(pool, work, "23"); thpool_add_job(pool, work, "24"); thpool_add_job(pool, work, "25"); thpool_add_job(pool, work, "26"); thpool_add_job(pool, work, "27"); thpool_add_job(pool, work, "28"); thpool_add_job(pool, work, "29"); thpool_add_job(pool, work, "30"); thpool_add_job(pool, work, "31"); thpool_add_job(pool, work, "32"); thpool_add_job(pool, work, "33"); thpool_add_job(pool, work, "34"); thpool_add_job(pool, work, "35"); thpool_add_job(pool, work, "36"); thpool_add_job(pool, work, "37"); thpool_add_job(pool, work, "38"); thpool_add_job(pool, work, "39"); thpool_add_job(pool, work, "40"); sleep(5); thpool_destroy(pool); return 0; }
int main(int argc, char *argv[]){ char* p; if (argc != 2){ puts("This testfile needs excactly one arguments"); exit(1); } int num_threads = strtol(argv[1], &p, 10); threadpool thpool = thpool_init(num_threads); thpool_destroy(thpool); sleep(1); // Sometimes main exits before thpool_destroy finished 100% return 0; }
int main (int argc, char* argv[]) { thpool_init (100); int *arg = (int *) malloc (sizeof (int) * 1000); int i; for (i=0; i<1000; i++) { arg[i] = i; thpool_add_worker (myprocess, &arg[i]); } sleep(6); thpool_destroy(); free(arg); return 0; }
int main(){ puts("Making threadpool with 4 threads"); threadpool thpool = thpool_init(4); puts("Adding 40 tasks to threadpool"); int i; for (i=0; i<20; i++){ thpool_add_work(thpool, (void*)task1, NULL); thpool_add_work(thpool, (void*)task2, NULL); }; puts("Killing threadpool"); thpool_destroy(thpool); return 0; }
void obf_clear(obf_state_t *s) { if (s) { s->vtable->sk->clear(s->mmap); free(s->mmap); aes_randclear(s->rand); for (uint64_t i = 0; i < s->nthreads; ++i) { aes_randclear(s->rands[i]); } free(s->rands); free(s->randomizer); free(s->inverse); thpool_destroy(s->thpool); } free(s); }
int main() { printf("~~~~~~~~~~~"); thpool_t* thpool; int i; thpool = thpool_init(5); puts("Adding 20 tasks to threadpool"); //int a=54; for (i=0; i<20; i++){ thpool_add_work(thpool, (void*)task1, NULL); thpool_add_work(thpool, (void*)task2, NULL); }; puts("Will kill threadpool"); thpool_destroy(thpool); }
int main(){ intptr_t i; thpool_t* threadpool; /* make a new thread pool structure */ threadpool=thpool_init(4); /* initialise it to 4 number of threads */ puts("Adding 20 tasks to threadpool"); for (i=0; i<10; i++){ thpool_add_work(threadpool, i, (void *)task1, NULL); thpool_add_work(threadpool, i, (void *)task2, (void *)i); }; puts("Will kill threadpool"); thpool_destroy(threadpool); return 0; }
int main(){ nonzero_stack(); nonzero_heap(); puts("Making threadpool with 4 threads"); threadpool thpool = thpool_init(4); puts("Adding 20 tasks to threadpool"); int i; for (i=0; i<20; i++){ thpool_add_work(thpool, (void*)task, NULL); }; puts("Killing threadpool"); thpool_destroy(thpool); 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_shutdown() { if(cppadcg_pool != NULL) { thpool_destroy(cppadcg_pool); cppadcg_pool = NULL; } }
// Shutdown handler, which catches signals and performs shutdown routines to cleanly terminate the server void shutdown_handler() { // Cancel the network handling thread, stopping all incoming connections pthread_cancel(net_thread); // If in daemon mode, unlock, close, and remove the lockfile if(daemonized == 1) { // Attempt to unlock lockfile if(lockf(pidfile, F_ULOCK, 0) == -1) { fprintf(stderr, "%s: %s failed to unlock lockfile\n", SERVER_NAME, ERROR_MSG); exit(-1); } // Attempt to close lockfile if(close(pidfile) == -1) { fprintf(stderr, "%s: %s failed to close daemon lockfile\n", SERVER_NAME, ERROR_MSG); exit(-1); } // Attempt to remove lockfile if(remove(lock_location) == -1) { fprintf(stderr, "%s: %s could not remove lockfile %s\n", SERVER_NAME, ERROR_MSG, lock_location); exit(-1); } } // Print newline to clean up output fprintf(stdout, "\n"); // Close SQLite database if(sqlite3_close(db) != SQLITE_OK) { // On failure, print error to console and exit fprintf(stderr, "%s: %s sqlite: failed to close database\n", SERVER_NAME, ERROR_MSG); exit(-1); } // Attempt to shutdown the local socket if(shutdown(loc_fd, 2) == -1) { // Print error and exit if socket fails to shutdown fprintf(stderr, "%s: %s failed to shutdown local socket\n", SERVER_NAME, ERROR_MSG); exit(-1); } // Attempt to close local socket to end program if(close(loc_fd) == -1) { // Print error and exit if socket fails to close fprintf(stderr, "%s: %s failed to close local socket\n", SERVER_NAME, ERROR_MSG); exit(-1); } // Destroy created threadpool. If 0 clients are connected, rejoin the threads. Else, cancel the threads (force destroy). if(client_count(0) == 0) thpool_destroy(threadpool, 0); else thpool_destroy(threadpool, 1); // Print final termination message fprintf(stdout, "%s: %s kicked %d client(s), server terminated\n", SERVER_NAME, OK_MSG, client_count(0)); // Exit, returning success exit(0); }