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

Sections: Finish the Testat Exercise | Catch up with the other exercises from last week | Own Hourglass/PIMPL class | Reusable Libraries Talk (Felix Morgner) | Preparation for GUI Exercises (W13) | extra: SCC99 |

Finish the Testat Exercise ^

Testat Exercise update

Here are some updated tests: https:files/BoundedQueueTestsW12.zip

Catch up with the other exercises from last week ^

Own Hourglass/PIMPL class ^

Take a non-template class of your favorite software and attempt to put it into a library employing a stable ABI using the PIMPL approach.

If non-existent, write test cases first (CUTE library test project), to approve your coding exercise.

Depending on the copyabiltiy of the original class select form the three versions:

If done, also try to do a similar approach with an Hourglass Design.

Reusable Libraries Talk (Felix Morgner) ^

If you would like to have a specific example to work on, you can use the code from Felix Morgner's talk about reusable libraries [here https://github.com/fmorgner/building-reusable-libraries].

Preparation for GUI Exercises (W13) ^

Try to get the hello world example of wxWidgets running: http://docs.wxwidgets.org/trunk/overview_helloworld.html

You will need the wxWidgets libraries (maybe compile them yourself).

extra: SCC99 ^

Recently on the accu-general mailing list/cvu magazine the following problem was posted. Can you help the student and analyze her/his code and correct the bugs?

Submissions to scc@accu.org by Jun 1st)

"I wanted to learn a bit about C++ threading to I tried writing a thread pool example. But it sometimes crashes -- I've managed to get it down to a small example. Sometimes I get what I expected as output, for example:

Worker done
Worker done
Ending thread #2
Ending thread #0
Worker done
Ending thread #1
Worker done
Ending thread #3
Worker done
All done
But other times I get a failure, for example:
Worker done
Ending thread #0
Worker done
Awaiting thread #1
Worker done
W
<crash>

I'm not sure what to do next - can you help?"

#include <algorithm>
using namespace std;
#include <array>
#include <chrono>
using namespace chrono;
#include <cstdlib>
#include <iostream>
#include <thread>

static const int POOL_SIZE = 4;

// Allow up to 4 active threads
array<thread, POOL_SIZE> pool;

// Example 'worker' -- would in practice
// perform some, potentially slow, calculation
void worker()
{
  this_thread::sleep_for(
    milliseconds(rand() % 1000));


  cout << "Worker done\n";
}

// Launch the thread functoid 't' in a new
// thread, if there's room for one
template <typename T>
bool launch(T t)
{
  auto it = find_if(pool.begin(), pool.end(),
    [](thread const &thr)
    { return thr.get_id() == thread::id(); }
  );
  if (it == pool.end())
  {
    // everyone is busy
    return false;
  }
  *it = thread([=]()
  {
    t();
    thread self;
    swap(*it, self);
    self.detach();
    cout << "Ending thread #"
      << (it - pool.begin()) << "\n";
  });
  return true;
}

int main()
{
  while (launch(worker))
  {}

  // And run one in this thread
  // as an example of what we do
  // when the pool is full
  worker();

  for (auto & it : pool)
  {
    thread thread;
    swap(thread, it);
    if (thread.joinable())
    {
      cout << "Awaiting thread #"
        << (&it - &*pool.begin()) << "\n";
      thread.join();
    }
  }
  cout << "All done\n";
}


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

Ex W12
go to start