News:

MASM32 SDK Description, downloads and other helpful links
MASM32.com New Forum Link
masmforum WebSite

multithread

Started by six_L, February 09, 2007, 02:57:00 PM

Previous topic - Next topic

six_L

.data
Objarr db 1024 dup(0)

.code
....
mov dwThread,0
        mov ThreadNum,0
        lea edx,Objarr
        mov   eax,FristIpAddr
.WHILE eax <= EndIpAddr
push eax ;save eax
bswap eax
push edx ;save edx
invoke CreateThread,NULL,0,OFFSET someproc,eax,THREAD_PRIORITY_NORMAL,NULL
push eax ; handle
invoke CloseHandle,eax
pop eax
pop edx ;restore edx
mov [edx+dwThread],eax ;<== happened error here
add dwThread,4
pop eax ;restore eax
inc ThreadNum
inc eax
.ENDW
invoke WaitForMultipleObjects,ThreadNum,offset Objarr,TRUE,INFINITE
....

hard to debug the multithread, i don't know where is the causation of error.
help.
regards

Tedd

Apart from the fact that you're creating new threads and then immediately destroying them.. :eek

It doesn't show that you've initialised the value of eax (just bad cutting?) - which is the loop counter -- "<=" is a signed comparison. So, depending on the starting value of eax, the loop could continue far more times than you expect, eventually the value of dwThread will cause "edx+dwThread" to go past its allocation, trying to write in memory it shouldn't access, and thus causes an exception.
No snowflake in an avalanche feels responsible.

six_L

regards

joerbanno

Quote from: Tedd on February 09, 2007, 03:19:01 PM
Apart from the fact that you're creating new threads and then immediately destroying them.. :eek

I always thought CloseHandle didn't destroy a thread  :red

raymond

At first glance, I would suspect that
mov   [edx+dwThread],eax
may be interpreted by the assembler as
mov   dwThread[edx],eax.
using edx as the displacement in the dwThread "array".

If that is the case (easily verified with a debugger), you would be trying to access memory way outside of your allocated space. Since your Objarr is declared as a global variable (and you should declare it as a DWORD array since you are storing dwords in it), why not keep the displacement in edx and address your array as Objarr[edx] such that the above instruction would become
mov   Objarr[edx],eax

Then you add 4 to edx instead of dwThread (which you don't need anymore).

Raymond
When you assume something, you risk being wrong half the time
http://www.ray.masmcode.com

Kestrel

try this style:


Objarr      dd  256 dup(0)
ThreadNum   dd  0
   
    ..
    mov edx, ThreadNum
    mov Objarr[edx*4],eax
    ..
    ..
    inc ThreadNum
    ..
   
   
and, Handle not only a number, it has some resource in system.

if you need using the hThread, don't close it.

Thread is running, but handle resource not exist(closed by you).

so, CloseHandle after handle be used END!!


Look this Microsoft sample:
http://msdn2.microsoft.com/en-us/library/ms682516.aspx

six_L

#6
hello,raymond
Thank you very much.


now, it doesn't crash. but auto exit when start up a moment.(the result of running is right)
in multiThread, prevent to use some local buffer?
.data
Objarr db 1024 dup(0)
.code
....
mov eax,dwIP_addr_first ;eax=0AB40000
xor edx,edx
.WHILE eax <= dwIP_addr_end
push eax
bswap eax ;0000B40A
push edx
invoke CreateThread,NULL,0,OFFSET someproc,eax,THREAD_PRIORITY_NORMAL,NULL
push eax
invoke CloseHandle,eax
pop eax
pop edx
mov Objarr[edx],eax
add edx,4
pop eax
inc eax
.ENDW
shr edx,2 ;(edx/4) =ThreadNum
invoke WaitForMultipleObjects,edx,Objarr,TRUE,INFINITE
....

someproc proc dwIpaddr:DWORD
LOCAL pulMac[8]:BYTE
LOCAL Macbuf[20]:BYTE
...

...
ret
someproc endp


hello,Kestrel and joerbanno
Thank your help.

the result is same.(still autoexit, no anyMSG.)

mov eax,dwIP_addr_first ;eax=0AB40000
xor edx,edx
.WHILE eax <= dwIP_addr_end
push eax
bswap eax ;0000B40A
push edx
invoke CreateThread,NULL,0,OFFSET someproc,eax,THREAD_PRIORITY_NORMAL,NULL
pop edx
mov Objarr[4*edx],eax
inc edx
pop eax
inc eax
.ENDW
push edx
invoke WaitForMultipleObjects,edx,Objarr,TRUE,INFINITE
pop edx
.while edx!=0
mov eax,Objarr[4*edx]
push edx
invoke CloseHandle,eax
pop edx
dec edx
.endw


if it is modified as following,works ok, but running slowly.very very slow.

mov eax,dwIP_addr_first ;eax=0AB40000
.WHILE eax <= dwIP_addr_end
push eax
bswap eax ;0000B40A
invoke CreateThread,NULL,0,OFFSET someproc,eax,THREAD_PRIORITY_NORMAL,NULL
push eax
invoke WaitForSingleObject,eax,INFINITE
pop eax
invoke CloseHandle,eax
pop eax
inc eax
.ENDW

regards

six_L

does nobody encounter the such trouble(a large local buffer be defined in some multiThreads)?
regards