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

About

RAII Thread that joins upon destruction

Resources

Stars

0 stars

Watchers

1 watching

Forks

Latest commit

 

History

7 Commits

Folders and files

NameName
Last commit message
Last commit date
 
 
 
 
 
 

Repository files navigation

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;
}

About

RAII Thread that joins upon destruction

Resources

Stars

0 stars

Watchers

1 watching

Forks

Releases

Packages

Contributors

Languages