Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.c++ > #79976

Re: std::thread does not follow RAII principles

From Bonita Montero <Bonita.Montero@gmail.com>
Newsgroups comp.lang.c++
Subject Re: std::thread does not follow RAII principles
Date 2021-05-26 14:04 +0200
Organization A noiseless patient Spider
Message-ID <s8ldgr$aq0$1@dont-email.me> (permalink)
References <s8lcnq$p2a$1@gioia.aioe.org>

Show all headers | View raw


> the program will still crash, because std::thread does not like being
> destroyed without being joined or detached, so it just terminates if that's
> the case.

Where's the problem ? Use a scrope-guard which waits for the end of the
thread or a jthread with C++.

That's my scope-guard class:

#pragma once
#include <cassert>
#include "debug_exceptions.h"

template<typename T>
struct invoke_on_destruct
{
private:
	T    m_t;
	bool m_enabled;

public:
	invoke_on_destruct( T t ) :
		m_t( t ), m_enabled( true )
	{
	}
	~invoke_on_destruct()
	{
		if( m_enabled )
		{
			try_debug
			{
				m_t();
			}
			catch_debug
			{
				assert(false);
			}
		}
	}
	void disable_and_invoke()
	{
		m_enabled = false;
		m_t();
	}
	void disable()
	{
		m_enabled = false;
	}
	void enable()
	{
		m_enabled = true;
	}
};

#if defined(IOD_SHORT)
template<typename C>
using iod = invoke_on_destruct<C>;
#define IOD(varName, lambdaName) \
iod<decltype(lambdaName)> varName( lambdaName );
#endif

Back to comp.lang.c++ | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

std::thread does not follow RAII principles Juha Nieminen <nospam@thanks.invalid> - 2021-05-26 11:51 +0000
  Re: std::thread does not follow RAII principles Bonita Montero <Bonita.Montero@gmail.com> - 2021-05-26 14:04 +0200
    Re: std::thread does not follow RAII principles MrSpook__u_8@_4fk8arybb76.eu - 2021-05-26 15:25 +0000
      Re: std::thread does not follow RAII principles Bonita Montero <Bonita.Montero@gmail.com> - 2021-05-26 17:50 +0200
  Re: std::thread does not follow RAII principles Paavo Helde <myfirstname@osa.pri.ee> - 2021-05-26 17:35 +0300
    Re: std::thread does not follow RAII principles Manfred <noname@add.invalid> - 2021-05-26 17:44 +0200
    Re: std::thread does not follow RAII principles Bonita Montero <Bonita.Montero@gmail.com> - 2021-05-26 17:52 +0200
      Re: std::thread does not follow RAII principles Paavo Helde <myfirstname@osa.pri.ee> - 2021-05-26 21:41 +0300
  Re: std::thread does not follow RAII principles scott@slp53.sl.home (Scott Lurndal) - 2021-05-26 15:20 +0000
    Re: std::thread does not follow RAII principles Bonita Montero <Bonita.Montero@gmail.com> - 2021-05-26 17:54 +0200
      Re: std::thread does not follow RAII principles MrSpook_c7o6A5w1@9wp2.net - 2021-05-26 16:28 +0000
        Re: std::thread does not follow RAII principles Bonita Montero <Bonita.Montero@gmail.com> - 2021-05-26 19:04 +0200
  Re: std::thread does not follow RAII principles MrSpook_0crgrbi6@q0jfxa33c.biz - 2021-05-26 15:24 +0000
    Re: std::thread does not follow RAII principles Bonita Montero <Bonita.Montero@gmail.com> - 2021-05-26 18:02 +0200

csiph-web