POSIX Thread

https://pubs.opengroup.org/onlinepubs/9799919799/basedefs/pthread.h.html

Copyright

Copyright (c) 2025 TNX Software Ltd. All rights reserved. Licensed under the terms of the QuantumRT Kernel License. the LICENSE file in the root of this package for details.

Defines

PTHREAD_CREATE_JOINABLE (1u << 1)

Joinable thread attribute.

PTHREAD_CREATE_DETACHED (1u << 2)

Detached thread attribute.

PTHREAD_CANCEL_ENABLE (1u << 3)

Cancel enable attribute.

PTHREAD_CANCEL_DISABLE (1u << 4)

Cancel disable attribute.

PTHREAD_CANCEL_DEFERRED (1u << 5)

Deferred cancel attribute.

PTHREAD_CANCEL_ASYNCHRONOUS (1u << 6)

Asynchronous cancel attribute.

PTHREAD_PRIO_INHERIT (1u << 0)

Priority inheritance protocol.

PTHREAD_PRIO_NONE (1u << 1)

No priority protocol.

PTHREAD_PRIO_PROTECT (1u << 2)

Priority protect protocol.

PTHREAD_MUTEX_DEFAULT (PTHREAD_MUTEX_NORMAL)

Default mutex type.

PTHREAD_MUTEX_ERRORCHECK (1u << 3)

Error-checking mutex type.

PTHREAD_MUTEX_NORMAL (1u << 4)

Normal mutex type.

PTHREAD_MUTEX_RECURSIVE (1u << 5)

Recursive mutex type.

PTHREAD_MUTEX_ROBUST (1u << 6)

Robust mutex type. Not supported.

PTHREAD_MUTEX_STALLED (1u << 7)

Stalled mutex type. Not supported.

PTHREAD_PROCESS_SHARED (1u << 8)

Process-shared mutex attribute. Not supported.

PTHREAD_PROCESS_PRIVATE (1u << 9)

Process-private mutex attribute. Not supported.

PTHREAD_SCOPE_PROCESS (1u << 10)

Process scope mutex attribute. Not supported.

PTHREAD_SCOPE_SYSTEM (1u << 11)

System scope mutex attribute. Not supported.

PTHREAD_INHERIT_SCHED (1u << 0)

Inherit scheduling attribute.

PTHREAD_EXPLICIT_SCHED (1u << 1)

Explicit scheduling attribute.

PTHREAD_PRIVILEGED_NP (1u << 0)

Privileged thread attribute.

PTHREAD_UNPRIVILEGED_NP (1u << 1)

Unprivileged thread attribute.

PTHREAD_ONCE_INIT (0)

Once initialization. Not supported.

PTHREAD_COND_INITIALIZER (0)

Condition variable initialization. Not supported.

PTHREAD_MUTEX_INITIALIZER

Mutex initialization.

PTHREAD_RWLOCK_INITIALIZER

Read-write lock initialization. Not supported.

PTHREAD_NULL ((pthread_t)-1)

Not equal to valid thread.

Functions

int pthread_mutex_clocklock(pthread_mutex_t *mutex, clockid_t clock_id, const struct timespec *abstime)

Lock a mutex with a timeout based on a specific clock. If the mutex is already locked, the calling thread will block until the mutex becomes available or the specified absolute time is reached.

  • EDEADLK: The current thread already owns the mutex and the mutex type is PTHREAD_MUTEX_ERRORCHECK.

  • EAGAIN: The maximum number of recursive locks for the mutex has been exceeded.

  • EINVAL: The mutex pointer is NULL or the mutex is not initialized.

  • EINVAL: The mutex was created with PTHREAD_PRIO_PROTECT and the current thread’s priority is higher than the current priority ceiling.

  • EINVAL: The thread would have blocked but the abstime parameter specified nanoseconds outside the valid range of [0, 999’999’999].

  • EINVAL: The clock ID specified is not supported.

Parameters:
  • mutex – Pointer to the mutex to lock.

  • clock_id – Clock ID to use for the timeout.

  • abstime – Pointer to the absolute time to wait until.

Returns:

0 on success, or an error number on failure.

int pthread_mutex_timedlock(pthread_mutex_t *mutex, const struct timespec *abstime)

Lock a mutex with a timeout. If the mutex is already locked, the calling thread will block until the mutex becomes available or the specified absolute time is reached.

Error numbers:

  • EDEADLK: The current thread already owns the mutex and the mutex type is PTHREAD_MUTEX_ERRORCHECK.

  • EAGAIN: The maximum number of recursive locks for the mutex has been exceeded.

  • EINVAL: The mutex pointer is NULL or the mutex is not initialized.

  • EINVAL: The mutex was created with PTHREAD_PRIO_PROTECT and the current thread’s priority is higher than the current priority ceiling.

  • EINVAL: The thread would have blocked but the abstime parameter specified nanoseconds outside the valid range of [0, 999’999’999].

