Hi, after searching this forum adequately (I think) I have decided to post this thread asking for an easy to understand resource that describes how to do multithreading on C++ in windows. The current ones I am finding out there on the web either
1. Simply don't work
or
2. Are too complicated for me to understand.

Please post any resource you have on multithreading in C++ in windows :D

>>2. Are too complicated for me to understand.
multithreading can be complicated. I use the win32 api functon CreateThread() to create a thread. Not all that complicated once you realize many of the paramters are left 0 :)

DWORD WINAPI ThreadProc(void * lpParameter)
{
    // This is the start of the new thread
}

DWORD dwThreadID = 0;

HANDLE hThread = CreateThread( 0, // Security
      0, // use default stack size
      ThreadProc, // new thread
      0, // parameter, you can pass a value if you wish
      0, // creation flags -- see MDSN for others
      &dwThreadID // Theread id to be filled in by CreateThread 
);

That is the code to create a thread. There other several complications you need to consider if both threads are to access the same data objects or use the same functions at the same time.

God I love you. I was able to make 2 simultaneous running threads so easily it is funny.

#include <iostream>
#include <windows.h>

using namespace std;

DWORD WINAPI ThreadProc(void * lpParameter)
{
    while(1)
    {
        cout << "Hi" << endl;
        Sleep(5000);
    }
    // This is the start of the new thread
}

DWORD WINAPI ThreadProc2(void * lpParameter)
{
    while(1)
    {
        cout << "Hello" << endl;
        Sleep(1000);
    }
    // This is the start of the new thread
}


DWORD dwThreadID = 0;
DWORD dwThreadID2 = 1;

HANDLE hThread = CreateThread( 0, // Security
      0, // use default stack size
      ThreadProc, // new thread
      0, // parameter, you can pass a value if you wish
      0, // creation flags -- see MDSN for others
      &dwThreadID // Theread id to be filled in by CreateThread 
);

HANDLE hThread2 = CreateThread( 0, // Security
      0, // use default stack size
      ThreadProc2, // new thread
      0, // parameter, you can pass a value if you wish
      0, // creation flags -- see MDSN for others
      &dwThreadID2 // Theread id to be filled in by CreateThread 
);

int main()
{
    cin.get();
    return 0;
}

I've noticed a lot of tutorials out there putting these things in classes in beginning thread tutorials, even though it just complicates the hell out of them, just to force the use of object oriented programming, thanks for making it simple.

God I love you. I was able to make 2 simultaneous running threads so easily it is funny.

Please deposit $1,000,000.00 USD in my PayPal account :) Glad I could help.

Oh, did I win the international Zimbabwe lottery, but I can only receive my winnings after putting a million in your account first?

Nah, I'll just add to your rep.

Your test program illustrates the need to synchronize the threads for use of cout. cout is not thread safe, meaning your program could lock up if you don't take precautions. There is an article I found that is only ONE way to solve the problem.

Indeed, that is what happened with the end lines in my code, I thought something was up with that, and assumed it was because the threads were taking turns to be processed.

I've decided to take one of those ideas in that nice article you posted, and modify it a bit so that I have a separate thread to manage output. It shall check a vector containing stream states in use by other threads and then stick em in cout if the state is in "ready" mode.

That may not work because two threads may still access the stream at the same time. Safer to use a sepaphore to lock the threads.

That may not work because two threads may still access the stream at the same time. Safer to use a sepaphore to lock the threads.

I don't think you understand, the other threads won't be accessing the stream, they will be accessing two new stream type things, and then setting a value in the vector to a ready state, which the third thread will read and then put the temporary stream type things into the cout stream. Which means writing to the cout stream will only be done by one thread.

>>Which means writing to the cout stream will only be done by one thread.
Not so.

They are both calling the same function but it is still in two different threads. It will be easier to synchonize the threads that way but they must still be sychronized. And they will also be accessing some ready state object, can't do that either without synchronization. The only safe way is via semiphore or critical section to lock entire functions.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.