⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 wpw_w32_thread_95.html

📁 VC programing
💻 HTML
字号:
<HTML>
<HR><A NAME=WIN32_thread_save_printf>
Return to <a href="wpw_w32_index.html#TOC">Table of Contents for this chapter</a><br>
<H4>Subject: Is printf() thread save?</H4><PRE>
On 6 Jul 1995, Chia Toong Shing wrote:

> Hi there,
>       I just wrote  some console routine ( no window just using dos
> prompt ) to try some threading.  Is seem that printf function is
> not thread safe ! Does it implies that C++ classes eg. iostream
> is not thread safe too ? After C++ is build on top of C.
>       Can somebody advise me ??/
> 
> Thanks
> 
> TS
> 
> 

From my experience, printf() is thread-safe in the meaning that several 
threads can execute printf() in same time, BUT, as I noticed, printf() 
takes a lock (mutex or something alike)  and releases it when finishing, 
so when a thread is suspended during printf(), other threads that want to 
execute printf() are blocked ! take care...

Ayal. 
---

 /\\  //\\       \           Ayal Itzkovitz, CS Faculty, Technion, Israel.
/--\\//--\\___  . \ .        email: ayali@cs.technion.ac.il
    /                        WWW:   http://www.cs.technion.ac.il/~ayali/
--------------------------------------------------------------------------
        "640K ought to be enough for anybody."  - Bill Gates 1981
</PRE>


<HR><A NAME=WIN32_thread_dde>
Return to <a href="wpw_w32_index.html#TOC">Table of Contents for this chapter</a><br>
<H4>Subject: Problem to DDE thread</H4><PRE>
Hello NT-Gurus!

I am writing an application with two threads.
- a main thread and a thread which shall wait for DDE
events.

The main thread starts the dde-thread, which looks like:

// DDE thread
DWORD ddeMain(...)
{
  DdeInitialize( ... )
  DdeCreateStringHandle(.., Service, ..)
  DdeCreateStringHandle(.., Topic, ..)

  DdeConnect(...)

  // hot link setup
  DdeCreateStringHandle(.., Item, ..)
  DdeClientTransaction(..,,, HItem,, XTYP_ADVSTART,,)

 // here is the problem
// !!!!!!!!!!!!!!!!!!
   MessageBox(NULL, "WAIT", "DDE", MB_ICONEXCLAMATION | MB_OK);

// how can i wait here to keep the thread alive without the
MessageBox??
//!!!!!!!!!!!!!!!!!!!!!!

  DdeFreeStringHandle(...);
  DdeFreeStringHandle(...);
  DdeFreeStringHandle(...);

  DdeUninitialize(ddeConvCaps);
}

