actually this post should be named as “pure C multithreads”, while it’s better to write some real code. a simple threadpool in pure C
exposed APIs
at most simple case, what a threadpool object should do?
thpool_init(num_threads);
thpool_destory(threapool_obj);
thpool_add_work(callback, args);
// so user define event callback() can be passed in;
threadpool is used to assign jobs (from job list) to a thread. so joblist should be an internal class; and it’s better to package thread, job as separate class too; to add work, what kind of funcs need for joblist ? at least, to add new job(from outside) to the joblist.
internal structures:
|
|
consider syncronlization
will multi threads call add_task() simutaneously? if so, jobqueue should have a mutex object;
|
|
during threadpool initialization, will all threads be created simultaneously and immediately? if not, there should be thread status flags (creating, exisiting-but-idle, working, died); and to update these flags need protect by mutex object;
|
|
this is an interesting, when design a lib, what’s in my mind. hopefully implement by the end of week.