Parameters:
  • mutex – Pointer to the mutex to lock.

  • abstime – Pointer to the absolute time to wait until.

Returns:

0 on success, or an error number on failure.

int pthread_mutex_destroy(pthread_mutex_t *mutex)

Destroy a mutex.

Error numbers:

  • EINVAL: The mutex pointer is NULL or the mutex is not initialized.

  • EBUSY: The mutex object is currently in use.

Parameters:
  • mutex – Pointer to the mutex to destroy.

Returns:

0 on success, or an error number on failure.

int pthread_mutex_init(pthread_mutex_t *mutex, const pthread_mutexattr_t *attr)

Initialize a mutex with specified attributes.

Error numbers:

  • EINVAL: The mutex pointer is NULL or the mutex is already initialized.

Parameters:
  • mutex – Pointer to the mutex to initialize.

  • attr – Pointer to the mutex attributes. NULL for default attributes.

Returns:

0 on success, or -1 on failure.

int pthread_mutex_getprioceiling(const pthread_mutex_t *mutex, int *prioceiling)

Get the priority ceiling of a mutex.

Error numbers:

  • EINVAL: The mutex pointer is NULL.

  • EINVAL: The mutex protocol attribute is set to PTHREAD_PRIO_NONE.

Parameters:
  • mutex – Pointer to the mutex.

  • prioceiling – Pointer to store the priority ceiling value.

Returns:

0 on success, or an error number on failure.

int pthread_mutex_setprioceiling(pthread_mutex_t *mutex, int prioceiling, int *old_ceiling)

Set the priority ceiling of a mutex.

Error numbers:

  • EINVAL: The mutex pointer is NULL.

  • EINVAL: The mutex protocol attribute is set to PTHREAD_PRIO_NONE.

  • EINVAL: The mutex was created with PTHREAD_PRIO_PROTECT and the current thread’s priority is higher than the current priority ceiling.

  • EAGAIN: The system lacked the necessary resources to change the priority ceiling.

  • EDEADLK: A deadlock condition was detected.

Parameters:
  • mutex – Pointer to the mutex.

  • prioceiling – New priority ceiling value.

  • old_ceiling – Pointer to store the old priority ceiling value.

Returns:

0 on success, or an error number on failure.

int pthread_mutex_lock(pthread_mutex_t *mutex)

Lock a mutex. If the mutex is already locked, the calling thread will block until the mutex becomes available.

Error numbers:

  • EDEADLK: The current thread already owns the mutex and the mutex type is PTHREAD_MUTEX_ERRORCHECK.

  • EAGAIN: The maximum number of recursive locks for the mutex has been exceeded.

  • EINVAL: The mutex pointer is NULL or the mutex is not initialized.

  • EINVAL: The mutex was created with PTHREAD_PRIO_PROTECT and the current thread’s priority is higher than the current priority ceiling.

Parameters:
  • mutex – Pointer to the mutex to lock.

Returns:

0 on success, or an error number on failure.

#include "pthread.h"
#include "tensorflow/lite/c/c_api.h"

// Shared model and interpreter
pthread_mutex_t      model_lock  = PTHREAD_MUTEX_INITIALIZER;
TfLiteModel         *model       = NULL;
TfLiteInterpreter   *interpreter = NULL;

int main(void)
{
    TfLiteInterpreterOptions *options;
    pthread_t t_cam;
    pthread_t t_mic;

    model = TfLiteModelCreateFromFile("/flash/model.tflite");

    options = TfLiteInterpreterOptionsCreate();
    TfLiteInterpreterOptionsSetNumThreads(options, 1);

    interpreter = TfLiteInterpreterCreate(model, options);
    TfLiteInterpreterAllocateTensors(interpreter);

    pthread_create(&t_cam, NULL, inference_thread, camera_input);
    pthread_create(&t_mic, NULL, inference_thread, mic_input);
}

void *inference_thread(void *arg)
{
    TfLiteTensor *input;
    TfLiteTensor *output;
    const float  *input_data = (const float*)arg;

    for (;;)
    {
        // Multiple inference threads share the same model and interpreter
        // Protect the interpreter and tensor buffer during inference
        pthread_mutex_lock(&model_lock);

        input = TfLiteInterpreterGetInputTensor(interpreter, 0);
        memcpy(input->data.f, input_data, input->bytes);

        TfLiteInterpreterInvoke(interpreter);

        output = TfLiteInterpreterGetOutputTensor(interpreter, 0);
        handle_results(output);

        // Unlock the model and interpreter for other threads
        pthread_mutex_unlock(&model_lock);
    }
    return NULL;
}

int pthread_mutex_trylock(pthread_mutex_t *mutex)

Try to lock a mutex. If the mutex is already locked, the function will return immediately.

