Portál AbcLinuxu, 24. října 2025 10:57
template<typename T>
class CancelableThreadData
{
protected:
bool _is_cleanup;
T* _data;
static void _cleanup_func(void* d);
public:
template<typename... T_args>
CancelableThreadData(T_args... args);
~CancelableThreadData();
T* operator->() { return _data; }
const T* operator->() const { return _data; }
T& operator*() { return *_data; }
const T& operator*() const { return *_data; }
};
template<typename T>
void CancelableThreadData<T>::_cleanup_func(void* d)
{
((CancelableThreadData<T>*)(d))->_is_cleanup = true;
((CancelableThreadData<T>*)(d))->~CancelableThreadData();
}
template<typename T>
template<typename... T_args>
CancelableThreadData<T>::CancelableThreadData(T_args... args)
{
_is_cleanup = false;
_data = new T(args...);
pthread_cleanup_push(_cleanup_func, (void*)_data);
}
/* Line 272 */template<typename T>
CancelableThreadData<T>::~CancelableThreadData()
{
if (!_is_cleanup) pthread_cleanup_pop(0);
delete _data;
}
Výstup gcc:
Thread.h: In constructor ‘BurnLib::CancelableThreadData<T>::CancelableThreadData(T_args ...)’: Thread.h:272:10: error: expected ‘while’ before ‘<’ token Thread.h:272:10: error: expected ‘(’ before ‘<’ token Thread.h:272:10: error: expected primary-expression before ‘<’ token Thread.h:272:20: error: expected nested-name-specifier before ‘T’ Thread.h:272:20: error: expected ‘(’ before ‘T’ Thread.h:272:20: error: expected ‘)’ before ‘T’ Thread.h:272:20: error: expected ‘;’ before ‘T’ Thread.h:272:21: error: expected unqualified-id before ‘>’ token
Řešení dotazu:
Tak ten manual zahodte a poridte si lepsi:
These functions may be implemented as macros. The application shall ensure that they appear as statements, and in pairs within the same lexical scope (that is, the pthread_cleanup_push() macro may be thought to expand to a token list whose first token is '{' with pthread_cleanup_pop() expanding to a token list whose last token is the corresponding '}' ).
. Nakonec pak skončim u assembleru, kde budu alespoň vědět, co se stane
Unlike some other implementations of threads, C++ destructors for automatic objects are allowed to run in a well defined and consistent manner when a thread is terminated.
....
When a thread terminates, the following occurs: If the thread was ended using pthread_exit(), pthread_cancel() or return from the thread start routine, then cancellation cleanup handlers and data destructors are run.
The thread is terminated. At the time that the thread is terminated, both C++ destructors for automatic objects and AS/400 cancel handlers run.
) mi řeknou, že to můžou být makra ...
Tiskni
Sdílej:
ISSN 1214-1267, (c) 1999-2007 Stickfish s.r.o.