Hello,
I have a problem with a thread used in loop.
One used only one time,it work.
One used in loop there is some failed.
What is the best memory function to use in a thread ?.
HeapAlloc without the HEAP_NO_SERIALIZE flag will avoid problems caused by multiple threads trying to allocate from the same heap at the same time -- if that is your problem.
Use of a mutex/semaphore may be appropriate, depending what you're doing.
I use Heapalloc like this:
Quote
invoke HeapAlloc,dword ptr [esi][MEMHEAP.Hheap],HEAP_ZERO_MEMORY,edx
i run it:
Quote
invoke CreateThread,NULL,NULL,Thread_BoucleTraduction,HwndProc,NULL,addr thread.threadid
mov thread.Htread,eax
And the thread:
Thread_BoucleTraduction PROC Hfenetre:DWORD
Local retour:DWORD
mov retour,1
invoke working functions;-------
FindeThread_BoucleTraduction:
invoke CloseHandle,thread.Htread
mov thread.Htread,0
mov thread.threadid,0
invoke PostMessage,HwndProc,WM_COMMAND,IDM_TRADUIRE6,NULL ;next call for thread
mov eax,retour
ret
Thread_BoucleTraduction endp
It seems I have found the problem (Heap fragmented).
Quote
invoke CloseHandle,thread.Htread
mov thread.Htread,0
mov thread.threadid,0
mov edx,NomdesDword.Hheap ;get a handle heap in use
.if edx != 0
invoke HeapCompact,edx,0 ;-------------------- and all is normal ---------
.endif
I think Tedd was also referring to the race conditions you have. It's entirely possible for the CloseThread in Thread_BoucleTraduction to happen before the main thread assigns the handle to thread.Hthread, which would leak the handle. It'd be even worse if you create lots of threads in a loop.
Yves,
If you need to repeatedly call a procedure that is pointer to by creating a new thread, you need some method to delay tyhe caller until the identification of any data sent to the new thread handling procedure is completed. I personally use an operating system type of spinlock, a closed loop that exits when a value is changed in the target procedure and the idea is to hold up the creation of a new thread until the last one is up and going.
Have a look at a masm32 example in "example\threads\multidl".
i doubt that i'd allocate memory in the thread code like that
allocate a single block, and pass a pointer to the indexed buffers
Quote from: ToutEnMasm on October 03, 2014, 01:45:06 AMinvoke HeapCompact,edx,0 ;-------------------- and all is normal ---------
... and that is a recipe for disaster. Heap fragmentation is a condition that *may* happen after you shuffled around megabytes of little strings, and even then, it rarely causes problems unless you are tight with RAM. So HeapCompact accidentally "solves" something with a totally different cause.
Depending on what your threads do, the spinlock thing proposed by Hutch could be a solution, but we should see the complete code before.
I will study the example\threads\multidl
The effect of HeapCompact can be tested with headinc.exe
http://masm32.com/board/index.php?topic=576.msg4665#msg4665 (http://masm32.com/board/index.php?topic=576.msg4665#msg4665)