Error numbers:

  • EBUSY: The mutex is already locked by another thread.

  • EDEADLK: The current thread already owns the mutex and the mutex type is PTHREAD_MUTEX_ERRORCHECK.

  • EINVAL: The mutex pointer is NULL or the mutex is not initialized.

  • EAGAIN: The maximum number of recursive locks for the mutex has been exceeded.

  • EINVAL: The mutex was created with PTHREAD_PRIO_PROTECT and the current thread’s priority is higher than the current priority ceiling.

Parameters:
  • mutex – Pointer to the mutex to lock.

Returns:

0 on success, or an error number on failure.

int pthread_mutex_unlock(pthread_mutex_t *mutex)

Unlock a mutex.

Error numbers:

  • EPERM: The current thread does not own the mutex.

  • EINVAL: The mutex pointer is NULL or the mutex is not initialized.

Parameters:
  • mutex – Pointer to the mutex to unlock.

Returns:

0 on success, or an error number on failure.

int pthread_mutexattr_destroy(pthread_mutexattr_t *attr)

Destroy a mutex attributes object.

Error numbers:

  • EINVAL: The mutex attributes pointer is NULL or the mutex attributes pointer is not initialized.

Parameters:
  • attr – Pointer to the mutex attributes to destroy.

Returns:

0 on success, or an error number on failure.

int pthread_mutexattr_init(pthread_mutexattr_t *attr)

Initialize a mutex attributes object with default values.

The default protocol is PTHREAD_PRIO_NONE. Error numbers:

  • EINVAL: The mutex attributes pointer is NULL or the mutex attributes pointer is already initialized.

Parameters:
  • attr – Pointer to the mutex attributes to initialize.

Returns:

0 on success, or an error number on failure.

int pthread_mutexattr_getprioceiling(const pthread_mutexattr_t *attr, int *prioceiling)

Get the priority ceiling attribute from a mutex attributes object.

Error numbers:

  • EINVAL: The mutex attributes pointer is NULL or the mutex attributes pointer is not initialized.

Parameters:
  • attr – Pointer to the mutex attributes object.

  • prioceiling – Pointer to store the priority ceiling value.

Returns:

0 on success, or an error number on failure.

int pthread_mutexattr_setprioceiling(pthread_mutexattr_t *attr, int prioceiling)

Set the priority ceiling attribute in a mutex attributes object.

Error numbers:

  • EINVAL: The mutex attributes pointer is NULL or the mutex attributes pointer is not initialized.

Parameters:
  • attr – Pointer to the mutex attributes object.

  • prioceiling – New priority ceiling value.

Returns:

0 on success, or an error number on failure.

int pthread_mutexattr_getprotocol(const pthread_mutexattr_t *attr, int *protocol)

Get the protocol attribute from a mutex attributes object.

Error numbers:

  • EINVAL: The mutex attributes pointer is NULL or the mutex attributes pointer is not initialized.

  • EINVAL: The protocol value is not valid.

Parameters:
  • attr – Pointer to the mutex attributes object.

  • protocol – Pointer to store the protocol value.

Returns:

0 on success, or an error number on failure.

int pthread_mutexattr_setprotocol(pthread_mutexattr_t *attr, int protocol)

Set the protocol attribute in a mutex attributes object.

Error numbers:

  • EINVAL: The mutex attributes pointer is NULL or the mutex attributes pointer is not initialized.

  • EINVAL: The protocol value is not valid.

Parameters:
  • attr – Pointer to the mutex attributes object.

  • protocol – New protocol value.

Returns:

0 on success, or an error number on failure.

int pthread_mutexattr_gettype(const pthread_mutexattr_t *attr, int *type)

Get the type attribute from a mutex attributes object.

Error numbers:

  • EINVAL: The mutex attributes pointer is NULL or the mutex attributes pointer is not initialized.

  • EINVAL: The type value is not valid.

Parameters:
  • attr – Pointer to the mutex attributes object.

  • type – Pointer to store the type value.

Returns:

0 on success, or an error number on failure.

int pthread_mutexattr_settype(pthread_mutexattr_t *attr, int type)

Set the type attribute in a mutex attributes object.

Error numbers:

  • EINVAL: The mutex attributes pointer is NULL or the mutex attributes pointer is not initialized.

  • EINVAL: The type value is not valid.

Parameters:
  • attr – Pointer to the mutex attributes object.

  • type – New type value.

Returns:

0 on success, or an error number on failure.

int pthread_mutexattr_getpshared(const pthread_mutexattr_t *attr, int *pshared)

Not supported.

Returns:

ENOSYS.

int pthread_mutexattr_setpshared(pthread_mutexattr_t *attr, int pshared)

Not supported.

Returns:

ENOSYS.

int pthread_mutexattr_getrobust(const pthread_mutexattr_t *attr, int *robust)

Not supported.

Returns:

ENOSYS.

