learn multithreading

This is a discussion on learn multithreading within the Windows Programming forums, part of the Platform Specific Boards category; I want to learn multithreading programming.
I know some basic thing about what processes and threads are,but didn't make any ...

learn multithreading

I want to learn multithreading programming.
I know some basic thing about what processes and threads are,but didn't make any program. If someone can suggest some good tutorial with examples suitable for beginer with explanations.
I read from msdn about CreateThread(...), CreateProcess(...) but I need to see some simple programs to gain real understanding. I found a lot of code on the internet, but there are a lot of complicated code, so I need a tutorial. Do you know some good links or has some recommendations?

First I'm not sure what is purpose of function WaitForMultipleObjects().
If I remove comments I get the following output:

Code:

This is thread 1This is thread 2 And This...
Press any key to continue

As you can notice that one endl is simply ignored, and operator << is some kind of signal first is displayed text to << in thread1() and then second function thread2() is executed, and again thread1() is executed. What is really going on here I'm not sure. Maybe this output will be different on your machine.
I have read in MSDN this:

Code:

The WaitForMultipleObjects function returns when one of the following occurs:
Either any one or all of the specified objects are in the signaled state.
The time-out interval elapses.

> As you can notice that one endl is simply ignored
No, they're both at the end

> first is displayed text to << in thread1() and then second function thread2() is executed
One of the things you've got to get to grips with in thread programming is that anything can happen in any order. If they share a resource (say the screen) then all sorts of weird interactions are possible.
A key idea in all of this is the idea of "atomic" operations - named so because they cannot be split into something smaller.

So in your example, it looks like at every <<, the system takes the opportunity to see if any other thread can be run.

> Maybe this output will be different on your machine.
That seems likely

> How can I appy this explanation on my example?
Well do something like

Thanks for replying Salem,
yes I have noticed that things can happen in any order
I put two for loops, one in every function, tried to display counters and saw what you tolod me.
I have a lot of questions about this. I'll try to figure out on my own as much as I can, but it would be very helpfull if someone recommended me a good tutorial link so not to waste time experimenting and wondering why something is happening!
Thanks

Unfortunately, my multithreading tutorial is not converted to my new web site yet.

When a Windows programs main thread, your main() in this case, all threads it has spun are terminated. If you don't wait for the threads to finish, the main could exit before the threads do their work. It depends a lot on your machine of course, most modern machines will suceed with a simple job like that at least some of the time.

To control access to global or shared resources, you need to use synchronisation routines. The easiest of these to understand is the Critical Section. Read this.

I need help with the following:
I'm learning Synchronization and read about Dijkstra mutex:

Code:

indivisible access routines:
V(s): [s = s + 1]
P(s): [while(s == 0) {wait}; s = s - 1]
The square braces surrounding the statements in the access routines indicate that the operations
are indivisible, or atomic, operations.
The P operation is intended to indivisibly test an integer variable and to block the calling
process if the variable is not positive. The V operation indivisibly signals a blocked
process to allow it to resume operation

And have example with balance management:

Code:

suppose that two processes, p1 and p2, execute concurrently
to access a common integer variable, balance. For example, thread p1 might handle credits
to an account, while p2 handles the debits.

I do understand conceptually this explanation, but problem is how to use that with functions such as CreateMutex() and ReleaseMutex().

I'm having problems to understand how following example really works

Code:

a word processor program creates two threads, one to read a file and another to write to a file. Everything is fine at first, the first thread waits for the second to finish writing to the file before reading it. The two threads work happily, everything is fine as long as the writing thread always writes first.

I realised that these problem are not the same, one is critical section problem sharing variable, other is solving race condition. But, I tried to think variable state as mutex in first example and didn't manage to make analogy. P(mutex) decrement int variable, and V(mutex) increment it. In the second example Writer function sets state to zero and Reader sets state to 1.
Maybe I'm on the wrong track from the begining.
I'm very new (start this morninig) to all this functions(WaitForSingle/MultipleObjects...etc), so
I would ask if someone is kind to explain me on second example how this mechanism really works what is going on and how these functions doing such great job.

I don't know if this is too much for a single thread on this forum, but you all helped me a lot before and hope will help me in future.