// 进程退出前清理资源 static void end(void) { // 销毁线程池 acl_pthread_pool_destroy(__thr_pool); // 打印所有连接池集群的存活状态 printf("\r\n"); check_all_connections(); printf("\r\n>>> STOPPING check thread now\r\n"); #if 0 int i = 0; while (i++ < 10) { sleep(1); printf("----------- sleep %d seconds -----------\r\n", i); } #endif // 停止后台检测线程 acl::connect_monitor* monitor = __conn_manager->stop_monitor(true); // 删除检测器对象 delete monitor; // 销毁连接池 delete __conn_manager; }
static void thread_pool_tls(int nthread, int nalloc) { const char *myname = "thread_pool_tls"; acl_pthread_pool_t *thr_pool; void *ptr; time_t begin = time(NULL); int i; /* 创建线程池 */ thr_pool = acl_thread_pool_create(nthread, 0); for (i = 0; i < nalloc; i++) { /* 主线程分配内存 */ if (1) ptr = acl_mymalloc(64); else ptr = 0; /* 向线程池中添加任务, 将新内存传递给线程池处理 */ /* * ACL_METER_TIME("--31--"); */ acl_pthread_pool_add(thr_pool, run_thread, ptr); if (i % 100000 == 0) printf(">>>%s: i=%d\n", myname, i); /* * ACL_METER_TIME("--32--"); */ } /* 释放线程池 */ acl_pthread_pool_destroy(thr_pool); printf(">>>%s: time cost: %ld\n", myname, (long) (time(NULL) - begin)); }
// 进程退出前清理资源 static void end(void) { // 销毁线程池 acl_pthread_pool_destroy(__thr_pool); // 打印所有连接池集群的存活状态 printf("\r\n"); check_all_connections(); printf("\r\n>>> STOPPING check thread now\r\n"); // 停止后台检测线程 __conn_manager->stop_monitor(true); // 销毁连接池 delete __conn_manager; }
void acl_master_vars_end(void) { if (ACL_MASTER_STAT_STREAM) { acl_vstream_close(ACL_MASTER_STAT_STREAM); ACL_MASTER_STAT_STREAM = NULL; } if (ACL_MASTER_FLOW_READ_STREAM) { acl_vstream_close(ACL_MASTER_FLOW_READ_STREAM); ACL_MASTER_FLOW_READ_STREAM = NULL; } if (ACL_MASTER_FLOW_WRITE_STREAM) { acl_vstream_close(ACL_MASTER_FLOW_WRITE_STREAM); ACL_MASTER_FLOW_WRITE_STREAM = NULL; } if (acl_var_master_thread_pool) { acl_pthread_pool_destroy(acl_var_master_thread_pool); acl_var_master_thread_pool = NULL; } }
static void test_thread_pool(void) { acl_pthread_pool_t *thr_pool; ACL_VSTREAM *fp = acl_vstream_fopen("test.log", O_WRONLY | O_CREAT, 0600, 4096); int i; acl_pthread_mutex_init(&__mutex, NULL); thr_pool = acl_thread_pool_create(10, 10); for (i = 0; i < 1000000; i++) { RUN_CTX *ctx = (RUN_CTX*) acl_mymalloc(sizeof(RUN_CTX)); ctx->fp = fp; ctx->i = i; acl_pthread_pool_add(thr_pool, run_thread, ctx); } acl_pthread_pool_destroy(thr_pool); acl_pthread_mutex_destroy(&__mutex); acl_vstream_close(fp); }
int main(int argc, char* argv[]) { #ifdef WIN32 acl::acl_cpp_init(); #endif logger_open("fs_benchmark.log", "fs_benchmark"); int ch; int nthreads = 2; #ifdef WIN32 snprintf(__path, sizeof(__path), "var/%d", (int) _getpid()); #else snprintf(__path, sizeof(__path), "var/%d", (int) getpid()); #endif while ((ch = getopt(argc, argv, "hn:c:l:kp:P:")) > 0) { switch (ch) { case 'h': usage(argv[0]); return 0; case 'n': nthreads = atoi(optarg); break; case 'c': __nfiles = atoi(optarg); break; case 'l': __length = atoi(optarg); break; case 'k': __kernel_event = true; break; case 'p': #ifdef WIN32 snprintf(__path, sizeof(__path), "%s/%d", optarg, (int) _getpid()); #else snprintf(__path, sizeof(__path), "%s/%d", optarg, (int) getpid()); #endif break; case 'P': __parallel = atoi(optarg); if (__parallel <= 0) __parallel = 1; break; default: break; } } for (size_t i = 0; i < sizeof(__data); i++) { __data[i] = 'X'; } acl_make_dirs(__path, 0700); // 创建线程池句柄 acl_pthread_pool_t* tp = acl_thread_pool_create(nthreads, 0); // 添加线程处理任务 for (int i = 0; i < nthreads; i++) acl_pthread_pool_add(tp, thread_main, NULL); // 销毁线程池 acl_pthread_pool_destroy(tp); logger_close(); #ifdef WIN32 printf("enter any key to exit\r\n"); getchar(); #endif return 0; }
bool master_threads::run_alone(const char* addrs, const char* path /* = NULL */, unsigned int count /* = 1 */, int threads_count /* = 1 */) { // 每个进程只能有一个实例在运行 acl_assert(has_called == false); has_called = true; daemon_mode_ = false; acl_assert(addrs && *addrs); __count_limit = count; #ifdef WIN32 acl_init(); #endif std::vector<ACL_VSTREAM*> sstreams; ACL_EVENT* eventp = acl_event_new_select_thr(1, 0); set_event(eventp); // 设置基类的事件句柄 ACL_ARGV* tokens = acl_argv_split(addrs, ";,| \t"); ACL_ITER iter; acl_foreach(iter, tokens) { const char* addr = (const char*) iter.data; ACL_VSTREAM* sstream = acl_vstream_listen(addr, 128); if (sstream == NULL) { logger_error("listen %s error(%s)", addr, acl_last_serror()); acl_argv_free(tokens); close_sstreams(eventp, sstreams); acl_event_free(eventp); return false; } acl_event_enable_listen(eventp, sstream, 0, listen_callback, sstream); sstreams.push_back(sstream); } acl_argv_free(tokens); // 初始化配置参数 conf_.load(path); service_pre_jail(NULL); service_init(NULL); if (threads_count > 1) { __thread_pool = acl_thread_pool_create(threads_count, 120); acl_pthread_pool_atinit(__thread_pool, thread_begin, NULL); acl_pthread_pool_atfree(__thread_pool, thread_finish, NULL); } else thread_init(NULL); while (!__stop) acl_event_loop(eventp); if (__thread_pool) acl_pthread_pool_destroy(__thread_pool); else thread_exit(NULL); service_exit(NULL); // 必须在调用 acl_event_free 前调用 close_sstreams,因为在关闭 // 网络流对象时依然有对 ACL_EVENT 引擎的使用 close_sstreams(eventp, sstreams); acl_event_free(eventp); eventp = NULL; return true; }
int main(int argc, char *argv[]) { char peer[64], local[64]; int ch, count = 1, dlen = 100, inter = 1000, nthreads = 1, quit = 0; int need_read = 0; acl_lib_init(); acl_msg_stdout_enable(1); snprintf(peer, sizeof(peer), "127.0.0.1:8888"); snprintf(local, sizeof(local), "127.0.0.1:0"); while ((ch = getopt(argc, argv, "hl:s:n:N:i:t:rq")) > 0) { switch (ch) { case 'h': usage(argv[0]); return 0; case 'l': snprintf(local, sizeof(local), "%s", optarg); break; case 's': snprintf(peer, sizeof(peer), "%s", optarg); break; case 'n': count = atoi(optarg); break; case 'N': dlen = atoi(optarg); break; case 'i': inter = atoi(optarg); break; case 't': nthreads = atoi(optarg); break; case 'r': need_read = 1; break; case 'q': quit = 1; break; default: break; } } if (peer[0] == 0 || local[0] == 0) { usage(argv[0]); return 1; } if (nthreads > 1) { int i; acl_pthread_pool_t *threads = acl_thread_pool_create(nthreads, 120); for (i = 0; i < nthreads; i++) { THREAD_CTX *ctx = (THREAD_CTX*) acl_mymalloc(sizeof(THREAD_CTX)); snprintf(ctx->local, sizeof(ctx->local), "%s", local); snprintf(ctx->peer, sizeof(ctx->peer), "%s", peer); ctx->count = count; ctx->dlen = dlen; ctx->inter = inter; ctx->need_read = need_read; ctx->quit = quit; acl_pthread_pool_add(threads, thread_run, ctx); } acl_pthread_pool_destroy(threads); } else run(local, peer, count, dlen, inter, need_read, quit); printf("\r\nlocal: %s, peer: %s, count: %d, dlen: %d, inter: %d\r\n", local, peer, count, dlen, inter); acl_lib_end(); return 0; }
int main(int argc, char* argv[]) { if (0) test1(); int ch, num = 10, nthread = 1; char addr[256]; void (*func)(const char* addr, int id, int num) = NULL; snprintf(addr, sizeof(addr), "127.0.0.1:11211"); while ((ch = getopt(argc, argv, "hl:gsdmn:t:T:")) > 0) { switch (ch) { case 'h': usage(argv[0]); return (0); case 'l': snprintf(addr, sizeof(addr), "%s", optarg); break; case 'g': func = get; break; case 's': func = set; break; case 'm': func = mod; break; case 'd': func = del; break; case 'n': num = atoi(optarg); break; case 't': __timeout = atoi(optarg); break; case 'T': nthread = atoi(optarg); break; default: break; } } printf("server: %s\n", addr); if (func == NULL) usage(argv[0]); else { if (nthread <= 1) func(addr, 0, num); else { acl_pthread_pool_t* pool = acl_thread_pool_create(nthread, 100); for (int i = 0; i < nthread; i++) { CTX* ctx = (CTX*) acl_mycalloc(1, sizeof(CTX)); ctx->fn = func; ctx->addr = addr; ctx->num = num; ctx->id = i; acl_pthread_pool_add(pool, thread_main, ctx); } acl_pthread_pool_destroy(pool); } } return (0); }