int pthread_mutexattr_setrobust(pthread_mutexattr_t *attr, int robust)

Not supported.

Returns:

ENOSYS.

int pthread_mutex_consistent(pthread_mutex_t *mutex)

Not supported.

Returns:

ENOSYS.

int pthread_create(pthread_t *thread, const pthread_attr_t *attr, void *(*start_routine)(void*), void *arg)

Create a new thread.

Parameters:
  • thread – Pointer to the thread handle. NULL if not needed.

  • attr – Pointer to the thread attributes. NULL for default attributes.

  • start_routine – Pointer to the function to be executed by the thread.

  • arg – Argument to be passed to the thread function.

Returns:

0 on success, or an error number on failure.

#include "pthread.h"
#include "stdio.h"

void *thread_a(void *arg)
{
    void            *ret;
    pthread_t        thread;

    if (pthread_create(&thread, NULL, thread_b, "thread_b entered") == -1)
    {
        exit(2);
    }

    if (pthread_join(thread, ret) == -1)
    {
        exit(3);
    }

    printf("thread_b() exited with return value %s\r\n", (char *)ret);

    return NULL;
}

void *thread_b(void *arg)
{
    char *ret = "thread_b exited\r\n";

    printf("thread_b() entered with argument %s\r\n", (char *)arg);

    // Perform thread operations...

    return ret;
}

void pthread_cleanup_push(void (*routine)(void*), void *arg)

Push a cleanup handler onto the thread’s cleanup stack.

Parameters:
  • routine – Pointer to the cleanup handler function.

  • arg – Argument to be passed to the cleanup handler.

    #include "pthread.h"
    
    pthread_mutex_t lock;
    
    void lock_cleanup(void *arg)
    {
        pthread_mutex_unlock((pthread_mutex_t *)arg);
    }
    
    void *thread(void *arg)
    {
        // Even if the thread is cancelled while in the critical,
        // section the lock will be unlocked.
    
        pthread_mutex_t *lock = (pthread_mutex_t *)arg;
    
        pthread_cleanup_push(lock_cleanup, (void *)lock);
    
        do_critical_section_work();
    
        pthread_cleanup_pop(1); // Execute cleanup handler
    
        return NULL;
    }
    

void pthread_cleanup_pop(int execute)

Pop a cleanup handler from the thread’s cleanup stack.

See also

pthread_cleanup_push() for example.

Parameters:
  • execute – If non-zero, the cleanup handler is executed.

int pthread_getschedparam(pthread_t thread, int *policy, struct sched_param *param)

Get the scheduling parameters of the specified thread.

Parameters:
  • thread – The thread to query.

  • policy – Pointer to a location where the scheduling policy will be stored.

  • param – Pointer to a location where the scheduling parameters will be stored.

Returns:

0 on success, or an error number on failure.

int pthread_setschedparam(pthread_t thread, int policy, const struct sched_param *param)

Set the scheduling policy and parameters of the specified thread. May trigger a context switch if the new priority is higher than the current priority.

Parameters:
  • thread – The thread to set the scheduling policy and parameters for.

  • policy – The new scheduling policy. One of:

  • param – Pointer to a structure containing the new scheduling parameters.

Returns:

0 on success, or an error number on failure.

int pthread_setschedprio(pthread_t thread, int prio)

Set thread scheduling priority. May trigger a context switch if the new priority is higher than the current priority.

Parameters:
  • thread – The thread to set the priority for.

  • prio – The new priority value.

Returns:

0 on success, or an error number on failure.

void pthread_exit(void *value_ptr)

Terminate the calling thread. Same as returning from the thread function.

void *thread(void *arg)
{
    // Perform thread operations...

    pthread_exit((void *)0);
}

Parameters:
  • value_ptr – Pointer to the return value of the thread.

int pthread_equal(pthread_t t1, pthread_t t2)

Compare two thread identifiers.

Parameters:
  • t1 – First thread identifier.

  • t2 – Second thread identifier.

Returns:

0 if equal, non-zero otherwise.

pthread_t pthread_self(void)

Get the calling thread’s identifier.

Returns:

The calling thread’s ID.

int pthread_cancel(pthread_t thread)

Request cancellation of the specified thread.

Parameters:
  • thread – The thread to cancel.

Returns:

0 on success, or an error number on failure.

void *worker_thread(void *arg)
{ 
    pthread_setcanceltype(PTHREAD_CANCEL_DEFERRED, NULL);

    // Simulate work

    // Create a cancellation point
    pthread_testcancel();

    return NULL;
}

void *thread(void *arg)
{
    struct timespec ts = {0, 100000000};

    // Let the worker thread run for a while
    nanosleep(&ts, NULL);

    pthread_cancel(worker);

    pthread_join(worker, NULL);

    return NULL;
}

int pthread_setcancelstate(int state, int *oldstate)

Set the cancelability state of the calling thread.

Parameters:
Returns:

