> For the complete documentation index, see [llms.txt](https://astranebula.gitbook.io/blog/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://astranebula.gitbook.io/blog/software-engineering/less-than-pthreads.h-greater-than-library.md).

# \<pthreads.h> Library

I learned about the **\<pthread.h> library**. The purpose of this library is to create a function that will run independently of the main program. This is achieved by creating a thread, which will terminate under various conditions, including the execution of an exit function within the thread function or when the main function reaches the end of the program.

***

The **pthread\_create()** function is used to create a new thread.

* The first argument is a pointer to a pthread\_t variable, which will be used to identify the thread in subsequent calls.
* The second argument is used to set the attributes of the thread.
* The third argument is a pointer to the function that will be executed by the thread.
* The fourth argument is a pointer to the argument that will be passed to the thread function.
* The function returns 0 if the thread is successfully created, otherwise it returns an error code.

The **pthread\_join()** function is used to wait for a thread to terminate.

* The first argument is the thread to wait for.
* The second argument is a pointer to a pointer that will be used to return the value returned by the thread function.
* The function returns 0 if the thread is successfully joined, otherwise it returns an error code.

***

### Simple implementation of the pthread library:

{% code lineNumbers="true" fullWidth="false" %}

```c
#include <pthread.h>

void *myThread(void &value)
{
  // do something

  return NULL;
}

int main()
{
  pthread_t thread;

  pthread_create(&thread, NULL, &myThread, NULL);
  pthread_join(thread, NULL);

  return 0;
}
```

{% endcode %}

Here is the documentation for the [pthread.h library](https://pubs.opengroup.org/onlinepubs/7908799/xsh/pthread.h.html)
