Jump to content
Tuts 4 You

How to read from CLI till its finished


LCF-AT

Recommended Posts

LCF-AT

Hi,

Quote

Can I also use Enter/leave functions multible time inside of a routine?Yes or?

Yes, provided you don't enter twice in a row and each enter is followed by a leave.

Thats why I'am aksing too.So this I have seen / found in VirtualDub app Entering 2 times EnterCriticalSection function in main thread with the same Object section but in case of the second entering it comes back from EnterCriticalSection to main code = Has no effect right?Like the example I did post before calling 2 times EnterCriticalSection and 2 times LeaveCriticalSection.This should be then redundant or?

greetz

Link to post
aIjundi
15 hours ago, LCF-AT said:

Like the example I did post before calling 2 times EnterCriticalSection and 2 times LeaveCriticalSection.This should be then redundant or?

It is redundant. I will have the same effect of just having the first Enter and last Leave

  • Like 1
Link to post
LCF-AT

Hi again,

just wanna sum up one more time about those CriticalSection functions and to use them correctly.Those function do read the thread ID of the actually thread who is calling that function.So if the thread ID is same then it comes back to main code (because it did already entered already) and runs it through but if the thread ID is NOT same then it will loop inside of EnterC function.Ok.

Now lets come back to one of my other questions I asked before.....what about the Main thread which gets called FROM a other thread?

Lets say I have running many threads and every single of them has a own thread ID and using EnterC LeaveC functions to prevent overlapping / accessing shared resources right.But if now ALL of those thread to send a message BACK to MAIN thread by using SendMessage function with paramter WM_COMMAND & Same ID (to execute another re-call create thread process) and just by random it does happen at the right SAME moment +/- then it can bring up trouble = they access sharing resources = HOW to protect code & routines which are in the MAIN thread?So you do remember, I can not use Enter / Leave functions for the MAIN thread itself = runs through.So you know what I mean right?

So I'am not sure whether there is any method too to protect SharedResources for the main thread if I do send messages FROM other threads back INTO main thread.Just need to know that clear whether I have to protect my stuff in main thread anyhow IF thread do send new tasks back into main thread.If I need to protect it similar as I can do inside of a thread (by using Enter / leave) so how should I do it then?Lets say the main thread gets send messages from 100 other threads at same time.Sorry I can't explain it better but I think you know what I mean.Just wanna know what happens in this case in the main thread.Getting all requests to main thread processed one by one  or at same time or how is it going there?Just need some image to imagine.Maybe you can tell me a little about that to make it clear.Thanks.

greetz

Link to post
aIjundi
Posted (edited)
1 hour ago, LCF-AT said:

Lets say the main thread gets send messages from 100 other threads at same time.Sorry I can't explain it better but I think you know what I mean.Just wanna know what happens in this case in the main thread.Getting all requests to main thread processed one by one  or at same time or how is it going there?Just need some image to imagine

Lets say there is a ticket windows that people want to buy from. To avoid conflict between people there is a security guard who puts people in order when they show up. The operator on the window sells the ticket to the first person in the line, the person leaves and so on.

The window operator = The main thread

security guard = the operating system

person showing up = a message being added to the queue (SendMessage or PostMessage was called)

 

As far as I know, SendMessage (and PostMessage) are thread-safe, you don't have to worry about them being called from different threads at the same time. When it is called, the message will be enqueued in the message queue. The main thread will be in a loop, getting, translating and dispatching messages from the queue one-by-one.

1 hour ago, LCF-AT said:

I can not use Enter / Leave functions for the MAIN thread itself

You can, but it can be tricky. You have to make sure that the following case does not happen:
Thread A holds the critical section

Thread A waits for Thread B to process

Thread B wants to hold the critical section but can't and have to wait for thread A to release it

In this case, you have both threads waiting on each other, i.e. a deadlock

Edited by aIjundi (see edit history)
  • Like 1
Link to post
LCF-AT

Hi again,

thanks for the info about it aIjundi. :) So I don't have to worry because any access gets proceed one by one.Good to know that now so before I thought it also could happen same time all at once you know.

You say the MAIN thread get executed thread safe by itself = Ok but in this case it also would mean that nobody needs to protect any code / procedere in the main thread = nobody needs to use Enter / Leave C functions IN the main thread because its already thread safe by itself....right?This would be my logic thinking now.The question is why are there so many apps who using those Crit functions IN the main thread?I dont check this.Do they all code wrong (VirtualDub / Winhex etc)?Of course I'am just asking you know.

greetz

Link to post
kao
24 minutes ago, LCF-AT said:

You say the MAIN thread get executed thread safe by itself

He never said that, and that's actually incorrect.

Thread itself is not "thread safe" or unsafe - code accessing a shared resource is. If code in main thread is accessing some resource that other threads are accessing too, it should call Enter/LeaveCriticalSection.

  • Like 1
Link to post
LCF-AT

Hi,

sure?What about this...?

Quote

The window operator = The main thread

security guard = the operating system

person showing up = a message being added to the queue (SendMessage or PostMessage was called)

 

As far as I know, SendMessage (and PostMessage) are thread-safe, you don't have to worry about them being called from different threads at the same time. When it is called, the message will be enqueued in the message queue. The main thread will be in a loop, getting, translating and dispatching messages from the queue one-by-one.

So what is right now?

You said, kao...

Quote