0 on success, or an error number on failure.

int pthread_setcanceltype(int type, int *oldtype)

Set the cancelability type of the calling thread.

See also

pthread_cancel() for example.

Parameters:
Returns:

0 on success, or an error number on failure.

void pthread_testcancel(void)

Create a cancellation point in the calling thread. A cancellation point is location where the control flow is returned back to the scheduler.

See also

pthread_cancel() for example.

int pthread_join(pthread_t thread, void **value_ptr)

Wait for the specified thread to terminate. Multiple simultaneous joins on the same thread are not allowed. Detached threads and deferred cancelability type threads are not joinable.

See also

pthread_create() for example.

Parameters:
  • thread – The thread to wait for.

  • value_ptr – Pointer to a location where the return value of the thread will be stored.

Returns:

0 on success, or EDEADLK if the thread is trying to join itself, or the thread is not joinable.

int pthread_detach(pthread_t thread)

Detach the specified thread. A detached thread’s resources are automatically released upon termination.

Parameters:
  • thread – The thread to detach.

Returns:

0 on success, or EINVAL if the thread does not exist.

int pthread_attr_init(pthread_attr_t *attr)

Initialize a thread attributes object.

Error numbers:

  • EINVAL: The thread attributes object is not valid.

    #include "pthread.h"
    
    void *thread(void *arg)
    {
        pthread_attr_t attr;
        pthread_attr_init(&attr);
    
        pthread_create(NULL, &attr, new_thread, NULL);
    
        return NULL;
    }
    

Parameters:
  • attr – Pointer to the thread attributes object to initialize.

Returns:

0 on success, or an error number on failure.

int pthread_attr_destroy(pthread_attr_t *attr)

Destroy a thread attributes object.

Error numbers:

  • EINVAL: The thread attributes object is not valid.

Parameters:
  • attr – Pointer to the thread attributes object to destroy.

Returns:

0 on success, or an error number on failure.

int pthread_attr_getinheritsched(const pthread_attr_t *attr, int *inheritsched)

Get the inherit scheduling attribute from a thread attributes object.

Error numbers:

  • EINVAL: The thread attributes object is not valid.

Parameters:
  • attr – Pointer to the thread attributes object.

  • inheritsched – Pointer to a location where the inherit scheduling attribute will be stored.

Returns:

0 on success, or an error number on failure.

int pthread_attr_setinheritsched(pthread_attr_t *attr, int inheritsched)

Set the inherit scheduling attribute in a thread attributes object.

Error numbers:

  • EINVAL: The thread attributes object is not valid.

  • ENOTSUP: Attempt to set an unsupported inherit scheduling attribute.

Parameters:
Returns:

0 on success, or an error number on failure.

int pthread_attr_getdetachstate(const pthread_attr_t *attr, int *detachstate)

Get the detach state attribute from a thread attributes object.

Error numbers:

  • EINVAL: The thread attributes object is not valid.

Parameters:
  • attr – Pointer to the thread attributes object.

  • detachstate – Pointer to a location where the detach state attribute will be stored.

Returns:

0 on success, or an error number on failure.

int pthread_attr_setdetachstate(pthread_attr_t *attr, int detachstate)

Set the detach state attribute in a thread attributes object.

Error numbers:

  • EINVAL: The thread attributes object is not valid.

Parameters:
Returns:

0 on success, or an error number on failure.

#include "pthread.h"

void *thread(void *arg)
{
    pthread_attr_t attr;
    pthread_attr_init(&attr);
    pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);

    pthread_create(NULL, &attr, new_thread, NULL);

    return NULL;
}

int pthread_attr_getstack(const pthread_attr_t *attr, void **stackaddr, size_t *stacksize)

Get the stack address and size attributes from a thread attributes object.

Error numbers:

  • EINVAL: The thread attributes object is not valid.

Parameters:
  • attr – Pointer to the thread attribute object.

  • stackaddr – Pointer to the stack base address.

  • stacksize – Pointer to a size_t variable where the stack size will be stored.

Returns:

0 on success, or an error number on failure.

int pthread_attr_setstack(pthread_attr_t *attr, void *stackaddr, size_t stacksize)

Set the stack address and size attributes in a thread attributes object.

Error numbers:

  • EINVAL: The thread attributes object is not valid.

Parameters:
  • attr – Pointer to the thread attribute object.

  • stackaddr – Pointer to the stack base address.

  • stacksize – Stack size.

Returns:

0 on success, or an error number on failure.

unsigned char myStack[1024u];

void *thread_a(void *arg)
{
    pthread_attr_t attr;
    pthread_attr_init(&attr);

    pthread_attr_setstack(&attr, myStack, sizeof(myStack));
    pthread_create(NULL, &attr, thread_b, NULL);

    return NULL;
}

void *thread_b(void *arg)
{
    // Thread implementation
    return NULL;
}

