Skip to content
This repository was archived by the owner on Feb 16, 2026. It is now read-only.

Latest commit

 

History

History
49 lines (38 loc) · 888 Bytes

README.md

File metadata and controls

49 lines (38 loc) · 888 Bytes

RAII-Thread

Header only file for a C++ RAII thread that joins upon destruction. It wraps the interface of std::thread. Feel free to download or copy the file into your own project.

Example

To create a RAII Thread with a lambda, we can do the following:

#include "RAIIThread"
#include <iostream>

int main()
{

  {
    RAIIThread thread([](){
      std::cout << "Hello from RAII Thread" << '\n';
    });
  }

  /* Thread is auto joined upon leaving inner scope */

  return 0;
}

Similarly for functions:

#include "RAIIThread"
#include <iostream>

void threadUnsafeFunction( int i )
{
  std::cout << "This function is not thread safe because of lack of mutex" << '\n';
  std::cout << "The number is " << i << '\n';
}

int main()
{

  {
    RAIIThread thread(threadUnsafeFunction, 5);
  }

  /* Thread is auto joined upon leaving inner scope */

  return 0;
}