2-1

/*
 * Numbers.c
 *
 * Sample code for "Multithreading Applications in Win32"
 * This is from Chapter 2, Listing 2-1
 *
 * Starts five threads and gives visible feedback
 * of these threads running by printing a number
 * passed in from the primary thread.
 *
 */

#define WIN32_LEAN_AND_MEAN
#include <stdio.h>
#include <stdlib.h>
#include <windows.h>

DWORD WINAPI ThreadFunc(LPVOID);

int main()
{
    HANDLE hThrd;
    DWORD threadId;
    int i;
   
    for (i=0; i<5; i++)
    {
        hThrd = CreateThread(NULL,
            0,
            ThreadFunc,
            (LPVOID)i,
            0,
            &threadId );
        if (hThrd)
        {
            printf("Thread launched %d\n", i);
            CloseHandle(hThrd);
        }
    }
    // Wait for the threads to complete.
    // We'll see a better way of doing this later.
    Sleep(2000);

    return EXIT_SUCCESS;
}

DWORD WINAPI ThreadFunc(LPVOID n)
{
    int i;
    for (i=0;i<10;i++)
     printf("%d%d%d%d%d%d%d%d\n",n,n,n,n,n,n,n,n);
    return 0;
}

od

主线程

 

00401000  /$  51            push    ecx                              ;  Numbers.004070D8
00401001  |.  53            push    ebx
00401002  |.  8B1D 08604000 mov     ebx, dword ptr [<&KERNEL32.Creat>;  kernel32.CreateThread
00401008  |.  55            push    ebp
00401009  |.  8B2D 04604000 mov     ebp, dword ptr [<&KERNEL32.Close>;  kernel32.CloseHandle
0040100F  |.  56            push    esi
00401010  |.  57            push    edi
00401011  |.  33FF          xor     edi, edi
00401013  |>  8D4424 10     /lea     eax, dword ptr [esp+10]
00401017  |.  50            |push    eax
00401018  |.  6A 00         |push    0
0040101A  |.  57            |push    edi
0040101B  |.  68 60104000   |push    00401060
00401020  |.  6A 00         |push    0
00401022  |.  6A 00         |push    0
00401024  |.  FFD3          |call    ebx
00401026  |.  8BF0          |mov     esi, eax
00401028  |.  85F6          |test    esi, esi
0040102A  |.  74 11         |je      short 0040103D
0040102C  |.  57            |push    edi
0040102D  |.  68 30704000   |push    00407030                        ;  ASCII "Thread launched %d",LF
00401032  |.  E8 59000000   |call    00401090
00401037  |.  83C4 08       |add     esp, 8
0040103A  |.  56            |push    esi
0040103B  |.  FFD5          |call    ebp
0040103D  |>  47            |inc     edi
0040103E  |.  83FF 05       |cmp     edi, 5
00401041  |.^ 7C D0         \jl      short 00401013
00401043  |.  68 D0070000   push    7D0                              ; /Timeout = 2000. ms
00401048  |.  FF15 00604000 call    dword ptr [<&KERNEL32.Sleep>]    ; \Sleep这里请挂起主线程,要不主线程退出,程序就Over了
0040104E  |.  5F            pop     edi
0040104F  |.  5E            pop     esi
00401050  |.  5D            pop     ebp
00401051  |.  33C0          xor     eax, eax
00401053  |.  5B            pop     ebx
00401054  |.  59            pop     ecx
00401055  \.  C3            retn

 线程

 

00401060   .  56            push    esi
00401061   .  8B7424 08     mov     esi, dword ptr [esp+8]
00401065   .  57            push    edi
00401066   .  BF 0A000000   mov     edi, 0A                          ;  i=10
0040106B   >  56            push    esi
0040106C   .  56            push    esi
0040106D   .  56            push    esi
0040106E   .  56            push    esi
0040106F   .  56            push    esi
00401070   .  56            push    esi
00401071   .  56            push    esi
00401072   .  56            push    esi
00401073   .  68 44704000   push    00407044                         ;  ASCII "%d%d%d%d%d%d%d%d",LF
00401078   .  E8 13000000   call    00401090                         ;  printf
0040107D   .  83C4 24       add     esp, 24
00401080   .  4F            dec     edi                              ;  i--
00401081   .^ 75 E8         jnz     short 0040106B                   ;  i<=10
00401083   .  5F            pop     edi
00401084   .  33C0          xor     eax, eax
00401086   .  5E            pop     esi
00401087   .  C2 0400       retn    4

 

 

ida

.text:00401000
.text:00401000 ; =============== S U B R O U T I N E =======================================
.text:00401000
.text:00401000
.text:00401000 ; int __cdecl main(int argc, const char **argv, const char *envp)
.text:00401000 _main           proc near               ; CODE XREF: start+AFp
.text:00401000
.text:00401000 ThreadId        = dword ptr -4
.text:00401000 argc            = dword ptr  4
.text:00401000 argv            = dword ptr  8
.text:00401000 envp            = dword ptr  0Ch
.text:00401000
.text:00401000                 push    ecx
.text:00401001                 push    ebx
.text:00401002                 mov     ebx, ds:CreateThread
.text:00401008                 push    ebp
.text:00401009                 mov     ebp, ds:CloseHandle
.text:0040100F                 push    esi
.text:00401010                 push    edi
.text:00401011                 xor     edi, edi        ; i=0
.text:00401013
.text:00401013 loc_401013:                             ; CODE XREF: _main+41j
.text:00401013                 lea     eax, [esp+14h+ThreadId]
.text:00401017                 push    eax             ; lpThreadId
.text:00401018                 push    0               ; dwCreationFlags
.text:0040101A                 push    edi             ; lpParameter
.text:0040101B                 push    offset StartAddress ; lpStartAddress
.text:00401020                 push    0               ; dwStackSize
.text:00401022                 push    0               ; lpThreadAttributes
.text:00401024                 call    ebx ; CreateThread
.text:00401026                 mov     esi, eax
.text:00401028                 test    esi, esi
.text:0040102A                 jz      short loc_40103D
.text:0040102C                 push    edi
.text:0040102D                 push    offset aThreadLaunched ; "Thread launched %d\n"
.text:00401032                 call    printf
.text:00401037                 add     esp, 8
.text:0040103A                 push    esi             ; hObject
.text:0040103B                 call    ebp ; CloseHandle
.text:0040103D
.text:0040103D loc_40103D:                             ; CODE XREF: _main+2Aj
.text:0040103D                 inc     edi
.text:0040103E                 cmp     edi, 5          ; i<5
.text:00401041                 jl      short loc_401013
.text:00401043                 push    7D0h            ; dwMilliseconds
.text:00401048                 call    ds:Sleep
.text:0040104E                 pop     edi
.text:0040104F                 pop     esi
.text:00401050                 pop     ebp
.text:00401051                 xor     eax, eax
.text:00401053                 pop     ebx
.text:00401054                 pop     ecx
.text:00401055                 retn
.text:00401055 _main           endp
.text:00401055
.text:00401055 ; ---------------------------------------------------------------------

thread

.text:00401060
.text:00401060 ; =============== S U B R O U T I N E =======================================
.text:00401060
.text:00401060
.text:00401060 ; DWORD __stdcall StartAddress(LPVOID)
.text:00401060 StartAddress    proc near               ; DATA XREF: _main+1Bo
.text:00401060
.text:00401060 n               = dword ptr  4
.text:00401060
.text:00401060                 push    esi
.text:00401061                 mov     esi, [esp+4+n]
.text:00401065                 push    edi
.text:00401066                 mov     edi, 0Ah        ; i=10
.text:0040106B
.text:0040106B loc_40106B:                             ; CODE XREF: StartAddress+21j
.text:0040106B                 push    esi
.text:0040106C                 push    esi
.text:0040106D                 push    esi
.text:0040106E                 push    esi
.text:0040106F                 push    esi
.text:00401070                 push    esi
.text:00401071                 push    esi
.text:00401072                 push    esi
.text:00401073                 push    offset aDDDDDDDD ; "%d%d%d%d%d%d%d%d\n"
.text:00401078                 call    printf
.text:0040107D                 add     esp, 24h
.text:00401080                 dec     edi             ; i--
.text:00401081                 jnz     short loc_40106B
.text:00401083                 pop     edi
.text:00401084                 xor     eax, eax
.text:00401086                 pop     esi
.text:00401087                 retn    4
.text:00401087 StartAddress    endp
.text:00401087
.text:00401087 ; ---------------------------------------------------------------------------
.text:0040108A                 align 10h

 

你可能感兴趣的:(2-1)