If this is your first visit, be sure to
check out the FAQ by clicking the
link above. You may have to register or Login
before you can post: click the register link above to proceed. To start viewing messages,
select the forum that you want to visit from the selection below.

But the arrivals of different events came so fast so that I even can't identify which is which.
(They actually came in under 0.1 s, not around 8 seconds), I conclude that the Suspend method doesn't even hold the thread. I wonder why.... I don't know this code would be where the core problem is, I will post more later.
Thanks
Jack

Re: A Thread that doesn't hold long enough

Originally Posted by lucky6969b

But the arrivals of different events came so fast so that I even can't identify which is which.
(They actually came in under 0.1 s, not around 8 seconds), I conclude that the Suspend method doesn't even hold the thread. I wonder why.... I don't know this code would be where the core problem is, I will post more later.
Thanks
Jack

Why not add some logging and figure it out for sure. Btw, are you familiar with the concept of RAII with respect to thread synchronization?

Re: A Thread that doesn't hold long enough

Hello Arjay,
I suddenly realized that I need to open up a main thread that derives from Process.
But I need to suspend the main thread in order for the child threads to execute.
So my new question is how do I suspend the main thread of other child threads without freezing the program?

Code:

Sim::Sim() {
Thread::Initialize(); // Initialize the threads package.
// Create a main thread, m_Arrival is a child thread which has no parent
// so that Process::Current == this because the m_Arrival thread == main thread
m_Arrivals = new Arrival_MainThread(false);
// Main Thread has to be suspended in order for child threads to execute
// but if I suspend the main thread here, it will freeze the program,
// because the main thread never terminates
m_Arrivals->Await();
}

Update:
Should I make this a daemon process?
How do I detach the child thread from the main thread in win32?
Thanks
Jack

Re: A Thread that doesn't hold long enough

Should I make this a daemon process?
How do I detach the child thread from the main thread in win32?

These are not Windows concepts as far as I know. I've heard about this on Linux, but not on Windows.

At any rate, threads should execute independent of other threads. So your design should generally avoid having to do anything in one thread that impacts other threads (the exception to this is synchronizing shared data).

With this in mind, what you wrote below puzzles me.

But I need to suspend the main thread in order for the child threads to execute.

Once a thread has been started it runs 'independently' of the thread that created it (subject to synchronization) until the process to which it belongs is destroyed. Destroying the main() thread is akin to destroying the process. So I don't understand what you mean by 'suspending the main thread'?

Last edited by 2kaud; July 18th, 2014 at 03:18 AM.

All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

Re: A Thread that doesn't hold long enough

If you have the time to take a look at the source code, it would be greatly appreciated!
I stuck with it for a week already. It's pretty simple and light-weight except that I just can't figure out
the thread part