PTHREAD_WORKQUEUE(3)
π PTHREAD_WORKQUEUE(3) π
okay bestie, sit DOWN, because today we're learning about thread workqueues β and yes this is a Library Functions Manual situation, so you know it's gonna be giving real system-programming energy. π§β¨
β¨ NAME β¨
So there's basically two girlboss squads of functions here:
pthread_workqueue_init_np,pthread_workqueue_create_np,pthread_workqueue_additem_npβ these are the thread workqueue operations. the main characters.pthread_workqueue_attr_init_np,pthread_workqueue_attr_destroy_np,pthread_workqueue_attr_getovercommit_np,pthread_workqueue_attr_setovercommit_np,pthread_workqueue_attr_getqueuepriority_np,pthread_workqueue_attr_setqueuepriority_npβ and these are the thread workqueue attribute operations. the supporting cast, but honestly they eat. π
πΈ SYNOPSIS πΈ
like, the whole vibe is right here, no notes:
#include <pthread_workqueue.h>
int
pthread_workqueue_init_np(void);
int
pthread_workqueue_create_np(pthread_workqueue_t *workqp,
const pthread_workqueue_attr_t * attr);
int
pthread_workqueue_additem_np(pthread_workqueue_t workq,
void ( *workitem_func)(void *), void * workitem_arg,
pthread_workitem_handle_t * itemhandlep, unsigned int *gencountp);
int
pthread_workqueue_attr_init_np(pthread_workqueue_attr_t *attr);
int
pthread_workqueue_attr_destroy_np(pthread_workqueue_attr_t *attr);
int
pthread_workqueue_attr_getovercommit_np(pthread_workqueue_attr_t *attr,
int *ocommp);
int
pthread_workqueue_attr_setovercommit_np(pthread_workqueue_attr_t *attr,
int ocomm);
int
pthread_workqueue_attr_getqueuepriority_np(pthread_workqueue_attr_t *attr,
int *qpriop);
int
pthread_workqueue_attr_setqueuepriority_np(pthread_workqueue_attr_t *attr,
int qprio);
π¦ DESCRIPTION π¦
so the pthread_workqueue_*_np() functions are how you create work queues and submit work items to a thread pool. that's the whole plot. π«Ά
and here's the fun part β you, the user, can spin up multiple work queues of different priority AND manually overcommit the available resources. slay, but also, respectfully, careful. π
pthread_workqueue_init_np()β allocates and initializes the whole thread workqueue subsystem. the origin story. βpthread_workqueue_create_np()β makes a brand new thread workqueue with the attributes you pass inattr. ifattrisNULL, then the default attributes get used instead (no thoughts, head empty, defaults). and you get a workqueue handle back in theworkqpparameter. π- thread workqueue attributes are literally just there to specify parameters to
pthread_workqueue_create_np(). and the bestie behaviour here? one attribute object can be reused across multiple calls topthread_workqueue_create_np()β with or without modifications between calls. recycle, reuse, iconic. π pthread_workqueue_additem_np()β this is the one that actually submits work items to the thread pool you named in theworkqparameter. the work item function and its argument go inworkitem_funcandworkitem_arg. and the work item handle comes back to you initemhandlep. πpthread_workqueue_attr_init_np()β initializesattrwith all the default thread workqueue attributes. fresh start energy.pthread_workqueue_attr_destroy_np()β destroysattr. rip bozo π- the
pthread_workqueue_attr_set*_np()functions set the attribute that matches whatever the function name says. yes it's that straightforward, the naming ate.pthread_workqueue_attr_setovercommit_np()sets the overcommit flag. and lowkey important: if the overcommit flag is set, then more threads will be started, if needed β which may overcommit the physical resources of the system. > bestie. that is a real warning. that is your machine's actual physical resources. π¨pthread_workqueue_attr_setqueuepriority_np()sets the queue priority attribute of the thread work queue, and it must be set to one of the following values:
WORKQ_HIGH_PRIOQUEUEβ work items in the queue with this attribute will be given higher priority by the thread scheduler. main character queue fr. π₯WORKQ_DEFAULT_PRIOQUEUEβ work items in the queue with this attribute are given the default priority. neutral. chill. no drama.WORKQ_LOW_PRIOQUEUEβ work items in the queue with this attribute will be given lower priority by the thread scheduler. they're in the back of the group chat, and that's just their truth.
and the pthread_workqueue_attr_get*_np() functions? they copy the value of the attribute matching the function name to the location pointed to by the second function parameter. get = grab the value and put it over there. iykyk. β¨
π RETURN VALUES π
if it works? these functions return 0. periodt. π
if it doesn't work? they return an error number to indicate the error. not a vibe, but at least it's informative. π
π ERRORS π
okay buckle up, here's the whole list, and yes we're going through all of it:
the pthread_workqueue_init_np() function will fail if:
[ENOMEM]β Out of memory.
the pthread_workqueue_create_np() function will fail if:
[ENOMEM]β Out of memory.
the pthread_workqueue_additem_np() function will fail if:
[EINVAL]β Invalid workqueue handle.[ENOMEM]β Out of memory.[ESRCH]β Can not find workqueue.
the pthread_workqueue_attr_init_np() function will fail if:
[ENOMEM]β Out of memory.
the pthread_workqueue_attr_destroy_np() function will fail if:
[EINVAL]β Invalid value forattr.
the pthread_workqueue_attr_setqueuepriority_np() function will fail if:
[EINVAL]β Invalid value forattror forqprio.
and the pthread_workqueue_attr_setovercommit_np(), pthread_workqueue_attr_getovercommit_np() and pthread_workqueue_attr_getqueuepriority_np() functions will fail if:
[EINVAL]β Invalid value forattr.
π SEE ALSO π
go read those next bestie, we love a little homework. π
π BUGS π
okay so there are two things you genuinely need to know, and I'm saying them with my whole chest:
- there is no way, currently, to remove or destory work queues and pending work items other than exiting the process. like. that's it. that's the escape hatch. exiting the process. π
- all worker threads run at the same thread priority β however, items placed on high-priority workqueues will be executed before those on lower-priority workqueues. so the queue priority still matters, it's just not coming from the thread priority itself. noted, remembered, never forgotten. π«‘
π HISTORY π
this thread workqueues code was created to support Grand Central Dispatch (GCD or libdispatch), and it first appeared in FreeBSD 8.0. she's got lore. she's got a whole origin arc. π
π AUTHORS π
- Mark Heily
<mark@heily.com>. π«Ά
and, based on earlier work by:
- Stacey Son
<sson@FreeBSD.org> - Apple, Inc.
collab of the century, honestly. π€
so yeah, that's the whole page bestie. and before I go β this one's stamped Debian, dated December 12, 2009, and it lives in the PTHREAD_WORKQUEUE(3) section of your manual. go forth and workqueue responsibly, mwah π
πβ¨
π¦ where this page comes from
- ubuntu jammy: libpthread-workqueue-dev 0.9.1-1
- ubuntu noble: libpthread-workqueue-dev 0.9.1-1build1
- Copyright (C) 2010 mark@heily.com
- Copyright (C) 2009 sson@FreeBSD.org
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms