LUMIERA.clone/tests/lib/thread-wrapper-autonomous-test.cpp
Ichthyostega a0d83b4fe8 Library: investigate design for an opaque inline functor
This is a design study to find out what would be required
to build an opaque function wrapper, similar to `std::function`,
yet without using any heap memory ever.

Such a building block would allow for some interesting options
for a well structured design of elaborate data structures.

This changeset provides an implementation draft.

__Two important findings to note__:
 * it is impossible to get away with just a single function pointer
 * because at the very least we need to invoke the destructor of the
   embedded delegate. Thus something akin to a "manager" is required.
 * this implies that the component would not be so efficient as I hoped,
   since it requires two pointers and requires to emit code for each
   type of delegate.
 * by making the component **NonCopyable**, I avoided the complexities
   of virtual copy support. In case the latter is desired, it would be
   much simpler to switch to a classical OO approach with a VTable
   and use our `PolymorphicValue` wrapper to handle copy and cloning.
2026-04-08 01:53:53 +02:00

106 lines
2.9 KiB
C++
Raw Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

/*
ThreadWrapperAutonomous(Test) - launching a self-contained and completely detached thread
Copyright (C)
2023, Hermann Vosseler <Ichthyostega@web.de>
  **Lumiera** is free software; you can redistribute it and/or modify it
  under the terms of the GNU General Public License as published by the
  Free Software Foundation; either version 2 of the License, or (at your
  option) any later version. See the file COPYING for further details.
* *****************************************************************/
/** @file thread-wrapper-autonomous-test.cpp
** unit test \ref ThreadWrapperAutonomous_test
*/
#include "test/run.hpp"
#include "test/tracking-dummy.hpp"
#include "lib/thread.hpp"
#include <atomic>
#include <chrono>
using test::Test;
using test::Dummy;
using std::atomic_bool;
using std::this_thread::sleep_for;
using namespace std::chrono_literals;
namespace lib {
namespace test{
/*******************************************************************//**
* @test a variation of the Thread wrapper to launch a detached thread,
* with automatic memory management for the _thread-object._.
* @see thread.hpp
* @see ThreadWrapper_test
*/
class ThreadWrapperAutonomous_test : public Test
{
virtual void
run (Arg)
{
demonstrateSimpleUsage();
verifyMemoryManagement();
}
/** @test demonstrate simply launching a λ-function into background */
void
demonstrateSimpleUsage()
{
atomic_bool didRun{false};
launchDetached ("anarchy", [&]{ didRun = true; });
sleep_for(1ms);
CHECK (didRun); // verify the effect has taken place
}
/**
* @test verify the detached thread autonomously manages its memory.
*/
void
verifyMemoryManagement()
{
struct TestThread
: ThreadHookable
{
using ThreadHookable::ThreadHookable;
Dummy watcher;
void
doIt (int extra)
{
watcher.setID (extra);
sleep_for (5ms);
}
};
// Note the Dummy member allows to watch instance lifecycle
CHECK (0 == Dummy::checksum());
launchDetached<TestThread> (&TestThread::doIt, 55);
CHECK (0 < Dummy::checksum());
sleep_for (1ms);
CHECK (55 == Dummy::checksum());
sleep_for (10ms);
CHECK (0 == Dummy::checksum());
}
};
/** Register this test class... */
LAUNCHER (ThreadWrapperAutonomous_test, "function common");
}} // namespace lib::test