🚀
Ad Astra
HomeProfileProjects
  • 🔄Back to Portfolio
  • 🖥️Welcome
  • 👨‍💻Software Engineering
    • <pthreads.h> Library
    • <poll.h> Library
    • Mutual Exclusion (Mutex)
    • <semaphore.h> Library
    • Volatile Type Qualifier
    • The Opaque Pointer Pattern
  • 🔬Computer Science
    • Analysis of Algorithms
    • Git & GitHub Guide
    • Graph Theory
  • Miscellaneous
    • SWE Internship @Uber
    • Learning Reference
    • Quotes
Powered by GitBook
On this page
  1. Software Engineering

<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:

#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;
}
PreviousWelcomeNext<poll.h> Library

Last updated 9 months ago

Here is the documentation for the

👨‍💻
pthread.h library