Multithreading

I don't see the use of threads in practice. Everything I've read states that threads allow two different operations to appear to happen at the same time. But every time I've tried to get that to happen it doesn't do anything special. For example, say I want to write a simple test for threads that shares two loops.

>>> But all of my tries with _beginthread and _endthread end up like this.

Obviously I cant' see your code, but if those two thread functions are really simple, it is quite possible that the first thread has finished executing before the second thread has been established.

Try making your loops longer, and possibly add a small random sleep() into each iteration.

If none of that makes sense, post your code.

>>> You probably have to adjust the thread priority

I would doubt that. If you throw two threads from the same process, they will have the same thread priority, altering one with respect to the other is more likely to produce the second, undesired result.

>>> show me how to do what I want

Not without knowing what it is you want to do!

Wave upon wave of demented avengers march cheerfully out of obscurity unto the dream.

>>it is quite possible that the first thread has finished executing before the second thread has been established.
If the threads are executed in order then what's the point? Why not just do whatever you're doing in order and forget about threads altogether? I'm under the impression that if I have two threads at the same time, they'll do a little bit of work and then let the other thread do a little bit until nothing is left.

You miss my point, the threads are started in order because you call beginthread once, then again. What I am saying, is with thread functions as small as those, the thread spun by the first call to beginthread has finished executing before the second call to beginthread has spun the thread.

>>> I'm under the impression that if I have two threads at the same time, they'll do a little bit of work and then let the other thread do a little bit until nothing is left.

That's exactly right, but you are not seeing it with your example. Set the loop counters right up, or, as I said, introduce a short sleep() into your loops.

You also need to review the use of endthread(), how you are doing it now is incorrect. endthread() is a routine you use inside thread functions, but in this case, it is not necessary since the thread exits when your thread function returns.

Wave upon wave of demented avengers march cheerfully out of obscurity unto the dream.

>>but you are not seeing it with your example
Wonderful! It works as I expected now that I've added a 1 second sleep.

>>it is not necessary since the thread exits when your thread function returns.
So I can just let the function return and the thread is terminated automatically? If so would it be better to say what I mean?

Originally posted by OneStiffRod This is more C++, I think as in Windows u use CreateThread() to create a thread.

No....beginthread (and beginthreadex for that matter) both call CreateThread..... these functions ensure that the correct multithreaded libraries are linked and makes sure that each thread has its own global buffers for some function calls (ie the C std function ctime().....)

CreateThread() has, in the past, been deprecated because it can cause small memory leaks in Win32 programs which use the standard library. There is still considerable debate on this subject, I am not sure this is an issue any longer, but to be safe, it's use is avoided by most Win32 programmers I know.

_beginthread() and _beginthreadex() do not have this problem, ( the name _beginthreadex() is somewhat compiler dependent, Borland for example, used to call it _beginthreadNT()).

*** EDIT ***

Bit of multithreading there Fordy!

Wave upon wave of demented avengers march cheerfully out of obscurity unto the dream.

Hmm, can't you use a critical section or semaphore to mediate between the threads? Okay, I don't know what I'm talking about (I've read about multithreading, but never used it) but I believe what I've mentioned is used for handling memory shared between threads...

Also...removing the _endthread() from main would have stopped you app working....._endthread() was trying to stop your primary thread and whatever confusion that was causing was allowing the other threads to complete....without the _endthread() it was finishing the primary thread after the creation of the threads..therefore killing them before they could action......use one of the WaitFor##### APIs to stall the primary thread and allow the child threads to terminate naturally (as above)

<edit>Oh...and its unlikely you will see the direct benefit of a critical sec in my example...though its certianly safer and will avoid the problem adrian stated..its use is far more obvious if you are manipulation memory....like say a link list with multiple threads accessing data at the same time......imagine 1 thread reading a node as another is deleting it - bound to cause bugs! </edit>