int pthread_attr_getstacksize(const pthread_attr_t *attr, size_t *stacksize)

Get the stack size attribute from a thread attributes object.

Error numbers:

  • EINVAL: The thread attributes object is not valid.

Parameters:
  • attr – Pointer to the thread attribute object.

  • stacksize – Pointer to a size_t variable where the stack size will be stored.

Returns:

0 on success, or an error number on failure.

int pthread_attr_setstacksize(pthread_attr_t *attr, size_t stacksize)

Set the stack size attribute in a thread attributes object. pthread_create() will allocate a stack with at least this size for the new thread. The allocated stack size is rounded up to the nearest power of two.

Error numbers:

  • EINVAL: The thread attributes object is not valid.

Parameters:
  • attr – Pointer to the thread attribute object.

  • stacksize – Stack size.

Returns:

0 on success, or an error number on failure.

void *thread_a(void *arg)
{
    pthread_attr_t attr;
    pthread_attr_init(&attr);

    // Set thread attributes as needed
    pthread_attr_setstacksize(&attr, sizeof(myStack));

    // No need to provide stack address,
    // it will be allocated by pthread_create()
    pthread_create(NULL, &attr, thread_b, NULL);

    return NULL;
}

void *thread_b(void *arg)
{
    // Thread implementation
    return NULL;
}

int pthread_attr_getschedpolicy(const pthread_attr_t *attr, int *policy)

Get the scheduling policy attribute from a thread attributes object.

Error numbers:

  • EINVAL: The thread attributes object is not valid.

Parameters:
  • attr – Pointer to the thread attributes object.

  • policy – Pointer to a location where the scheduling policy attribute will be stored.

Returns:

0 on success, or an error number on failure.

int pthread_attr_setschedpolicy(pthread_attr_t *attr, int policy)

Set the scheduling policy attribute in a thread attributes object.

Error numbers:

  • EINVAL: The thread attributes object is not valid.

  • ENOTSUP: Invalid scheduling policy value.

Parameters:
  • attr – Pointer to the thread attributes object.

  • policy – The scheduling policy attribute to set. One of:

Returns:

0 on success, or an error number on failure.

#include "pthread.h"

void *thread(void *arg)
{
    pthread_attr_t attr;
    pthread_attr_init(&attr);

    // Default policy is SCHED_FIFO
    pthread_attr_setschedpolicy(&attr, SCHED_RR);

    pthread_create(NULL, &attr, new_thread, NULL);

    return NULL;
}

int pthread_attr_getschedparam(const pthread_attr_t *attr, struct sched_param *param)

Get the scheduling parameters attribute from a thread attributes object.

Error numbers:

  • EINVAL: The thread attributes object is not valid.

  • EINVAL: The scheduling parameters pointer is NULL.

Parameters:
  • attr – Pointer to the thread attributes object.

  • param – Pointer to a location where the scheduling parameters will be stored.

Returns:

0 on success, or an error number on failure.

int pthread_attr_setschedparam(pthread_attr_t *attr, const struct sched_param *param)

Set the scheduling parameters attribute in a thread attributes object.

Error numbers:

  • EINVAL: The thread attributes object is not valid.

  • EINVAL: The scheduling parameters pointer is NULL.

  • ENOTSUP: Invalid scheduling priority value.

Parameters:
  • attr – Pointer to the thread attributes object.

  • param – Pointer to the scheduling parameters to set.

Returns:

0 on success, or an error number on failure.

#include "pthread.h"

void *thread(void *arg)
{
    struct sched_param param;
    pthread_attr_t attr;
    pthread_attr_init(&attr);

    param.sched_priority = 10;
    pthread_attr_setschedparam(&attr, &param);

    pthread_create(NULL, &attr, new_thread, NULL);

    return NULL;
}

int pthread_attr_getscope(const pthread_attr_t *attr, int *contentionscope)

Not supported.

Returns:

ENOSYS.

int pthread_attr_setscope(pthread_attr_t *attr, int contentionscope)

Not supported.

Returns:

ENOSYS.

int pthread_attr_getguardsize(const pthread_attr_t *attr, size_t *guardsize)

Not supported.

Returns:

ENOSYS.

int pthread_attr_setguardsize(pthread_attr_t *attr, size_t guardsize)

Not supported.

Returns:

ENOSYS.

int pthread_attr_getname_np(const pthread_attr_t *attr, char *name, size_t len)

Get the name attribute from a thread attributes object.

Error numbers:

  • EINVAL: The thread attributes object is not valid.

Parameters:
  • attr – Pointer to the thread attributes object.

  • name – Pointer to a buffer to store the thread name.

  • len – Length of the buffer.

Returns:

0 on success, or an error number on failure.

int pthread_attr_setname_np(pthread_attr_t *attr, const char *name)

Set the name attribute in a thread attributes object.

Error numbers:

  • EINVAL: The thread attributes object is not valid.

