go to start Ex W11
|home |print view |recent changes |changed May 19, 2017 |
exact
|You are 54.81.139.56 <- set your identity!

Sections: Threading in C++ on Windows | Extended RAII Wrapper | Experiment Memory Order | Peterson Mutex | TESTAT-3: BoundedQueue with timed operations | Extra (Not part of the testat): Lock-free implementation of BoundedQueue |

Threading in C++ on Windows ^

'''Iff you encounter problems with threads not being supported by your compiler:!!!

The MinGW-w64 compiler should work if you install it with posix threading model: https://sourceforge.net/projects/mingw-w64/

NOTE: As of today, Windows Users should rely on the Linux VM, because neither Cygwin nor MinGW support threads on Windows. You might attempt it, if you have a very recent Microsoft's C++ installed and switch your Eclipse project to the corresponding tool chain.

Or alternatively, you can try the headers from https://github.com/meganz/mingw-std-threads

Extended RAII Wrapper ^

This is a repetition exercise for variadic forwarding templates. In the lecture you have seen Anythony Williams' suggestion for a scoped_thread. The wrapper is always constructed from an rvalue thread. Wouldn't it be nice to be able to create the scoped_thread from a function and the call arguments directly?

scoped_thread st{<function>, [<arguments>]};

Instead of

scoped_thread st{std::thread{<function>, [<arguments>]}};

Experiment Memory Order ^

Take the example code https:files/AtomicCountersRelaxed.cpp of the lecture and experiment with it. Run it with memory_order_seq_cst several times and compare the output to what happens with memory_order_relaxed

Unfortunately the code itself can not really demonstrate that sequential consistency is violated, because thread switches can happen between the three atomic loads of the triple read.

Peterson Mutex ^

From the module Parallel Programming you should be familiar with the Peterson Mutex. If you need to recap it you can find it on the Skripte server here.

Below you have the implementation ported to C++ using volatile. While this works in Java, it is incorrect in C++.

struct PetersonMutexVolatile {
    // acquire lock by thread 0
    void thread0Lock() {
        state0 = true;
        turn = 1;
        while (turn == 1 && state1);
    }

    void thread0Unlock() {
        state0 = false;
    }

    // acquire lock by thread 1
    void thread1Lock() {
        state1 = true;
        turn = 0;
        while (turn == 0 && state0);
    }

    // release lock by thread 1
    void thread1Unlock() {
        state1 = false;
    }
private:
    volatile bool state0 { false };
    volatile bool state1 { false };
    volatile int turn { 0 };
};

TESTAT-3: BoundedQueue with timed operations ^

Due date: Friday 19. May 2017 23:59 (CEST)

Subject: [C++A-testat-3] hsrname1 hsrname2 hsrname3

Write a BoundedQueue<T> template class that provides the functions

Use the mechanisms from your BoundedBuffer to allow move-only and non-default-constructible template argument types. You can also copy parts of the code and add the required synchronization.

Anthony Williams gives the following advice for testing multi-threaded bounded queue:

Use the mechanisms from your BoundedBuffer to allow move-only and non-default-constructible template argument types. You can also copy parts of the code and add the required synchronization.

Tests provided: https:files/BoundedQueueTestsW11.zip

or on github

Please also answer the following questions when handing in your header file:

  1. Why can't we use the front() and back() member functions from BoundedBuffer?
  2. Why doesn't it make sense to provide iterators for BoundedQueue?
    • Can you suggest an alternative means for observing the BoundedQueue content?
  3. Why is pop() returning a value by value and not void as in BoundedBuffer?

Extra (Not part of the testat): Lock-free implementation of BoundedQueue ^

If you are eager, try to figure out, if there is the possibility to implement a BoundedQueue just relying on atomics for synchronization. Be aware of the ABA problem. Would that be faster?


|home |print view |recent changes |changed May 19, 2017 |
exact
|You are 54.81.139.56 <- set your identity!

Ex W11
go to start