Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

_beginthread() and thread priority

832 views
Skip to first unread message

kgold

unread,
Jan 21, 1999, 3:00:00 AM1/21/99
to
I have a typical application with an MFC based GUI thread and an
occasional worker thread. The worker thread called by the GUI is in a
DLL.

When I use _beginthread() to create the worker, the program works.
However, whenever the worker thread runs, the GUI becomes slow, and
the mouse movements are choppy.

My idea was to use SetThreadPriority() to lower the priority of the
worker thread. However, it requires a thread HANDLE returned from
CreateThread(). _beginthread() returns a different type of handle.

My understanding is that CreateThread() cannot be used if standard C
runtime libraries are called. This function is in a DLL, uses many
standard C library calls, and also uses other DLL's of unknown
content.

When I tried to run the thread using CreateThread(), the thread did
not even start. I set a breakpoint at the entrance and it never gets
there.

Now for the questions:

1 - Is my basic analysis correct?

2 - Is there a function to lower a thread priority which can be used
after _beginthread()?

3 - Is there a way to convert the handle so that I can use
_beginthread() followed by SetThreadPriority()?

3 - Is there some other way to keep the GUI running smoothly
while a worker thread is running?

3 - Am I right that CreateThread() cannot be used if C runtime
libraries are called?

4 - What would cause CreateThread() to fail in such a way that
it doesn't seem to get to the first instruction.

--
Ken Goldman kg...@watson.ibm.com 914-784-7646

Dirk Ringe

unread,
Jan 21, 1999, 3:00:00 AM1/21/99
to
Use _beginthreadex (). It gives you the handle and threadid you need for the
various win32 functions...

Dirk


Alen Ilkov

unread,
Jan 21, 1999, 3:00:00 AM1/21/99
to
Both _beginthread and _beginthreadex return the thread handle, which they
get from CreateThread. You can use that handle to set priority.
You are right to use _beginthread when using C, CreateThread alone will
cause memory leaks, but it should run nonetheless I think.

You can set the worker thread priority from within the worker thread using
GetCurrentThread to get the handle. You can also control the context
switches from within the worker thread SwitchToThread or from GUI thread
by SuspendThread. If you have tight loops in the worker, you can
occasionally Sleep or switch context.

Best Regards,
Alen
ail...@esd.dl.nec.com

Felix Kasza [MVP]

unread,
Jan 22, 1999, 3:00:00 AM1/22/99
to
Alen,

> You are right to use _beginthread when using C, CreateThread alone will
> cause memory leaks, but it should run nonetheless I think.

This applies only to the static-link version of the runtime. For this
and other reasons, I *highly* recommend using the DLL runtime, which
lets you use CreateThread() without prejudice.

--

Cheers,

Felix.

If you post a reply, kindly refrain from emailing it, too.
Note to spammers: fel...@mvps.org is my real email address.
No anti-spam address here. Just one comment: IN YOUR FACE!

0 new messages