Tag Archives: cpp

Quickstart for cross-platform c++ projects

A typical dilemma for a c++ developer is creating the initial build configuration. Out of my affection for Lua, I’ve collected my typical premake4 patterns into a separate project to be able to set up c++ projects on any platform within a minute.

Here’s a sample from selfdestructing:

_G.package.path=_G.package.path..[[;./?.lua;./?/?.lua]]
assert ( require 'premake.quickstart' )

make_solution 'selfdestructing'

includedirs { 
	'./selfdestructing',
	'./Catch/single_include'
}

make_console_app( 'selfdestructing-test', { './test/test.cpp' } )
make_cpp11()
run_target_after_build()

Self-destructing objects

One day, recalling the programmed cell death I decided to create class that would self-destruct on a certain amount of copying.

Today’s little research resulted in the following project: https://github.com/d-led/selfdestructing

The question arises, what exactly copying stands for. I could think of four policies. The implementation of those policies is configurable and extensible. Right now there is no explicitly thread-safe policy implemented, but those would not be very complicated, if the purpose is debugging solely.

Usage

#include "crash_on_copy.hpp"
struct TestNumberCrash : public crashes::on<3>::copies {};

on_copiesCrashes on 3 total copies of the originally created object, but doesn’t crash on 3 total instances of the class

struct TestCopyNrCrash : public crashes::after<3>::copies {};

after_copiesCrashes on any third copy of the original object, but doesn’t crash on 3 total instances of the class

struct TestTotalNrCrash : public crashes::on_total<3,TestTotalNrCrash>::instances {};

on_total_instancesCrashes on instantiation of an object if 2 objects are alive, but doesn’t crash on any creation if the total amount of instances is below 2

struct TestAfterTotalNrCrash : public crashes::after_total<3,TestAfterTotalNrCrash>::instances {};

after_total_instancesCrashes after a third object instantiation of the class

Singular form aliases are also available, i.e. crashes::on<1>::copy.

The project at github contains the test showing the crashing pattern.

A Background Ticker, now in C++ with PPL

Unfortunately, the Reactive Extensions for C++ (Rx++) did not work for me as smoothly as their C# counterpart. My goal is to recreate the background ticker from the Rx version. The first, and perhaps not the last example implementation will be using the Parallel Patterns Library by Microsoft, which uses the Concurrency Runtime. The goal is to write a code that abstracts the threads away, and uses some background scheduler to schedule calls to functions in an asynchronous manner.

Here, asynchronous calls will be done by tickers, one of which will poll a value from a bogus frequency meter and write it to the console, whereas the other one will just write “tick”.

First, the frequency meter that gives random values around 62 (a magic number):

#include <mutex>
#include <random>
class FrequencyMeter
{
	mutable std::mt19937 gen;
	std::uniform_int_distribution<int> dis;
	mutable std::mutex m;
public:
	FrequencyMeter() :
		dis(0,3),
		gen(std::random_device()())
	{}

	int Hz() const
	{
		std::lock_guard<std::mutex> lock(m);
		return 60+dis(gen);
	}
};

This class is not the primary concern here, but if the Hz value is accessed from multiple threads, the access should perhaps be serialized (by a mutex here).

Now, one of the side wishes for the ticker is the ability to start and stop it any time. The ticker should also not care what it executes, hence we can take an std::function for now, or perhaps anything callable. Howerver, having c++11 lambdas, it’s easy to convert to standard function (thread-safety and ownership issues notwithstanding yet). Following the examples for cancellation tokens, one can write a small wrapper that will tick at predefined intervals (see full code), wrapping a simple loop:

while (running)
{
	// Check for cancellation. 
	if (concurrency::is_task_cancellation_requested())
	{
		running = false;
		concurrency::cancel_current_task();
	}
	else
	{
		this->tick_once();
		concurrency::wait(interval);						
	}
}

In the small test program, the frequency meter is instantiated and the first ticker is started, ticking once a quarter of a second:

FrequencyMeter FM;
active_ticker measure([&FM]{std::cout<<FM.Hz()<<std::endl;});
measure.start(250);

Then another ticker is started:

active_ticker ticker([]{std::cout<<"tick"<<std::endl;});
ticker.start(500);

And some wait, start and stop logic:

wait(2000); //see the output for a couple of seconds
measure.stop(); //stop the frequency meter
wait(2000); //wait some more
ticker.stop(); //stop ticking
measure.start(250); //start measuring again
wait(2000); //and wait a bit more
measure.stop();

The output looks like this:

60
tick
61
62
tick
61
62
tick
60
60
Canceling measurement ...
tick
60
tick
tick
tick
Restarting measurement ...
tick
60
63
62
61
62
62
62
Done

Any other technology to try elegant ticking (tinkering) with? TBB, zeromq in process?