线程池连接函数

时间:2017-05-19 18:56:45

标签: c multithreading threadpool

我正在尝试在C中创建一个线程池。在那里,我必须实现一个join函数,它将每个线程作为参数;实质上,它与thread_join函数类似。谁能为我提供有关如何实现简单线程连接功能的资源?

//structure that contains a pthread 
struct ThreadID; 

// from this run method I am creating threads in a thread pool
int ThreadPool_run(struct ThreadPool *, struct ThreadID *, void *(*run)(void *), void *);

// this is the join function   
int ThreadPool_join(struct ThreadID, void **);

1 个答案:

答案 0 :(得分:2)

我不确定你是否完全理解线程池的概念。你不应该有线程进入线程池接口的概念。事实上,我更喜欢java的ExecutorService名称。它更好地包含了这个概念。基本上,您希望异步运行任务,而不必担心线程生存期。

这样的服务会有一个如下所示的界面:

typedef struct executor executor_t;
typedef struct executor_options executor_options_t;
typedef struct executor_task executor_task_t;
typedef struct blocking_queue blocking_queue_t;
typedef struct future future_t;
typedef void *(*task_t)(void* data);

int executor_init(const executor_options_t options);
int executor_submit(const executor_t *const executor, future_t *const future, const task_t task, const void *const data);
int executor_destroy(const executor_t *const executor);

int blocking_queue_init(blocking_queue_t *const queue);
int blocking_queue_enqueue(const blocking_queue_t *const queue, const void *const data);
int blocking_queue_dequeue(const blocking_queue_t *const queue, const void ** data);
int blocking_queue_destroy(const blocking_queue_t *const queue);

int future_init(future_t *const future);
int future_wait(const future_t *const future);
int future_destroy(const future_t *const future);

结构可能如下所示:

struct executor_options {
    int min_thread;
    int max_thread;
    int max_idle_ms;
};

struct executor {
    pthread_mutex_t mutex;
    pthread_t *threads;
    executor_options_t options;
    blocking_queue_t *queue;
};

struct executor_task {
    task_t task;
    void *data;
    future_t *future;
};

struct blocking_queue {
    executor_task_t *tasks;
    pthread_mutex_t mutex;
    pthread_cond_t empty;
    pthread_cond_t full;
    bool_t is_empty;
    bool_t is_full;
};

struct future {
    void *result;
    pthread_mutex_t mutex;
    pthread_cond_t computed;
    bool_t is_computed;
};

executor_init中,您将初始化所有线程。这些线程应该阻塞等待任务的队列,运行它们,并通知未来的条件以解除对等待者的阻止。在executor_submit中,应该初始化给定的未来,并且任务和未来应该排队,由工作人员异步计算。然后,用户可以使用未来等待任务的结果。最后,executor_destroy应该等待计算所有剩余的任务,以防止提交任何新任务。它最终释放所有资源并返回。