Library tutorials & articles

Worker Threads

Introduction

Worker threads are an elegant solution to a number of problems about concurrent processing; for example, the need to keep the GUI active while a computation is being performed. This essay addresses some of the issues of using worker threads. A companion essay talks about some techniques dealing with user-interface threads.

Why Worker Threads?

Consider a simple implementation of a program. For our purposes, the program has the task of inverting the color of every pixel of an image, just because we sometimes need a concrete example to illustrate techniques. For the sake of our example, we will assume that the image being processed is 10 megapixels of 24-bit color.

The GUI has a menu item or other means of saying "Invert it now". This calls the doInvert method on the view class:

void CMyView::doInvert()
    {
     for(int x=y = 0; y < image.height; y++)
          for(int x = 0; x < image.width; x++)
              changePixel(x, y);
    }

This is a perfectly correct program. It traverses all 10 megapixels, happily changing the pixels, until it completes. But it is not a good implementation.

Why not? Because the entire GUI is dead until the operation completes. This means that for some long duration, the user is forced to wait while the operation proceeds, and there is absolutely nothing that can be done about it. If the user decides that the transformation is bogus, and wants to stop it, well, tough. It is going to complete anyway.

Comments

  1. 29 Nov 2002 at 14:37

    First of all, please let me congratulate you on your articles.  They are great.


    I'm a newbie when it comes to C++.  I have dedicated most of my time to develop in VB5.


    My interest in C++ is currently being focused in creating helper dll's to use in VB.  Since VB can only use COM classes, I started up with the book "C++ for VB programmers".  It is awful!  BUT, it helped on my feet.  I am now reading ATL Developer's Guide by Tom Armstrong; chapter 2 was amazingly clear in explaining the basics of COM and how to create a COM dll from scratch, without using ATL.  As a note, I know nothing of MFC except the fact that creating COM dlls in it would require a huge dependency file (hence my decision of using ATL instead).


    Anyway, back to threads.  You encourage the use of member variables in a C++ class to store data that will be accessed by the worker thread.  Sounds pretty reasonable.  However, that doesn't seem to be a good practice in COM, because the "COM contract" specifies that the properties and methods of a COM class must only be accessed by the thread that created them.  So...

    Question:
    Can you enlight me on how to create a worker thread that allows me to access information of the thread that created the COM object?

    Take your Invert color example.  I want to have a modeless dialog box with a Cancel button.  This button would change the running flag to FALSE.  With COM, how can I pass this variable to the worker thread?


    Many thanks in advance.

  2. 01 Jan 1999 at 00:00

    This thread is for discussions of Worker Threads.

Leave a comment

Sign in or Join us (it's free).

Joseph M. Newcomer

Related podcasts

  • Concurrency Pt. 2

    Podcast (MP3): Download Hosts: Alexander Michael Guests: Recording venue: In this second part of our concurrency series Michael and Alexander talk about basic patterns for concurrent programming, such as Active and Monitor Object, Scoped Locking and ...

We'd love to hear what you think! Submit ideas or give us feedback