Threads

I want to learn about threads, but I am having a hard time understanding how threads work. Anyone know any good resources for it? I've tried searching on google and all the sites I found had "simple" explanations which were to vague.

My understanding is threads are different than what you would do in what is called a game loop. I have written a 2d space ship game in good'ol Quickbasic, so I how you multi-task, but not where threads come into the picture.

-Thanks
I use threads when I have a scheduled program and I can't afford to loose time to a bulky operation such as copying a file. Therefore I put that operation in a thread. This makes the threaded code run in parallel to my main code.

The following code is a simple method in windows to run a thread.
1
2
3
4
5
6
7
8
9
10
11
12
13
#include <windows.h>
#include <iostream>

DWORD threadedFunction()
{
    std::cout << "Hello World\n";
}

int main()
{
    CreateThread(NULL, 0, threadedFunction,NULL, 0, NULL);
    std::cout << "HelloWorld\n";
}
Well, you have shed some light on the subject for me, and things make more sense now. However, I'd like to know more. You used windows.h header for your function, and I know there's a boost library you can use also.

I'd like to know more about threads, well enough to write my own thread functions. I just want that level of understanding. Also, I won't to look from a platform independent angle.

Indeed, boost threads are the most platform-independent solution today. C++ also supports threads directly, although today there are more compilers that support boost.threads than there are compilers that support C++ threads.

I'll use C++ here (but switching to boost is trivial in these examples, just replace std:: with boost:: and switch the header file)

Here's a quick example: start 3 threads and let them print some stuff for a bit.

1
2
3
4
5
6
7
8
9
10
11
12
13
#include <thread>
#include <iostream>
void f(int id)
{
    for(int n=0; n<10000; ++n)
        std::cout << "Output from thread " << id << '\n';
}
int main()
{
    std::thread t1(f, 1); // launch thread executiong function f() with argument 1
    std::thread t2(f, 2), t3(f, 3); // two more threads, also executing f()
    t1.join(); t2.join(); t3.join(); // wait for all three threads to finish before ending main()
}

if you run it, you may see output such as

Output from thread Output from thread 2
Output from thread 2
Output from thread 12


That happens because each operator<< prints to the same global object std::cout: thread 1 printed "Output from thread", then before it was able to print "1", thread 2 managed to print "Output from thread 2" twice, and another "Output from thread". Then thread 1 finally got to squeeze out its "1", and thread 2 finished its "2".

To solve such issues, mutual exclusion may be needed:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <thread>
#include <iostream>
static std::mutex m; // global mutex object, visible from f() in all threads
void f(int id)
{
    for(int n=0; n<10000; ++n)
    {
        std::lock_guard<std::mutex> lk(m); // acquire lock or wait until it is released
        std::cout << "Output from thread " << id << '\n';
    } // release lock
}
int main()
{
    std::thread t1(f, 1), t2(f, 2), t3(f, 3);
    t1.join(); t2.join(); t3.join();
}


Now the output is exactly 30000 lines, with no mix-ups, something like

Output from thread 1
Output from thread 2
Output from thread 2
Output from thread 3
Output from thread 1
Output from thread 3


There are many more ways to synchronize and communicate between threads, there are books written on the topic.
Hey, that's much easier than the WINAPI version!

Edit: Nevermind, I don't have <thread> installed. I may need to expand my installed SDKs or install boost.
Last edited on
I'll have to give those a whirl. I never realized there was a thread C++ class. Thanks!
I tried to give your code a test run and I get this error:

error: 'thread' is not a member of 'std'

I am using the win version of the gnu c++ compiler, if that helps. I read it didn't support <thread> but then I read it did. So I don't know what the deal is.
You need a recent version of GCC and you need to use the compiler flag -std=c++0x and -pthread

Here is a recent version of GCC for windows
http://code.google.com/p/mingw-builds/downloads/detail?name=mingw32-gcc-4.7.0-snapshot-20110815.7z
Last edited on
There's quite a lot of information on threads throught the forum, but it can be tricky to find.

Pre-emptive threads are a facility offered by the OS. This basic facility is different on different kinds of OS', so are encapsulated in a C++ class, boost being the most popular. The most recent C++ standard, C++ 11, includes thread support in the standard library, but the language is not generally available yet.

The example above uses the new standard library, but most of us won't have this available in a production compiler.

There are some Microsoft C++/Windows links here, and like I said, there's a lot more throughout the forum.
http://www.cplusplus.com/forum/windows/39507/#msg213258
Topic archived. No new replies allowed.