Jerry's Tutorials: Simple Minded C++ Functions To Be Turned Into Threads

Jerry's Tutorials: Simple Minded C++ Functions To Be Turned Into Threads


The executable code for a thread in C++ is written as a C++ function.  From the point of view of C++ and the compiler, the function doesn't know that it's in a separate thread.  It just knows that it's a function.  You as the developer know that the function is going to execute in a separate thread, and you as the developer are responsible for such things as appropriate serialization of critical sections of code.  But the compiler is simply compiling functions.


#include "stdafx.h"
#include <iostream>

void funct1()
{
    for (int i = 0; i < 50; i++) std::cout << '?';
    std::cout << std::endl;
}

void funct2()
{
    for (int i = 0; i < 50; i++) std::cout << '!';
    std::cout << std::endl;
}

int main()  // simple program to call two simple functions
{	
    funct1();
    funct2();
    return 0;
}

Something like the code at the left is usually they way functions are written in examples for threading applications.  These functions are not threaded just yet, but as we move through the tutorial I will make changes so that the functions become threaded.

Actually, I don't like writing functions quite in this fashion, even trivial functions for examples, because they have what is usually called global scope or file scope.  Which is to say that they appear before the main function and in the same .cpp file as the main function.  I know that many programmers develop C++ code in this fashion, but I very much prefer to have each function in a separate .cpp file so that each function is a separate compilation unit.  By doing so, I only have to recompile that code which I have changed rather than having to recompile the entire program when I make a change.

As such, my code nearly always has function prototypes in front of the main function and the actual function definitions are in separate files.  For the purpose of this tutorial, I'm will include function prototypes as described and I will place the function definitions below the main functions.  See below for how this will look in my examples.

In addition, I nearly always place all the function prototypes in a single header files that is included at the beginning of each of my .cpp files.  That way, all of my functions provide the information to the compiler that is required to invoke any of the other functions.  But for this tutorial, I will just place the function prototypes in front of the main function.


#include "stdafx.h"
#include <iostream>

    // function prototypes before the main() function
    void funct1();
    void funct2();

int main()  // simple program to call two simple functions
{	
    funct1();
    funct2();
    return 0;
}

// function definitions after the main() function

void funct1()
{
    for (int i = 0; i < 50; i++) std::cout << '?';
    std::cout << std::endl;
}

void funct2()
{
    for (int i = 0; i < 50; i++) std::cout << '!';
    std::cout << std::endl;
}

So here is the way I will most typically write functions in this tutorial.  You can easily imagine that instead of the function definitions being below the main function, they are really in separate .cpp files.

Most programmers would probably include a namespace std; statement to avoid having to say things like std::cout, std::endl, etc.  However, I worry a great deal about namespace pollution and most of the time I write std:: explicitly each time rather than using a namespace statement.


Return to Jerry's Home Page

This page last edited on 26 Mar 2016.