If code in main thread is accessing some resource that other threads are accessing too, it should call Enter/LeaveCriticalSection.

....so that clear so far but I mean just the main thread itself in this case.If the main thread gets many requests from other threads and the main thread does handle every single request one by one (not at once like other threads do) THEN I don't need to use any Enter / Leave Crit function IN the main thread ALSO not if I use global variables / buffer IF they are just used by the main thread itself.....right?

Example:

.data?
;;;;;;;;;;;;;;;;;;;;;;;;;;;; Below = buffer variables just used by Main thread itself
_buffer1 	dd ?
_buffer2 	dd ?
_Buf		db 128 dup (?)
_Buf2		db 128 dup (?)
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

WndProc proc hWin:HWND,uMsg:UINT,wParam:WPARAM,lParam:LPARAM

	mov eax, uMsg
	.IF eax == WM_INITDIALOG

	.ELSEIF eax == WM_COMMAND
		mov eax, wParam
		mov ecx, eax
		and eax, 0FFFFh 
		shr ecx,16 
		.IF ax == ID_MAKETHREAD_READY
			....using / filling buffers above....
			invoke PrepairFunction,addr _Buf,addr _Buf2,_buffer1,_buffer2
			....finished using global main thread buffer variables...
			invoke CreateThread,NULL,5000,addr TestThread,edi,NULL,NULL
			....
		.endif


	.ELSE
		Invoke DefWindowProc,hWin,uMsg,wParam,lParam
		ret
	.ENDIF
	xor    eax,eax
	ret
WndProc endp

So I mean if I just use buffers / variables for the main thread itself (called global main thread buffers) then I don't have to worry about anything to overlap,overwrite,alloc,free actions IF all processes the main thread gets are proceed one by one...right?If so THEN I still dont check why app using Enter / Leave in main thread.So Enter Leave dont have the effect using them in main thread instead using them in created other threads.

PS: Maybe I'am a little pedantic in that case but just beacuse I want to know it right, you know.That all.

greetz

Link to post
kao
8 hours ago, LCF-AT said:

invoke PrepairFunction,addr _Buf,addr _Buf2,_buffer1,_buffer2 ....finished using global main thread buffer variables...

invoke CreateThread,NULL,5000,addr TestThread,edi,NULL,NULL

IFF _Buf, _Buf2, _buffer1, _buffer2 are not accessed by other threads, you're fine without calling Enter/LeaveCriticalSection.
In your example code I don't see any reason not to move them into local variables. So, perhaps there's more code hidden somewhere else..

What does EDI point to? This might be a problem.

 

8 hours ago, LCF-AT said:

Maybe I'am a little pedantic in that case but just beacuse I want to know it right, you know.That all.

That perfectly fine, no worries!  :thumbsup: 
I still think you should try making a small test app first, but whatever works for you...

 

  • Like 1
Link to post
LCF-AT

Hi,

ok good to know that now about variables & using them just for the Main thread only. :) The variables in my example above are just used a tiny part of many I just use to prepair a large struct which is completely memory allocated / copied.The buffers are just used temporary for prepairing.In EDI it hold the entire filled ready struct (80h bytes long / 32 entry infos) I do send with the thread so that the thread can work with / check / free if finished etc.

So I tried doing to debug some apps like VirtualDub but I dont get really smart if I do that and I just get more questions in my mind.I made 2 images, now have a look....

Image 1: You see that VD will enter the EnterC function a second time in the MAIN thread....

T1_2021-05-17_203222.png.05c966e45ddcb9f11a4356382d18b944.png

....it will use again the same CritSection which you can see in dump window + infos about the first entering = Main thread ID and now it will enter it again in a sub routine....below image 2 after entering again...

T2_2021-05-17_203222.png.a04ea157c8feda5df811872b9c4623bf.png

....it come out straight of course because thread ID is same.The master question for me in that case is why?Why using EnterC / LeaveC in MAIN thread any why using it more than once?Is used for anything else maybe?Otherwise I am pretty clueless in this case and really can not see why the VD app does use the functions in MAIN thread.

Just to make it clear one more time and what I did lern so far about those crit functions / threads.

MAIN Thread = accessed ony by one only = I can use variables / buffers just in main thread itself without to worry about any overlapp / free issues by others because others have to wait till actually (EIP etc) is finished.Also in this case about Main thread my logical understanding tells me that I don't need to use ANY EnterC / LeaveC functions in the MAIN thread itself (not sure whether I am the only one who does see it so but my brain tells me YES). :)

Ok wait, I think I got it now. :) So it only makes sense if there is running minimum one another thread who using same object section.In that case the threads X or Main does wait till other one is leaving.Ahhhh! :) Right?I think so.No plan why it was so hard to check that for me but I think now I got it more than before.I was just thinking about MAIN thread / extern thread seperated (somehow like D.C. and not like A.C.) you know.Oh men, hard birth again but the baby is out now (think so). :)

greetz

Link to post
aIjundi
2 hours ago, LCF-AT said:

Why using EnterC / LeaveC in MAIN thread

If a resource/memory is shared between threads and can be modified by anyone of them, all threads using it should lock when accessing/modifying it. This includes the main thread. 

 

2 hours ago, LCF-AT said:

why using it more than once?

I guess what is happening here is they have multiple functions with EnterC / LeaveC, and they are calling one of them from another. Or it could be a recursive call.

  • Like 1
Link to post

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...