/* Initialise thread pool */ struct thpool_* thpool_init(int num_threads) { threads_on_hold = 0; threads_keepalive = 1; if (num_threads < 0) { num_threads = 0; } /* Make new thread pool */ thpool_* thpool_p; thpool_p = (struct thpool_*)malloc(sizeof(struct thpool_)); if (thpool_p == NULL) { err("thpool_init(): Could not allocate memory for thread pool\n"); return NULL; } thpool_p->num_threads_alive = 0; thpool_p->num_threads_working = 0; /* Initialise the job queue */ if (jobqueue_init(&thpool_p->jobqueue) == -1) { err("thpool_init(): Could not allocate memory for job queue\n"); free(thpool_p); return NULL; } /* Make threads in pool */ thpool_p->threads = (struct thread**)malloc(num_threads * sizeof( struct thread*)); if (thpool_p->threads == NULL) { err("thpool_init(): Could not allocate memory for threads\n"); jobqueue_destroy(&thpool_p->jobqueue); free(thpool_p); return NULL; } pthread_mutex_init(&(thpool_p->thcount_lock), NULL); pthread_cond_init(&thpool_p->threads_all_idle, NULL); /* Thread init */ int n; for (n = 0; n < num_threads; n++) { thread_init(thpool_p, &thpool_p->threads[n], n); #if THPOOL_DEBUG printf("THPOOL_DEBUG: Created thread %d in pool \n", n); #endif } /* Wait for threads to initialize */ while (thpool_p->num_threads_alive != num_threads) {} return thpool_p; }
/** * @brief Initialize threadpool * * Initializes a threadpool. This function will not return untill all * threads have initialized successfully. * * @example * * .. * threadpool thpool; //First we declare a threadpool * thpool = thpool_init(4); //then we initialize it to 4 threads * .. * * @param num_threads number of threads to be created in the threadpool * @return threadpool created threadpool on success, * NULL on error */ struct ThPool* thpool_init(int num_threads) { if (num_threads < 0) { num_threads = 0; } if(cppadcg_pool_verbose) { fprintf(stdout, "thpool_init(): Thread pool created with %i threads\n", num_threads); } if(num_threads == 0) { cppadcg_pool_disabled = 1; // true return NULL; } /* Make new thread pool */ ThPool* thpool; thpool = (ThPool*) malloc(sizeof(ThPool)); if (thpool == NULL) { fprintf(stderr, "thpool_init(): Could not allocate memory for thread pool\n"); return NULL; } thpool->num_threads = num_threads; thpool->num_threads_alive = 0; thpool->num_threads_working = 0; thpool->threads_keepalive = 1; /* Initialize the job queue */ if (jobqueue_init(thpool) == -1) { fprintf(stderr, "thpool_init(): Could not allocate memory for job queue\n"); free(thpool); return NULL; } /* Make threads in pool */ thpool->threads = (Thread**) malloc(num_threads * sizeof(Thread*)); if (thpool->threads == NULL) { fprintf(stderr, "thpool_init(): Could not allocate memory for threads\n"); jobqueue_destroy(thpool); free(thpool->jobqueue); free(thpool); return NULL; } pthread_mutex_init(&(thpool->thcount_lock), NULL); pthread_cond_init(&thpool->threads_all_idle, NULL); /* Thread init */ int n; for (n = 0; n < num_threads; n++) { thread_init(thpool, &thpool->threads[n], n); } /* Wait for threads to initialize */ while (thpool->num_threads_alive != num_threads) {} return thpool; }
void register_job(int job_type,JOB_FUNCTION function,JOB_JUDGE judge,int call_type){ if(job_type<0 || job_type>=JOB_NUMBER){ syslog(project_params.syslog_level,"job_type(%d) is error\n",job_type); return; } job_queue[job_type].job_type = job_type; jobqueue_init(&(job_queue[job_type]));//init work queue pthread_cond_init(&(job_cond[job_type]),NULL);// init job cond job_functions[job_type] = function; // init job_funtion job_call_type[job_type] = call_type; // init job_call_type job_judges[job_type] = judge; // init judge function pthread_mutex_init(&(job_mutex_for_cond[job_type]),NULL); // init mutex for cond pthread_mutex_init(&(job_mutex_for_queue[job_type]),NULL);// init mutex for work queue job_registed[job_type] = 1; //flag registered 1 represent true, 0 represent false }
/* Initialise thread pool */ struct thpool_* thpool_init(int num_threads){ threads_on_hold = 0; threads_keepalive = 1; if ( num_threads < 0){ num_threads = 0; } /* Make new thread pool */ thpool_* thpool_p; thpool_p = (struct thpool_*)malloc(sizeof(struct thpool_)); if (thpool_p==NULL){ fprintf(stderr, "thpool_init(): Could not allocate memory for thread pool\n"); exit(1); } thpool_p->num_threads_alive = 0; thpool_p->num_threads_working = 0; /* Initialise the job queue */ if (jobqueue_init(thpool_p)==-1){ fprintf(stderr, "thpool_init(): Could not allocate memory for job queue\n"); exit(1); } /* Make threads in pool */ thpool_p->threads = (struct thread**)malloc(num_threads * sizeof(struct thread)); if (thpool_p->threads==NULL){ fprintf(stderr, "thpool_init(): Could not allocate memory for threads\n"); exit(1); } /* Thread init */ int n; for (n=0; n<num_threads; n++){ thread_init(thpool_p, &thpool_p->threads[n], n); printf("Created thread %d in pool \n", n); } /* Wait for threads to initialize */ while (thpool_p->num_threads_alive != num_threads) {} return thpool_p; }
thpool_t* thpool_init(unsigned int num_threads) { thpool_t* p = malloc(sizeof(thpool_t)); if (!p) return NULL; p->threads = malloc(sizeof(pthread_t) * num_threads); if (!p->threads) return NULL; p->queue = jobqueue_init(); pthread_mutex_init(&p->update_mutex, NULL); pthread_cond_init(&p->update_cv, NULL); p->num_threads = num_threads; p->running = true; // Initialize and detach threads for (int i = 0; i < num_threads; i++) { int rc = pthread_create(&p->threads[i], NULL, (void*) thread_do, p); if (rc) return NULL; pthread_detach(p->threads[i]); } return p; }
/* Initialise thread pool */ struct thpool_* thpool_init(int num_threads) { threads_on_hold = 0; threads_keepalive = 1; if (num_threads < 0){ num_threads = 0; } /* Make new thread pool */ thpool_* thpool_p; thpool_p = (struct thpool_*)malloc(sizeof(struct thpool_)); if (thpool_p == NULL) { fprintf(stderr, "thpool_init(): Could not allocate memory for thread pool\n"); return NULL; } thpool_p->num_threads_alive = 0; thpool_p->num_threads_working = 0; /* Initialise the job queue */ if (jobqueue_init(thpool_p) == -1) { fprintf(stderr, "thpool_init(): Could not allocate memory for job queue\n"); free(thpool_p); return NULL; } /* Make threads in pool */ thpool_p->threads = (struct thread**)malloc(num_threads * sizeof(struct thread *)); if (thpool_p->threads == NULL){ fprintf(stderr, "thpool_init(): Could not allocate memory for threads\n"); jobqueue_destroy(thpool_p); free(thpool_p->jobqueue_p); free(thpool_p); return NULL; } /* Make tag list */ taglist *tlist; tlist = (taglist *) malloc(sizeof(taglist)); if (tlist == NULL) { fprintf(stderr, "thpool_init(): Could not allocate memory for job queue\n"); jobqueue_destroy(thpool_p); free(thpool_p->jobqueue_p); free(thpool_p->threads); free(thpool_p); return NULL; } tlist->num = TAGLIST_SIZE; tlist->tags = (tag *) calloc(tlist->num, sizeof(tag)); if (tlist->tags == NULL) { fprintf(stderr, "thpool_init(): Could not allocate memory for tag list\n"); jobqueue_destroy(thpool_p); free(thpool_p->jobqueue_p); free(thpool_p->threads); free(thpool_p); free(tlist); return NULL; } pthread_mutex_init(&tlist->lock, NULL); thpool_p->tlist = tlist; pthread_mutex_init(&(thpool_p->thcount_lock), NULL); pthread_cond_init(&thpool_p->threads_all_idle, NULL); /* Thread init */ for (int n = 0; n < num_threads; n++) { thread_init(thpool_p, &thpool_p->threads[n], n); if (THPOOL_DEBUG) printf("THPOOL_DEBUG: Created thread %d in pool \n", n); } /* Wait for threads to initialize */ while (thpool_p->num_threads_alive != num_threads) {} return thpool_p; }