C 语言栈式线程池
C
Switch branches/tags
Nothing to show
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Failed to load latest commit information.
example docs Mar 7, 2018
src fix prev Mar 8, 2018
.gitignore Initial commit Mar 5, 2018
LICENSE Initial commit Mar 5, 2018
README.md docs Mar 7, 2018

README.md

threadpool

C 语言栈式线程池

特性

  • 动态扩展、缩减线程池规模

示例

./example/whoami.c

#include <unistd.h>
#include <stdio.h>
#include "../src/threadpool.h"

extern struct thread_pool pool;

void *
whoami(void *_ __attribute__ ((__unused__))) {
    printf("I am %zd\n", pthread_self());
    return NULL;
}

int
main(void) {
    // 新建线程池
    // 参数含义:常备线程数:4,动态扩展最大线程数:5
    // 第二个参数必须大于第一个参数
    pool.init(4, 5);

    for (int i = 0; i < 10; i++) {
        pool.add(whoami, NULL);
    }

    sleep(1);
    return 0;
}

编译

cd example && cc -O2 -lpthread whoami.c ../src/threadpool.c -o whoami