The MASM Forum

General => The Campus => Topic started by: hesho28 on November 22, 2013, 10:14:42 AM

Title: CreateThread/TerminateThread API
Post by: hesho28 on November 22, 2013, 10:14:42 AM
Hello,

I want an example of using CreateThread/TerminateThread APIs in masm32.

Thanks
Title: Re: CreateThread/TerminateThread API
Post by: qWord on November 22, 2013, 10:46:51 AM
TerminateThread is a function that does not appear in well written programs, because threads terminate themselves by calling ExitThread or returning from the thread-function (see also msdn)
include \masm32\include\masm32rt.inc
.code
thread proc param:PVOID
   
    fnc crt_printf,"param = 0x%X\n",param
   
    invoke ExitThread,0
thread endp

main proc
LOCAL hThread:DWORD
LOCAL dwTID:DWORD
   
    invoke CreateThread,0,0,thread,0BADF00Dh,0,ADDR dwTID
    mov hThread,eax
   
    ;...
   
    invoke WaitForSingleObject,hThread,INFINITE
    invoke CloseHandle,hThread
    print "fin",13,10
    inkey
   
    exit
main endp
end main


BTW: did you take a look in to the examples-folder?
Title: Re: CreateThread/TerminateThread API
Post by: dedndave on November 22, 2013, 11:12:03 AM
also - don't use parameters in the PROC line, or LOCAL's
        .XCREF
        .NoList
        INCLUDE    \Masm32\Include\Masm32rt.inc
        .List

;###############################################################################################

fnThread PROTO

;###############################################################################################

;        .DATA

;***********************************************************************************************

        .DATA?

hStdOut HANDLE ?

;###############################################################################################

        .CODE

;***********************************************************************************************

_main   PROC

        INVOKE  GetStdHandle,STD_OUTPUT_HANDLE
        mov     hStdOut,eax
        mov     edi,15

loop00: mov     eax,'P'
        sub     eax,edi
        INVOKE  CreateThread,0,0,fnThread,eax,0,0
        dec     edi
        jnz     loop00

        INVOKE  Sleep,250
        print   chr$(13,10)
        inkey
        INVOKE  ExitProcess,0

_main   ENDP

;***********************************************************************************************

fnThread PROC

    xor     eax,eax
    lea     edx,[esp+4]
    push    eax
    mov     ecx,esp
    INVOKE  WriteFile,hStdOut,edx,1,ecx,eax
    pop     ecx
    INVOKE  ExitThread,0

fnThread ENDP

;###############################################################################################

        END     _main
Title: Re: CreateThread/TerminateThread API
Post by: qWord on November 22, 2013, 11:16:36 AM
dave,
we had already had that discussion: the correct function has one parameter and can of course  use local variables.
Quote from: msdnDWORD WINAPI ThreadProc( _In_  LPVOID lpParameter);
Title: Re: CreateThread/TerminateThread API
Post by: dedndave on November 22, 2013, 11:20:05 AM
yah - but, i wasn't going to let you be the winner - lol
there is one parm passed, but the stack doesn't get balanced by a RET (unless you use RET)
if you want to use a stack-frame, make it yourself
Title: Re: CreateThread/TerminateThread API
Post by: qWord on November 22, 2013, 11:25:39 AM
Quote from: dedndave on November 22, 2013, 11:20:05 AMthere is one parm passed, but the stack doesn't get balanced by a RET (unless you use RET)
Sorry, I can't follow you.
Title: Re: CreateThread/TerminateThread API
Post by: dedndave on November 22, 2013, 11:31:53 AM
fnThread PROC Parm:DWORD
;
;
;
    INVOKE  ExitThread,0

fnThread ENDP

generates
fnThread PROC

    push    ebp
    mov     ebp,esp
;
;
;
    INVOKE  ExitThread,0

fnThread ENDP

the stack frame is created, but not balanced

the question is, does ExitThread really release the stack space (including previous operating system versions)
Title: Re: CreateThread/TerminateThread API
Post by: qWord on November 22, 2013, 11:36:44 AM
and what is the problem with that? The stack-memory will be release when the thread-object is destroyed, regardless what ESP points to.
Also, the MSDN is clear about ExitThread: "ExitThread is the preferred method of exiting a thread in C code"
Title: Re: CreateThread/TerminateThread API
Post by: qWord on November 22, 2013, 11:39:41 AM
Quote from: dedndave on November 22, 2013, 11:31:53 AMthe question is, does ExitThread really release the stack space (including previous operating system versions)
it does:
When this function is called (either explicitly or by returning from a thread procedure), the current thread's stack is deallocated, all pending I/O initiated by the thread is canceled, and the thread terminates. The entry-point function of all attached dynamic-link libraries (DLLs) is invoked with a value indicating that the thread is detaching from the DLL.
Title: Re: CreateThread/TerminateThread API
Post by: jj2007 on November 22, 2013, 05:29:34 PM
Quote from: qWord on November 22, 2013, 11:36:44 AMThe stack-memory will be release when the thread-object is destroyed, regardless what ESP points to.
Also, the MSDN is clear about ExitThread: "ExitThread is the preferred method of exiting a thread in C code"

An interesting question is whether correctly exiting with ret leaves something behind, i.e. what happens if an app creates a Million threads.
Title: Re: CreateThread/TerminateThread API
Post by: TWell on November 22, 2013, 09:20:35 PM
Quote from: jj2007 on November 22, 2013, 05:29:34 PM
An interesting question is whether correctly exiting with ret leaves something behind, i.e. what happens if an app creates a Million threads.
ExitThread:
->RtlExitUserThread
  ->NtTerminateThread

ret:
->BaseThreadInitThunk
  ->RtlExitUserThread
    ->NtTerminateThread
Title: Re: CreateThread/TerminateThread API
Post by: jj2007 on November 22, 2013, 09:38:20 PM
Quote from: TWell on November 22, 2013, 09:20:35 PMExitThread:
->RtlExitUserThread
  ->NtTerminateThread

ret:
->BaseThreadInitThunk
  ->RtlExitUserThread
    ->NtTerminateThread


Makes sense, thanks. Probably their formula "the preferred method of exiting a thread in C code" means Microsoft doesn't believe C compilers can handle a retn n correctly ;-)
Title: Re: CreateThread/TerminateThread API
Post by: dedndave on November 23, 2013, 12:23:40 AM
it's been some time since i have read through the create/destroy thread documentation
but, i remember one thing that wasn't mentioned throughout most of it
then, it popped up in one or two places - to leave the reader in a mystery zone - lol
that was, closing the thread handle
to date, i have used CreateThread and ExitThread without any further coding
and, i've had what seems to be good success
but, i am still not sure if i should close the thread handle after it terminates

as for the stack, i was told by an experienced programmer that it could create memory leaks
it may well be that it was a windows 3.1 issue, that lingered into his current experience