Sunday, October 20, 2013

A lesson in philosophy

It has been a while since there has been any forward progress made, mostly due to class eating up all of my time.  In order to not feel like the time has been completely wasted, I thought I'd make a post about a recent assignment I completed.

The assignment was to implement a solution to the famous dining philosopher's problem.  The problem generally requires students to use various tools such as semaphores and threads that, historically, have not been available in a simple cross platform way unless certain specialized libraries have been added.  Enter the C++11 standard, and you have the opportunity to finally write a cross platform solution with nothing but the STL.

The dining philosophers problem turns out to be one of the problems where it is beneficial to think of each thread as a class - after all, each thread represents a philosopher.  This solution called for a classic runnable class model which I implemented entirely in a header file out of a combination of laziness and ease of use (the latter reason is debatable).

The runnable class
A few points about the runnable class: first - visual studio hasn't yet implemented the delete operator so we still have to work to make runnable noncopyable the old way.  I expect this will change in the near future, so the preprocessor guards will come out eventually.  Everything else about this class is fairly straightforward - implementors are encouraged to use the m_stop variable or the stopped method inside their loops in order to respect the stop command, and I'm not sure if more can (or even should) be done on this front, though I am wary of relying on the people using the class to ensure most of the methods the base class itself defines are useful.

The magic of the solution occurs in the philosopher class.  C++11 provides a lock function that guarantees it is deadlock free.  A simple solution (modified Dijkstra's solution) is then presented using this mechanism for philosophers to request their chopsticks.

A simplified view of the philosopher class

C++11 provides a couple of interesting locking mechanics for a mutex, but the appropriate mechanism for a philosopher is a unique lock, since only one philosopher may hold a chopstick at any given time and the lock must hold even after the method that locks the resource (get_resource) has completed. The second argument to the unique_lock constructor tells it not to immediately grab the lock, since that will be done in the thread's run loop.

And that is really all there is to it.  Unfortunately, the current solution does nothing to address possible thread starvation.  In order to truly do that, a conductor would be needed (or in this case, a waiter).  The solution to that problem, however, will have to wait for another day.  Until then, you can click here to view the git repo for this project.

Saturday, August 24, 2013

Adventures in reinvention

If at first it seems right, that is no reason we can't try again, right?

It has been a while since I've made any forward momentum on the RDA, but I'd like this post to be the first step in picking things back up.  And since this is a new location for the development blog, I'll give some history - after all, that is the purpose of this blog.

From the beginning of this project I felt like I needed an outlet to speak about the development of the RDA  - design decisions and technical choices about the project are so numerous that I'd really like a way to keep track of them for myself even if no one else really reads this.  The plan was to post reasoning, updates, and even code snippets so that this would be a truly open project.  I do enjoy a bit of web design every now and then so I went ahead and made a GitHub page with the intention of keeping track of everything there.  It wasn't a bad system, but I always felt like it could be made better and never had any time to do so myself.

After some though, I realized that I should really stop trying to reinvent the wheel here and just start a blog on Blogger (which made sense since I've already set up the RDA dev email address).  So here we are - this should help me focus on finishing something resembling a product and encourage me to make more posts since updating the site will no longer be such a hassle.  I'll leave the old site up for historical reasons, but I'll be working on back porting the posts from it to here, so I won't have to jump back and forth.  Eventually, I will probably convert the GitHub site into something more static with a link to this blog.  I'll mark posts from the past and try to provide more information in the new spirit of this blog.

Now on to features: the RDA has a solid networking setup with Twisted and AMP currently, but what it is lacking is the ability to separate users into chat channels.  After some waffling, I'll be using a publish subscribe paradigm for the channels.

The bulk of the code will reside in the chat handler on the server.  For now, I'll focus on simply providing the ability to split users into channels - I'll provide the functionality to manage the channels later when I work on the concept of a campaign.

Sunday, January 27, 2013

Code conformity

Post from the past

Got to... style all over... python

Not much to this update - sorted out a few mistakes in git and finally purged all legacy urls. Submodule should be working correctly now. Overall this was mostly prep work for getting back into the swing of things.

Notes from the future

I had seriously broken everything and was quite happy when I made the corrections here.  As the post notes, the submodule structure was very wrong.  This was due to the fact that I had copied over a lot of the data (poorly) from the original BitBucket repo that house it.  As I will explain in an older post, I had chosen BitBucket mostly out of cowardice.  I wanted the ability to keep the repo private, but I eventually relented and moved to GitHub to run the original blog.  Half baked ideas from yours truly?  Say it ain't so!