Parameters:
  • attr – Pointer to the thread attributes object.

  • name – The name to set for the thread.

Returns:

0 on success, or an error number on failure.

int pthread_attr_setprivilege_np(pthread_attr_t *attr, int privilege)

Set the privilege level from a thread attribute object.

Error numbers:

  • EINVAL: The thread attributes object is not valid.

Parameters:
Returns:

0 on success, or an error number on failure.

#include "pthread.h"

void *thread(void *arg)
{
    pthread_attr_t attr;
    pthread_attr_init(&attr);

    // We want to create a privileged thread in this case,
    // but the default value is unprivileged.
    pthread_attr_setprivilege_np(&attr, PTHREAD_PRIVILEGED_NP);

    pthread_create(NULL, &attr, new_thread, NULL);

    return NULL;
}

int pthread_attr_getprivilege_np(pthread_attr_t *attr, int *privilege)

Get the privilege level from a thread attribute object.

Error numbers:

  • EINVAL: The thread attributes object is not valid.

Parameters:
Returns:

0 on success, or an error number on failure.

int pthread_setprivilege_np(pthread_t thread, int privilege)

Set the privilege level of a thread. This API call can only be made by privileged thread.

Error numbers:

  • EINVAL: The thread attributes object is not valid.

  • EPERM: Insufficient permissions to set the privilege level.

Parameters:
Returns:

0 on success, or an error number on failure.

#include "pthread.h"

int main(void)
{
    ...

    qrt_kernel_boot(NULL, NULL, main_thread, NULL);

    // Main thread is created as privileged,
    // drop privilege if not needed.
    pthread_setprivilege_np(pthread_self(), PTHREAD_UNPRIVILEGED_NP);

    ...
}

int pthread_setname_np(pthread_t thread, const char *name)

Set the name of the specified thread.

Error numbers:

  • EINVAL: The thread object is not valid.

Parameters:
  • thread – The thread to set the name of.

  • name – The new name for the thread.

Returns:

0 on success, or an error number on failure.

int pthread_getname_np(pthread_t thread, char *name, size_t len)

Get the name of the specified thread.

Error numbers:

  • EINVAL: The thread object is not valid.

Parameters:
  • thread – The thread to get the name of.

  • name – Pointer to a buffer to store the thread name.

  • len – Length of the buffer.

Returns:

0 on success, or an error number on failure.

int pthread_getconcurrency(void)

Not supported.

Returns:

ENOSYS.

int pthread_setconcurrency(int new_level)

Not supported.

Returns:

ENOSYS.

int pthread_atfork(void (*prepare)(void), void (*parent)(void), void (*child)(void))

Not supported.

Returns:

ENOSYS.

void *pthread_getspecific(pthread_key_t key)

Not supported.

Returns:

ENOSYS.

int pthread_setspecific(pthread_key_t key, const void *value)

Not supported.

Returns:

ENOSYS.

int pthread_key_create(pthread_key_t *key, void (*destructor)(void*))

Not supported.

Returns:

ENOSYS.

int pthread_key_delete(pthread_key_t key)

Not supported.

Returns:

ENOSYS.

int pthread_kill(pthread_t thread, int sig)

Not supported.

Returns:

ENOSYS.

int pthread_once(pthread_once_t *once_control, void (*init_routine)(void))

Not supported.

Returns:

ENOSYS.

int pthread_sigmask(int how, const sigset_t *set, sigset_t *oset)

Not supported.

Returns:

ENOSYS.

int pthread_getcpuclockid(pthread_t thread_id, clockid_t *clock_id)

Not supported.

Returns:

ENOSYS.

int pthread_spin_destroy(pthread_spinlock_t *lock)

Not supported.

Returns:

ENOSYS.

int pthread_spin_init(pthread_spinlock_t *lock, int pshared)

Not supported.

Returns:

ENOSYS.

int pthread_spin_lock(pthread_spinlock_t *lock)

Not supported.

Returns:

ENOSYS.

int pthread_spin_trylock(pthread_spinlock_t *lock)

Not supported.

Returns:

ENOSYS.

int pthread_spin_unlock(pthread_spinlock_t *lock)

Not supported.

Returns:

ENOSYS.

int pthread_barrier_destroy(pthread_barrier_t *barrier)

Not supported.

Returns:

ENOSYS.

int pthread_barrier_init(pthread_barrier_t *barrier, const pthread_barrierattr_t *attr, unsigned count)

Not supported.

Returns:

ENOSYS.

int pthread_barrier_wait(pthread_barrier_t *barrier)

Not supported.

Returns:

ENOSYS.

int pthread_barrierattr_destroy(pthread_barrierattr_t *attr)

Not supported.

Returns:

ENOSYS.

int pthread_barrierattr_init(pthread_barrierattr_t *attr)

Not supported.

Returns:

ENOSYS.

