Help with threading?

I know that to create a thread, you call:
CreateThread()

Well... I don't exactly know where to go from there. I know you terminate a thread using ExitThread()... but I have no idea what to do in between. Also... bear with me, 1 more question... what exactly is a thread used for? Isn't it a small process or something?
EDIT: Can I also have an example of something... made simplistic?
Last edited on
dont know how to use threads, but i do know what they are used for. threads allow you to run two or more functions at the same time
Let me preface this by saying that multithreading in C++ is not a beginner topic. It is ripe with gotchas, pitfalls, and demands extreme care.


There are many different threading libs.

If you are using C++11, it has it's own (standard) threading lib (in the <thread> header) which is probably easier to use than most other libs. For that, you just create a std::thread object:

1
2
3
4
5
6
7
8
9
10
#include <thread>

void func()
{
}

int main()
{
   std::thread thd( func);
}


This will create a new thread, represented by our 'thd' object. A thread is another code path that runs alongside your main code path. So with this code... once the 'thd' object is created, code inside both 'main' and 'func' will be running simultaneously.

However, even though they are two separate code paths, they share memory and resources. So all variables/objects/etc are shared between threads. Changes made to a var in one thread will be visible in another thread.

But this is where it gets complicated. Due to lots of factors, updating variables doesn't necessarily update the variable immediately. And if two different threads are trying to access the same variable at the same time, very bad things happen.

Because of this, you need to be careful about which memory is being accessed by which threads and when. And if multiple threads need to share access to the same object, those accesses need to be guarded somehow (usually with a mutex).
Well, thank you for clearing that up... but unfortunately my version of MinGW does not support ALL of the C++11 features... only some. I do not feel like downloading a compiler that does (I heard GNU supports full C++11?) because I am just getting a new computer in a couple days (literally. My last part should deliver 2moro). For the mean time though... do you know how to thread without C++11? If not, can you tell me a compiler that supports it so I can download it on my new PC?
Wow this is so cool! I just figured out how to do it the way I am talking about. I don't know why but I find multitasking so awesome haha. Anyway, about my previous question... Any idea on a C++11 compiler? NOT VISUAL STUDIO. I don't like VS because it always prompts me 2 pay for it after 30 days... and frankly I do not have 400 dollars to choke up for an IDE.
MinGW does support C++11.
Just add -std=c++11 to the command line.
http://gcc.gnu.org/projects/cxx0x.html

Also, the express Visual Studio versions are free to use, without time limitations.
Last edited on
but unfortunately my version of MinGW does not support ALL of the C++11 features... only some.


If you want you can just upgrade to the latest. gcc should have had support for <thread> and related headers for a while now.

do you know how to thread without C++11?


If you're on Windows, WinAPI has it's own thread lib (ie: CreateThread and/or beginthread_ex). There's also boost and pthreads (both 3rd party libs you'd have to download).

Boost as the advantage of being nearly identical to C++11 threads (or rather..vice versa.. since C++11 was based on boost)

Whichever one you choose, I would check it's documentation for examples and tutorials. They all use the same concepts but differ slightly in the means why which to accomplish them.

Of the ones mentioned (other than C++11), I'd recommend boost. As it's the most "C++ like" of all of them. It uses RAII so it's fairly easy to use and is probably the easiest.

pthreads is a nightmare.

NOT VISUAL STUDIO. I don't like VS because it always prompts me 2 pay for it after 30 days... and frankly I do not have 400 dollars to choke up for an IDE.


Visual Studio Express is 100% free, has no ads, and never asks you to pay.

Though it might ask you to "register"... which basically amounts to sending them your name and maybe your email address (you can probably make up fake ones if you don't want to give them your real info... I don't think it matters). But do that once and it never bugs you again.
O really? I had gotten VS Express 2013 and it prompted me to buy the full version before continuing. And I did figure out how to thread without C++11, but thank you.
O really? I had gotten VS Express 2013 and it prompted me to buy the full version before continuing.


I'm still running 2012. But I've used 2008, 2010, and 2012 and have never had this issue. So unless they changed something in 2013....
I didnt like 2013 anyway... It was only about Windows 8 apps. The problem (other than required pay): It did not let me delete all of the add ons from my computer! I have over 5 gb on my computer of just libraries and files for VSE13. It wont let me get rid of them either! I downloaded Qt, and while not using the GUI editor, it still has a lot of things Code::Blocks does not. So my main IDE, even for console applications, is Qt.
@AceDawg45: Qt uses MinGW (Which, on compile-time, feature-wide, is equally as valid as Code::Blocks' MinGW [in fact, they are the same, maybe just a different version]).
To enable C++11 on QT, in your .pro file, add:
CONFIG += c++11

This will set everything up correctly.
Last edited on
afaik TDM-GCC build supports std::thread, try to take a look at it
So unless they changed something in 2013....
No, still doesn't expire.
Directing the user to change the IDE/compiler and use std::thread without knowing how multithreaded programs is a big mistake (I've seen many responsesof this kind on these forums).

Moving to std::thread will not automagically solve the @OP issue, is not the compiler problem here.

Here are a sample program taken from Microsoft documentation, it works in every windows compiler, does not matter how old it is:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
// crt_begthrdex.cpp
// compile with: /MT
#include <windows.h>
#include <stdio.h>
#include <process.h>

unsigned Counter; 
unsigned __stdcall SecondThreadFunc( void* pArguments )
{
    printf( "In second thread...\n" );

    while ( Counter < 1000000 )
        Counter++;

    _endthreadex( 0 );
    return 0;
} 

int main()
{ 
    HANDLE hThread;
    unsigned threadID;

    printf( "Creating second thread...\n" );

    // Create the second thread.
    hThread = (HANDLE)_beginthreadex( NULL, 0, &SecondThreadFunc, NULL, 0, &threadID );

    // Wait until second thread terminates. If you comment out the line
    // below, Counter will not be correct because the thread has not
    // terminated, and Counter most likely has not been incremented to
    // 1000000 yet.
    WaitForSingleObject( hThread, INFINITE );
    printf( "Counter should be 1000000; it is-> %d\n", Counter );
    // Destroy the thread object.
    CloseHandle( hThread );
}


http://msdn.microsoft.com/en-us/library/kdzttdcb.aspx

I hope you read for yourself what each function does and try to change it and see what happens.
Topic archived. No new replies allowed.