A pthreads Tutorial

I’m going to use a simple program to illustrate the basic use of pthreads, and highlight some of the issues that you may run into when you’re creating your own threaded programs (for C++11 threads, see this post).

I want my program to print a message from each thread I create, to the console, in a different colour.

I’m going to build the program up in three steps, and talk about the issues we encounter on the way. I’m not going to go deeply into the pthreads calls – if you want to read about them in detail, there is an excellent break down and set of tutorials here.

Threads program, version 1 – faulty

Right, so here’s my first version of the program: threads1.cpp. Build it with:

g++ threads1.cpp -lpthread

Take a look, and I’ll talk through what I’ve done below.

Read More »



Function Templates In C++

We looked at a very straightforward class template last time, which should have given you the confidence to get started in creating your own template classes.

Now, to round off this introduction to C++ templates, we’ll just have a quick look at function templates, and talk a little more about the workings behind template source code.

As usual, we’ll start with the code, and then dissect it below in order to understand what is going on.

Here’s an example program:

Read More »



C++ Templates Made Easy

Templates.

Groan.

With their godawful syntax and impressive verbosity it’s no wonder we screw our faces up in distaste when we see them in code. ESPECIALLY when we’re debugging that code. Oh my. It’s enough to make you wish you were writing the software for musical birthday cards instead (does anyone actually do that?).

Anyway, I’ve wanted to talk about templates for a long time but the approach is a tricky one because even hearing the dreaded ‘T’ word is enough to make people run for the hills. I probably should have put that in angle brackets. The dreaded <T> word. Ha ha.

Anyway, here we go, let’s dive in together because there is safety in numbers.

Templates – made easy. I promise 🙂

Read More »



Using Multiple Workspaces In Eclipse

If you do a lot of development work for lots of different programs or projects, you could try grouping them into separate workspaces in Eclipse.

It’s really easy to do and is a nice way to keep different types of projects together.

As you probably know, the default workspace is called ‘workspace’ and lives under your home folder on Linux. If you want to use additional workspaces, the steps below cover everything you need to know about using multiple workspaces:

 

1. Create a new folder

In your home directory, you need to create a new folder, which will be the location of your new workspace. You can call this folder anything: Coursework or 3DProjects, for example.

Read More »