int pthread_barrierattr_getpshared(const pthread_barrierattr_t *attr, int *pshared)

Not supported.

Returns:

ENOSYS.

int pthread_barrierattr_setpshared(pthread_barrierattr_t *attr, int pshared)

Not supported.

Returns:

ENOSYS.

int pthread_rwlock_clockrdlock(pthread_rwlock_t *rwlock, clockid_t clock_id, const struct timespec *abstime)

Not supported.

Returns:

ENOSYS.

int pthread_rwlock_timedrdlock(pthread_rwlock_t *rwlock, const struct timespec *abstime)

Not supported.

Returns:

ENOSYS.

int pthread_rwlock_clockwrlock(pthread_rwlock_t *rwlock, clockid_t clock_id, const struct timespec *abstime)

Not supported.

Returns:

ENOSYS.

int pthread_rwlock_timedwrlock(pthread_rwlock_t *rwlock, const struct timespec *abstime)

Not supported.

Returns:

ENOSYS.

int pthread_rwlock_destroy(pthread_rwlock_t *rwlock)

Not supported.

Returns:

ENOSYS.

int pthread_rwlock_init(pthread_rwlock_t *rwlock, const pthread_rwlockattr_t *attr)

Not supported.

Returns:

ENOSYS.

int pthread_rwlock_rdlock(pthread_rwlock_t *rwlock)

Not supported.

Returns:

ENOSYS.

int pthread_rwlock_tryrdlock(pthread_rwlock_t *rwlock)

Not supported.

Returns:

ENOSYS.

int pthread_rwlock_trywrlock(pthread_rwlock_t *rwlock)

Not supported.

Returns:

ENOSYS.

int pthread_rwlock_wrlock(pthread_rwlock_t *rwlock)

Not supported.

Returns:

ENOSYS.

int pthread_rwlock_unlock(pthread_rwlock_t *rwlock)

Not supported.

Returns:

ENOSYS.

int pthread_rwlockattr_destroy(pthread_rwlockattr_t *attr)

Not supported.

Returns:

ENOSYS.

int pthread_rwlockattr_init(pthread_rwlockattr_t *attr)

Not supported.

Returns:

ENOSYS.

int pthread_rwlockattr_getpshared(const pthread_rwlockattr_t *attr, int *pshared)

Not supported.

Returns:

ENOSYS.

int pthread_rwlockattr_setpshared(pthread_rwlockattr_t *attr, int pshared)

Not supported.

Returns:

ENOSYS.

int pthread_cond_init(pthread_cond_t *cond, const pthread_condattr_t *attr)

Not supported.

Returns:

ENOSYS.

int pthread_cond_destroy(pthread_cond_t *cond)

Not supported.

Returns:

ENOSYS.

int pthread_cond_wait(pthread_cond_t *cond, pthread_mutex_t *mutex)

Not supported.

Returns:

ENOSYS.

int pthread_cond_timedwait(pthread_cond_t *cond, pthread_mutex_t *mutex, const struct timespec *abstime)

Not supported.

Returns:

ENOSYS.

int pthread_cond_signal(pthread_cond_t *cond)

Not supported.

Returns:

ENOSYS.

int pthread_cond_broadcast(pthread_cond_t *cond)

Not supported.

Returns:

ENOSYS.

int pthread_condattr_init(pthread_condattr_t *attr)

Not supported.

Returns:

ENOSYS.

int pthread_condattr_destroy(pthread_condattr_t *attr)

Not supported.

Returns:

ENOSYS.

int pthread_condattr_getclock(const pthread_condattr_t *attr, clockid_t *clock_id)

Not supported.

Returns:

ENOSYS.

int pthread_condattr_setclock(pthread_condattr_t *attr, clockid_t clock_id)

Not supported.

Returns:

ENOSYS.

int pthread_condattr_getpshared(const pthread_condattr_t *attr, int *pshared)

Not supported.

Returns:

ENOSYS.

int pthread_condattr_setpshared(pthread_condattr_t *attr, int pshared)

Not supported.

Returns:

ENOSYS.

int pthread_attr_settimeslice_np(pthread_attr_t *attr, const struct timespec *timeSlice)

Set the time slice for a thread attribute object.

Error numbers:

  • EINVAL: The thread attributes object is not valid.

Parameters:
  • attr – Pointer to the thread attribute object.

  • timeSlice – Pointer to a timespec structure that specifies the time slice duration.

Returns:

0 on success, or an error number on failure.

int pthread_attr_gettimeslice_np(pthread_attr_t *attr, struct timespec *timeSlice)

Get the time slice for a thread attribute object.

Error numbers:

  • EINVAL: The thread attributes object is not valid.

Parameters:
  • attr – Pointer to the thread attribute object.

  • timeSlice – Pointer to a timespec structure that specifies the time slice duration.

Returns:

0 on success, or an error number on failure.