Archive

Archive for November, 2019

C++ Equivalent of SpinWait.SpinUntil

November 30th, 2019 Comments off

I like to use threads in my code, but the problem with threads is that sometimes you need to wait for them to finish.

In c++ it is very easy to do.

All the code above does is, start a thread, call the function my_function and passes the argument 1.
The t.join(); tells the main thread to wait until the thread is finished.

Now the problem is that t.join() could hang forever … and I would never know about it.

What I needed was something like join_until( 1000) where we either wait for the thread to finish or wait for 1000ms. What I needed was something similar to c# SpinUntil

In very simple terms all SpinUntil does is check a condition, (very often), and if it is true then return (true), but if a timeout happens then we return (false).
So in my case I would wait for the thread to complete, (yes I know, I know, you can’t easily tell if a std::thread is complete or not.

Enter Wait.SpinUntil …

Basically we will

  1. Start a new thread
  2. Check if the given condition is true
    1. If the condition is true, return true
    2. If the condition is false … continue.
  3. Wait for a couple of nanoseconds for other threads to do their bit
  4. Look how long we have been spinning
    1. If less than milliseconds go back to number 2
    2. If more than milliseconds, return false.

The code

This is a very simplified version of the code, you can find a working solution on my github page

So now the code above will wait for a couple of ms while running in the background and if the given condition is true we will get out and return true, otherwise we will return false after a while.

Possible issues

  • For one thing, the condition() function could hang, it is up to the caller to make sure this never happens.
  • The condition() function could take a rather long time to execute … it is up to the caller to make sure this never happens

In other words, don’t stuff things up in your condition() function.

Categories: development Tags: , , ,

Add Google test directly to your Visual Studio project

November 9th, 2019 Comments off

Why?

While it is easy(ish) enough to add google test using various packages, you might want to add it directly to your project, (to debug your tests and so on).

I also found that packages are not always as often as you might want, so it might be because you need a new feature or something added to your test.

Before we start

– I am using Visual Studio 2019, but it should work fine with 2017 and even 2015
– I am using the latest Google Test (v1.10 at the time of writing this, but the principle is the same for almost all versions and should work fine for a few versions.

Getting started

  • Go to Google test on Github and get the latest release, make sure you get the ‘source code’
  • Don’t get it from anywhere else, you don’t know what has been changed and who changed it!
  • Remember that google test is written to a test application, not your main application!
  • Create a new console application, (File > New > Project …) and select “Console App”

Now add Google Test

  • In your project, create a new ‘folder’, I normally prefer to have it mirror what it looks like in my actual windows source code directory, but you could call it whatever you want, (like I did below).
  • Right-click on the ‘src’ folder and select “Add > Existing Items…”
  • Navigate to the folder you extracted, and find the ‘googletest > src’ folder
  • And then select ‘gtest_main.cc’ and ‘gtest-all.cc’, nothing else.
  • Finally, right-click your project and select “properties” and edit the “Additional Include Directories”
  • Add the directory relative to the location of your solution
    You could enter the full path if you want, (but that … blah), or you could use macros …
    Either way, make sure that the path is valid.
  • Make sure that the changes you make apply to all your build configuration.
    In the screenshot above I have x64 and Release but make sure that you choose all the configurations you will be using.
  • Compile your code … nothing funny should happen, (’cause you did nothing really).
    If you get an error, double check your paths and permissions.

Write and run a test

Last but not least, write a very simple test in your main and run a test

You do the rest

Now that a simple test is running, you can start creating tests for your own project.

Example?

Have a look at my directory watcher project and see my own tests.

As an aside, I am alway looking for comments, reviews and so on, please have a look at the project and comment!