Corrections

On September 15, 2009, in Uncategorized, by kris

I fixed some problems with the post about Boost threads. The example program worked correctly for the most part, but only by accident. I had incorrectly assumed that the join() method started the thread. This is not the case. It’s a method that blocks until the thread is done.

The correct solution would be to make a “barrier” which has one method, wait(). When a barrier is constructed, it is initalized with a counter. Each thread calls wait() when it’s done, decrementing the counter. When the counter reaches zero, all calls to wait() return. The example in the previous post about Boost threads now uses a barrier.

 

Getting Started with Boost Threads

On September 8, 2009, in Little Projects, by kris

Boost is collection of open source C++ libraries. They are released under the “Boost License” so they can be incorporated into open-source and closed-source projects. Anyway, one of the libraries in the collection that is of particular interest to me is the threading library. It’s cross-platform, so I should be able to run my code on any platform. It also uses proper C++ templating, so it’s clean as well.

This post is targeted to readers who already have some experience writing multi-threaded applications (in Java for example). This post tell you only what you need to go from nothing to compiling a simple Boost-based program that uses locks.

The first thing we have to do is get Boost. The threading library was last changed in version 1.36, so anything 1.36 and later will do. You can do a manual install by following the instructions in the Getting Started Guide. However, I use Ubuntu 9.04 which packages the Boost library, and I’m a huge advocate of using your distro’s package management system so I’ll be using that.

To get Boost in Ubuntu, run the following:

sudo apt-get install libboost1.37-dev
echo "That was easy!"

So now all that’s left is make a simple, multi-threaded application.

// File: sample.cpp
#include <iostream>
#include <boost/thread.hpp>

using namespace std;

class MyRunnable
{
public:

	MyRunnable(int id, boost::mutex* mutex, boost::barrier* bar)
	{
		this->id    = id;
		this->mutex = mutex;
		this->bar   = bar;
	}

	// The entry point for a thread
	void operator()()
	{
		for(int i = 0; i < 10; ++i)
		{
			boost::mutex::scoped_lock  lock(*mutex);
                       cout << "id: " << this->id << ", " << i << endl;
		}

		// all done, wait at the barrier.
                // wait() returns when everyone has met at the barrier
		bar->wait();
	}

private:
	int id;
	boost::mutex* mutex;
	boost::barrier* bar;

};

int main()
{
	boost::mutex io_mutex;
        // this barrier will wait for two invocations of wait()
	boost::barrier my_barrier(2); 

	cout << "Starting two counting threads..." << endl;
	// the boost::mutex cannot be copied (for obvious reasons)
	// so we must pass the pointer to the mutex.
	boost::thread thread1(MyRunnable(1, &io_mutex, &my_barrier));
	boost::thread thread2(MyRunnable(2, &io_mutex, &my_barrier));

	thread1.join(); // wait for thread1 to finish

	// Note how the program doesn't return until all threads are dead
	return 0;

}

Then compile and run!:

g++ sample.cpp -lboost_thread-mt
./a.out

The output will of course vary a lot each time you run it, but it should look something like this:

id: Starting two counting threads...
1, 0
id: 1, 1
id: 1, 2
id: 1, 3
id: 1, 4
id: 1, 5
id: 1, 6
id: 1, 7
id: 1, 8
id: 1, 9
id: 2, 0
id: 2, 1
id: 2, 2
id: 2, 3
id: 2, 4
id: 2, 5
id: 2, 6
id: 2, 7
id: 2, 8
id: 2, 9

Notice how the output of the two threads we created is interleaved with the output of the main thread of execution. This is one of the dangers of threading!

Tagged with:
 

Limiting Bandwidth in Linux

On July 12, 2009, in Uncategorized, by kris

Ever wanted to limit the bandwidth of a single command in Linux? It’s easy with trickle. You use it to launch the program that you want to restrict and it will provide a modified (restricted) version of sockets. No configuration… nothing.

Here’s how to install and use it to limit the bandwidth given to Firefox to 300 KB/sec on Ubuntu:

sudo apt-get install trickle
trickle -d 300 firefox

Ridiculously easy right?

Tagged with:
 

Sweet Bicycle Gear (LightLane)

On July 12, 2009, in Uncategorized, by kris

So, I ride a bicycle a lot and nothing feels quite as uncertain as riding in the dark with a dinky flashing light on your back as your only protection. This little guy (which is still just a prototype) draws lines on either side of you forming a “bike lane” around you. I guess the idea is that if there’s a line to be seen then drivers will obey it.

Here is the LightLane Website.

The video on the site is broken so you can view it here: http://www.youtube.com/watch?v=WOU563OvpUY

Unfortunately, it isn’t for purchase yet.

 

So now that I have a server on a static IP address, I decided it was time to lockdown sshd a little. I thought there was a simple option I could change in /etc/ssh/sshd_config to block repeated attempts. A few minutes of googling turned up nothing though. I did stumble upon a nifty application called DenyHosts. It basically watches your log files for repeated and failed attempts to login via ssh, and then after reaching a threshold, adds that IP to the hosts.deny file. Banning the user from any interaction with your server.

The whole things is pretty customizable. For instance, you can even set it up to email you about banned users or suspicious logins! Here’s a quickstart guide for Ubuntu:

We will use postfix, a very slim SMTP server to send mail.
sudo apt-get install deny-hosts
sudo apt-get install postfix

Now some configuration:
sudo vi /etc/denyhosts.conf
# Change the line "ADMIN_EMAIL = root@localhost" to
# ADMIN_EMAIL = your@emailaddres
sudo /etc/init.d/denyhosts restart

And now you’re done! It’s ridiculously easy to get going, it’s tweaking it to your liking that takes time. The default settings are pretty harsh, so you may want to lax them a bit. For instance, it considers 10 bad logins for an existant over the course of 5 days to be a ban-worthy offense and bans are never forgotten. The configuration file, /etc/denyhosts.config, is well documented so it’s a good place to start.

Tagged with: