Threading

Library Required

1#include <pthread.h>

Types

1pthread_t       // uniquely identify a thread in POSIX thread programming
2pthread_attr_t  // uniquely identify a thread attributes in POSIX

pthread_create()

Creates a new thread and starts executing the specified routine in parallel

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

Return Value:

  • Success: 0
  • Error: Error number

Example:

1void* print_msg(void* arg) {
2    printf("Thread: %s\n", (char*)arg);
3    return NULL;
4}
5
6pthread_t tid;
7pthread_create(&tid, NULL, print_msg, "Hello from thread");

pthread_join()

Waits for the specified thread to terminate and optionally collects its return value

1int pthread_join(pthread_t thread, void **retval);

Return Value:

  • Success: 0
  • Error: Error number

Example:

1void* thread_func(void* arg) { return (void*)42; }
2
3pthread_t tid;
4pthread_create(&tid, NULL, thread_func, NULL);
5void* retval;
6pthread_join(tid, &retval);
7printf("Thread returned: %ld\n", (long)retval);

pthread_detach()

Marks a thread as detached so its resources are automatically released on termination

1int pthread_detach(pthread_t thread);

Return Value:

  • Success: 0
  • Error: Error number

Example:

1void* detached_func(void* arg) { pthread_exit(NULL); }
2
3pthread_t tid;
4pthread_create(&tid, NULL, detached_func, NULL);
5pthread_detach(tid);

pthread_exit()

Terminates the calling thread and optionally returns a value to any joining thread

1void pthread_exit(void *retval);

Return Value:

  • Does not return

Example:

1void* thread_func(void* arg) {
2    pthread_exit("Finished");
3    return NULL;
4}
5
6pthread_t tid;
7pthread_create(&tid, NULL, thread_func, NULL);

pthread_self()

Returns the thread ID of the calling thread

1pthread_t pthread_self(void);

Return Value:

  • The ID of the calling thread

Example:

1void* thread_func(void* arg) {
2    printf("Thread ID: %lu\n", pthread_self());
3    return NULL;
4}
5
6pthread_t tid;
7pthread_create(&tid, NULL, thread_func, NULL);

pthread_attr_init()

Initializes a thread attribute object with default values

1int pthread_attr_init(pthread_attr_t *attr);

Return Value:

  • Success: 0
  • Error: Error number

Example:

1pthread_attr_t attr;
2pthread_attr_init(&attr);

pthread_attr_destroy()

Destroys a thread attribute object and frees associated resources

1int pthread_attr_destroy(pthread_attr_t *attr);

Return Value:

  • Success: 0
  • Error: Error number

Example:

1pthread_attr_t attr;
2pthread_attr_init(&attr);
3pthread_attr_destroy(&attr);

Thread Attr Setter/Getter

These functions are used to set and get properties of a pthread_attr_t object before creating a thread.

pthread_attr_setdetachstate / pthread_attr_getdetachstate

Sets or gets the detach state (joinable or detached).

1int pthread_attr_setdetachstate(pthread_attr_t *attr, int detachstate);
2int pthread_attr_getdetachstate(const pthread_attr_t *attr, int *detachstate);

Values: PTHREAD_CREATE_JOINABLE (default) · PTHREAD_CREATE_DETACHED

Example

1pthread_attr_t attr;
2pthread_attr_init(&attr);
3pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
4
5int state;
6pthread_attr_getdetachstate(&attr, &state);   // state now holds the detach flag
7
8pthread_attr_destroy(&attr);

pthread_attr_setschedpolicy / pthread_attr_getschedpolicy

Sets or gets the scheduling policy.

1int pthread_attr_setschedpolicy(pthread_attr_t *attr, int policy);
2int pthread_attr_getschedpolicy(const pthread_attr_t *attr, int *policy);

Policies: SCHED_OTHER (default) · SCHED_FIFO · SCHED_RR

Example

1pthread_attr_t attr;
2pthread_attr_init(&attr);
3pthread_attr_setschedpolicy(&attr, SCHED_FIFO);
4
5int policy;
6pthread_attr_getschedpolicy(&attr, &policy);  // policy now SCHED_FIFO
7
8pthread_attr_destroy(&attr);

pthread_attr_setschedparam / pthread_attr_getschedparam

Sets or gets scheduling parameters (e.g., priority).

1int pthread_attr_setschedparam(pthread_attr_t *attr,
2                               const struct sched_param *param);
3int pthread_attr_getschedparam(const pthread_attr_t *attr,
4                               struct sched_param *param);

Example

 1pthread_attr_t attr;
 2pthread_attr_init(&attr);
 3
 4struct sched_param sp = { .sched_priority = 20 };
 5pthread_attr_setschedparam(&attr, &sp);
 6
 7struct sched_param out;
 8pthread_attr_getschedparam(&attr, &out);      // out.sched_priority == 20
 9
10pthread_attr_destroy(&attr);

pthread_attr_setinheritsched / pthread_attr_getinheritsched

Sets or gets whether the thread inherits or explicitly uses scheduling attributes.

1int pthread_attr_setinheritsched(pthread_attr_t *attr, int inherit);
2int pthread_attr_getinheritsched(const pthread_attr_t *attr, int *inherit);

Values: PTHREAD_INHERIT_SCHED · PTHREAD_EXPLICIT_SCHED

Example

1pthread_attr_t attr;
2pthread_attr_init(&attr);
3pthread_attr_setinheritsched(&attr, PTHREAD_EXPLICIT_SCHED);
4
5int inherit;
6pthread_attr_getinheritsched(&attr, &inherit); // inherit now explicit
7
8pthread_attr_destroy(&attr);

pthread_attr_setstacksize / pthread_attr_getstacksize

Sets or gets the thread stack size.

1int pthread_attr_setstacksize(pthread_attr_t *attr, size_t stacksize);
2int pthread_attr_getstacksize(const pthread_attr_t *attr, size_t *stacksize);

Example

1pthread_attr_t attr;
2pthread_attr_init(&attr);
3pthread_attr_setstacksize(&attr, 1024 * 1024);  // 1 MB
4
5size_t sz;
6pthread_attr_getstacksize(&attr, &sz);          // sz == 1048576
7
8pthread_attr_destroy(&attr);

pthread_attr_setstack / pthread_attr_getstack

Sets or gets both stack address and size.

1int pthread_attr_setstack(pthread_attr_t *attr,
2                          void *stackaddr, size_t stacksize);
3int pthread_attr_getstack(const pthread_attr_t *attr,
4                          void **stackaddr, size_t *stacksize);

Example

 1char stack_area[64 * 1024];                     // 64 KB buffer
 2
 3pthread_attr_t attr;
 4pthread_attr_init(&attr);
 5pthread_attr_setstack(&attr, stack_area, sizeof stack_area);
 6
 7void *addr;
 8size_t sz;
 9pthread_attr_getstack(&attr, &addr, &sz);       // addr == stack_area, sz == 65536
10
11pthread_attr_destroy(&attr);
Last updated on