// dde callback
HDDEDATA CALLBACK ddeCallback(...)
{
  switch (Type)
  {
    case XTYP_ADVDATA:
    .
    .
}

The DDE callback will only be executed, if I start a messagebox in the
threadprocedure (ddeMain).

Is there another function (or what is wrong) to keep the thread alive
to handle the dde messages??? - for(;;) is bad and doesn't work, too.

Gerhard Schmidmayr
 Alcatel Austria AG, Scheydgasse 41, A-1211 Vienna, Austria, Europe

 Phone: (+43 1) 27722 3864   Fax:   (+43 1) 27722 138
 Email: Gerhard.Schmidmayr@aut.alcatel.at  
<HR>
Gerhard Schmidmayr (gerhard.schmidmayr@aut.alcatel.at) wrote:
: Hello NT-Gurus!

Or Guru-wannabes...

: The main thread starts the dde-thread, which looks like:

Don't tell me. The reason you have a DDE thread is because DDE startup
is so incredibly slow that it would sink your whole app if your main
thread started it.  I wish I understood this phenomenon.

The thing that MessageBox is doing for you is running an event-loop:
you can write and run your own, the standard PeekMessage / Translate
Message/DispatchMessage business as in Petzold. You might also want to
use MsgWaitForMultipleObjects outside the loop so you can also wait on
an NT event that tells your thread to give up and go home.  More
details on request.  See Jeffrey Richter, "Advanced NT..." (both
books are on the MSDN cd) for example using MsgWait...

Bernard S. Greenberg
bsg@basistech.com
<HR>
gerhard.schmidmayr@aut.alcatel.at (Gerhard Schmidmayr) wrote:

>Hello NT-Gurus!

>I am writing an application with two threads.
>- a main thread and a thread which shall wait for DDE
>events.

>The main thread starts the dde-thread, which looks like:

>// DDE thread
>DWORD ddeMain(...)
>{
>  DdeInitialize( ... )
>  DdeCreateStringHandle(.., Service, ..)
>  DdeCreateStringHandle(.., Topic, ..)

>  DdeConnect(...)

>  // hot link setup
>  DdeCreateStringHandle(.., Item, ..)
>  DdeClientTransaction(..,,, HItem,, XTYP_ADVSTART,,)

> // here is the problem
>// !!!!!!!!!!!!!!!!!!
>   MessageBox(NULL, "WAIT", "DDE", MB_ICONEXCLAMATION | MB_OK);

>// how can i wait here to keep the thread alive without the
>MessageBox??
>//!!!!!!!!!!!!!!!!!!!!!!

>  DdeFreeStringHandle(...);
>  DdeFreeStringHandle(...);
>  DdeFreeStringHandle(...);

>  DdeUninitialize(ddeConvCaps);
>}

>// dde callback
>HDDEDATA CALLBACK ddeCallback(...)
>{
>  switch (Type)
>  {
>    case XTYP_ADVDATA:
>    .
>    .
>}

>The DDE callback will only be executed, if I start a messagebox in the
>threadprocedure (ddeMain).

>Is there another function (or what is wrong) to keep the thread alive
>to handle the dde messages??? - for(;;) is bad and doesn't work, too.

>Gerhard Schmidmayr
> Alcatel Austria AG, Scheydgasse 41, A-1211 Vienna, Austria, Europe

> Phone: (+43 1) 27722 3864   Fax:   (+43 1) 27722 138
> Email: Gerhard.Schmidmayr@aut.alcatel.at  


As DDEML is just creating windows and using WM_DDE messages on your
behalf I guess you would need to call peekmessage, or getmessage or
whatever to keep the messages flowing, and then have a global flag
that you scan occasionally and set in your dde callback. You could use
an event and WaitForSingleObject BUT i dont beleive that will let the
DDEML dll call you back because you may be yielding to other processes
but not to your self ! Try it, and tells us what happens out of
interest. 

darrenr@gassoc.demon.co.uk
</PRE>


<HR><A NAME=WIN32_thread_process_list>
Return to <a href="wpw_w32_index.html#TOC">Table of Contents for this chapter</a><br>
<H4>Subject: How Do I get a List of Running Proccesses</H4><PRE>
It was eloquently stated by khamsi@mathworks.com (Sarir Khamsi) :

> >>>>>   <nkatz@panix.com> writes:
> In article <3uk7c8$l63@news1.panix.com> nkatz@panix.com () writes:

> > How do I get a list of running Proccesses in NT.  I want to be able
> > to generate a list of proccesses like what you can see with the
> > Performance Monitor.

> > Also is there any way to get the Proccess Handle for a seperate
> > running proccess ?

> Look at the TLIST example on the Developer Network CD. If you don't
> have it, I can email it to you.

You might also have a look at the source code for PView which is in
the MSVC20\SAMPLES\WIN32\PVIEW directory. I have a class library that
wraps the Performance Monitor stuff adequately.

All you have to do to get a process handle is call OpenProcess(DWORD
ID) where the DWORD ID is something you got from somewhere like the
performance monitor keys in the registry (which is how pview does it).

All in all I would argue that this is one of the more painful aspects
of process management in NT and reasonably bogus. Another bogus aspect
is the inability to get a thread handle except by being the one who
started the thread, or by attaching to the threads process with the
debug apis. There should be an OpenThread() API. My US$0.02.

					-charles



===============================================================================

    _______     ___                ^           ___         ___   _______ 
  /| ____  \   /| |               / \         /|  \       /  |  /|  __  \ 
 | | |  /|  | | | |              /   \       | |   \     /   | | | | /\  |
 | | |_/_| /  | | |             /  _  \      | | |\ \   / /| | | | || /  |
 | |      |   | | |            /  //\  \     | | | \ \_/ / | | | | |_/  /
 | | ____  \  | | |           /  /___\  \    | | |\ \___/| | | | | ____/
 | | |  /|  | | | |          /  _______  \   | | | \/__/ | | | | | |  /
 | | |_/_| /  | | |_____    /  /     / \  \  | | |       | | | | | |_/
 | |______/   | |_______|  /__/ ____/   \__\ | |_|       | |_| | |_|
 |/      /    |/       /  /  /       \  /  / |/ /        |/ /  |  /
 /______/     /_______/  /__/         \/__/  |_/         |_/   |_/

===============================================================================
   Charles Parker                   ph. 617.825.0792
   Software Engineer                fax 617.492.2836

   Personal E-mail                  cparker@groovyshoes.com
   Politzer & Haney                 parker@world.std.com
                                    Compuserve:76217,1111
===============================================================================
</PRE>

<HR><A NAME=WIN32_thread_kill>
Return to <a href="wpw_w32_index.html#TOC">Table of Contents for this chapter</a><br>
<H4>Subject: Killing thread from outside it?</H4><PRE>

Jeremy H. Griffith (jeremy@wco.com) wrote:


:  
: However, my (limited) study of Win32 has not revealed any function
: for terminating a thread from *outside* that thread.  Is there one?

When you create a thread using CreateThread, you get a handle to the 
thread of type HANDLE.  If you want to destroy this thread, you use that 
handle and call TerminateThread.  Very basic, very easy.  I wrote a 
program that created dozens of threads that were in infinite loops that I 
used to test the multitasking ability of Windows 95 and Windows NT, and I 
used this function to kill them when I was done.

Try getting the book:
	Schildt, Herbert.  SCHILDT'S WINDOWS 95 PROGRAMMING IN C AND 
C++.  c1995.


Good luck.

Taud
cramesto@eniac.seas.upenn.edu

<HR>

cramesto@blue.seas.upenn.edu (Taud B Crames) wrote:
>When you create a thread using CreateThread, you get a handle to the 
>thread of type HANDLE.  If you want to destroy this thread, you use that 
>handle and call TerminateThread.  

the problem with TerminateThread is that it supports almost no cleanup. Your
thread just dies. What would be much more useful is a way to generate an
exception in the thread so that a top level catch  would do the cleanup. Alas
it doesn't exist.

muzo


standard disclaimer


</PRE>

<HR><A NAME=WIN32_process_Control>
Return to <a href="wpw_w32_index.html#TOC">Table of Contents for this chapter</a><br>
<H4>Subject: Process Control in win32?</H4><PRE>

In article <3vb0kt$mje@hudson.lm.com>, soup@telerama.com (John T. Campbell, Jr.) says:
>
>
>I'm trying to find out how to get unix like control of a launched
>process in ms-windows.  I start a process with CreateProcess, and I
>want to be notified when the process exits (I don't wait around for it
>to complete after launch) and I want to kill the process if the caller
>dies.
>
>Anyone got any ideas?
>
>thanks...
>


you have a handle to the process and it will be signaled when the
process ends.  just call waitforsingleobject() or waitformultipleobjects()
using the process handle.  you can create a separate thread if need be
to wait for the termination.  if you need to kill the process then
call terminateprocess().  its that simple.

</PRE> 

</HTML>

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -