Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / Languages / C++

Learning Poco: Getting Started with Threads

4.33/5 (3 votes)
13 Sep 2011CC (ASA 3U)1 min read 28.3K  
In this tutorial, we will write a very simple example for getting started with threads.

In this tutorial, we will write a very simple example for getting started with threads. Poco provides abstraction for threads of many platforms and systems. We first need to implement our own Runnable worker class, then create a Thread and start the worker.

Poco here stands for the POCO C++ libraries and is not to be confused with Plain Old CLR Object.

Create a file thread.cc with the following lines of code:

C++
#include <Poco/Runnable.h>
#include <Poco/Thread.h>
#include <iostream>

using namespace Poco;
using namespace std;

class MyWorker : public Runnable
{
public:
  MyWorker(int k = -1) : Runnable(), n(k) {}

  virtual void run()
  {
    for (int i = 1; i <= 10; i++)
    {
      for (int j = 0; j < 1000000; j++) ; // do nothing
      cout << n << " ";
    }
  }

private:
  int n;
};

int main()
{
  const int N = 5;

  MyWorker w[N];
  for (int i = 0; i < N; i++) w[i] = MyWorker(i);

  Thread t[N];
  for (int i = 0; i < N; i++) t[i].start(w[i]);
  for (int i = 0; i < N; i++) t[i].join();  // wait for all threads to end

  cout << endl << "Threads joined" << endl;

  return 0;
}

Compile it and run multiple times:

$ g++ -o thread thread.cc -lPocoFoundation
$ ./thread
1 1 2 1 0 1 3 1 1 1 1 0 1 2 4 0 1 2 0 3 4 0 4 0 2 0 3 0 0 2 0 3 4 4 2 4 3 4 3 4 2 4 2 4 3 2 3 2 3 3 
Threads joined
$ ./thread
4 1 4 0 4 4 4 4 4 4 4 3 4 2 1 2 0 3 0 1 1 2 3 1 3 0 2 0 2 0 1 3 1 3 1 2 0 2 0 3 0 3 1 2 1 3 0 2 3 2 
Threads joined
$ ./thread
2 1 1 1 0 1 1 1 1 2 1 1 1 3 0 4 2 4 2 0 0 3 0 4 2 4 2 3 0 3 2 3 0 4 0 4 2 3 2 4 3 2 0 4 0 4 3 4 3 3 
Threads joined

Run the program several times and you will see it yields different sequences, because of the non-determinism of threads.

Also, there are somethings to note. Firstly, we have a do-nothing loop here in the worker because we do not have much work to do in the method. If that loop is eliminated, it will be difficult to see whether threads are working according to the program output. For example, after commenting out that line of loop, the program will always output something like:

$ ./thread
0 0 0 0 0 0 0 0 0 0 1 1 1 1 1 1 1 1 1 1 2 2 2 2 2 2 2 2 2 2 4 4 4 4 4 4 4 4 4 4 3 3 3 3 3 3 3 3 3 3 
Threads joined
$ ./thread
0 0 0 0 0 0 0 0 0 0 1 1 1 1 1 1 1 1 1 1 2 2 2 2 2 2 2 2 2 2 3 3 3 3 3 3 3 3 3 3 4 4 4 4 4 4 4 4 4 4 
Threads joined
$ ./thread
0 0 0 0 0 0 0 0 0 0 2 2 2 2 2 2 2 2 2 1 1 1 1 1 1 1 1 1 1 2 4 4 4 4 4 4 4 4 4 4 3 3 3 3 3 3 3 3 3 3 
Threads joined

According to these outputs, threads are never switched out to other threads and go back. Of course, these situations are OK, but they’re just not suitable for tutorials. :–)

Secondly, we join all the threads at the end of the program. If we do not join them, the main program will exit without considering unfinished tasks of the threads, and we will possibly see no number sequences printed out. Again, this is for tutorials.

Hope this article will give you a basic impression of what threads are in Poco and how to work with them. We will come back to the threads and make use of them in much more interesting applications in the future.

Related Articles

License

This article, along with any associated source code and files, is licensed under The Creative Commons Attribution-Share Alike 3.0 Unported License