Windows 8: Writing Multi-threaded Applications for the Windows Store Using Intel® Threading Building Blocks

  • Tutorial
As you know, the program interface of applications for the Windows Store (Windows Store apps) lacks many functions for working with threads, starting with CreateThread () and ending with working with TLS keys. And this is a great reason to switch from concurrency based on system-dependent flows to concurrency based on tasks. This post provides step-by-step instructions on how to write a simple multi-threaded example that passes certification for the Windows store (Windows App Certification Kit validation) and, hypothetically, can be scaled to toys of a cosmic scale. And since the cross-platform library Intel Threading Building Blocks (Intel TBB, TBB, threadingbuildingblocks.org), then the computing part can be easily transferred to other platforms, and the task will only be to draw a new beautiful graphical interface.

The recently released Intel TBB library tbb41_20121112oss, which is available for download from our threadingbuildingblocks.org website , added experimental application support for the Windows store, i.e. using only an authorized programming interface for developing such applications.
And so, what needs to be done to build a simple application using Intel TBB.
First you need to unzip and compile the library, since this release is distributed only in the source. It is understood that there is gnu make, on the command line for studio 2012 we execute the command

gnumake tbb tbbmalloc target_ui=win8ui target_ui_mode=production

From the library, everything, go to the studio.
We create a new project “Blank App (XAML)” using the standard template “Visual C ++” -> “Windows Store”. For simplicity, let us leave the default project name “App1”.
Now add the directory <tbb directory> / include to the project property “Additional Include Directories” and the directory with the built-in library tbb.lib in the “Additional Library Directories”.
Then add a couple of buttons to the main page (App1.MainPage Class). After that, the XAML page file will look something like this



By the way, before connecting the library, it would be nice to check that up to this point we had no time to mess up in the code, and there are all the necessary rights and licenses of the developer. To do this, you need to build and run the application, and check that the buttons are pressed and nothing falls. If the result is positive, we proceed to connecting the library.

We connect TBB and add button click handlers. For example, we take the reduction algorithms (tbb :: parallel_reduce) and the determined reduction (tbb :: parallel_deterministic_reduce) and add the MainPage.xaml.cpp to the source file for the main page:
#include "tbb/tbb.h"
void App1::MainPage::SR_Click(Platform::Object^ sender, Windows::UI::Xaml::RoutedEventArgs^ e)
{
    int N=1000000;
    float fr = 1.0f/(float)N;
    float sum = tbb::parallel_reduce(
        tbb::blocked_range(0,N), 0.0f,
        [=](const tbb::blocked_range& r, float sum)->float {
            for( int i=r.begin(); i!=r.end(); ++i )
                sum += fr;
            return sum;
    },
        []( float x, float y )->float {
            return x+y;
    }
    );	
    SR->Content="Press to run Simple Reduction\nThe result is " + sum.ToString();
}
void App1::MainPage::DR_Click(Platform::Object^ sender, Windows::UI::Xaml::RoutedEventArgs^ e)
{
    int N=1000000;
    float fr = 1.0f/(float)N;
    float sum = tbb::parallel_deterministic_reduce(
        tbb::blocked_range(0,N), 0.0f,
        [=](const tbb::blocked_range& r, float sum)->float {
            for( int i=r.begin(); i!=r.end(); ++i )
                sum += fr;
            return sum;
    },
        []( float x, float y )->float {
            return x+y;
    }
    );	
    DR->Content="Press to run Deterministic Reduction\nThe result is " + sum.ToString();
}

And the XAML file for the main page will look like this


Add the tbb.dll and tbbmalloc.dll libraries to the application container. To do this, add files to the project (via project-> add existing item) and set the “Content” property to “Yes”. In this case, the files will be copied to the container (AppX) inside the application and can be loaded both at application startup and later.
Done. You can run the simulator and watch how the application works:

The next step is to launch the “Windows App Cert Kit” and verify that the application successfully passes the certification:


That's all! A simple application is ready, now you can complicate things.

For those interested, try:

Download the Intel Threading Building Blocks library (Open Source Version):
threadingbuildingblocks.org
Commercial version of Intel TBB (functionally no different):
software.intel.com/en-us/intel-tbb

English and Russian blogs about Intel TBB
software.intel.com/en-us/tags/17207
software.intel.com/en -us / tags / 17220
And, of course, our forum,
software.intel.com/en-us/forums/intel-threading-building-blocks

Also popular now: