fOdder has just given me some advice about using global variables in a thread........"not a good idea"
So i was wondering if it is a bad idea to call a macro from a thread that also uses Global Variables.
The problem i have is i want to let a bitmap scroll across the top of my window several times by a call to a thread.
Here is the code for my thread...i'm a newbie so feel free to critisize it to death but give me some suggestions also if you have any. All variables not defined local are global.
___________________________________________________
ThreadProc PROC USES ebx Param:DWORD
LOCAL var3:DWORD,var2:DWORD,var1:DWORD
mov var3, 0
invoke LoadBitmap,hInstance,35
mov hBitmap,eax
invoke SelectObject,cdc,hBitmap
mov obitmap,eax
.while var3 < 10 ;loop ten times
mov var1, 0
.while var1 < 290 ;<< Bitmap width
invoke BitBlt,hdc,XPOS,YPOS,BMW,BMH,cdc,var1,0,SRCCOPY
invoke GetTickCount
mov var2, eax
add var2, 5 ; delay
.while eax < var2
invoke GetTickCount
.endw
add var1,3
.endw
inc var3
.endw
ret 0
ThreadProc END
So i was wondering if it is a bad idea to call a macro from a thread that also uses Global Variables.
The problem i have is i want to let a bitmap scroll across the top of my window several times by a call to a thread.
Here is the code for my thread...i'm a newbie so feel free to critisize it to death but give me some suggestions also if you have any. All variables not defined local are global.
___________________________________________________
ThreadProc PROC USES ebx Param:DWORD
LOCAL var3:DWORD,var2:DWORD,var1:DWORD
mov var3, 0
invoke LoadBitmap,hInstance,35
mov hBitmap,eax
invoke SelectObject,cdc,hBitmap
mov obitmap,eax
.while var3 < 10 ;loop ten times
mov var1, 0
.while var1 < 290 ;<< Bitmap width
invoke BitBlt,hdc,XPOS,YPOS,BMW,BMH,cdc,var1,0,SRCCOPY
invoke GetTickCount
mov var2, eax
add var2, 5 ; delay
.while eax < var2
invoke GetTickCount
.endw
add var1,3
.endw
inc var3
.endw
ret 0
ThreadProc END
Threads should not end...
unless a global variable tolds them to do so
1.Threads are first setup
2.Threads are started
3.Eventually threads are stopped (terminated)
The ThreadProc is a CALLBack kind of...windows will call it whenever it likes and Windows will get you out from it anywhere it so likes (i mean at any code line) ...so general CallBack register preservations apply here but you must take a lot of other precautions
You can use any global variables in a thread but you get into problems when the same variables are used by another thread for read/write. If you use only one atomic opeartion to read/write them then its safe
And your main win32 application is another thread ;)
So RET at the end is useless, you should jump back to the start
I am also unsure if the local variables are usefully to something as they just unbalance the threads stack ;)
Here is an example from our simple MOUSE THREAD (you dont want to see the complicated PAINT THREAD ;) )
sorry but TASM style
unless a global variable tolds them to do so
1.Threads are first setup
2.Threads are started
3.Eventually threads are stopped (terminated)
The ThreadProc is a CALLBack kind of...windows will call it whenever it likes and Windows will get you out from it anywhere it so likes (i mean at any code line) ...so general CallBack register preservations apply here but you must take a lot of other precautions
You can use any global variables in a thread but you get into problems when the same variables are used by another thread for read/write. If you use only one atomic opeartion to read/write them then its safe
And your main win32 application is another thread ;)
So RET at the end is useless, you should jump back to the start
I am also unsure if the local variables are usefully to something as they just unbalance the threads stack ;)
Here is an example from our simple MOUSE THREAD (you dont want to see the complicated PAINT THREAD ;) )
sorry but TASM style
;*******************************************
; THREAD FLIP OPERATIONS
;
; This is done for only one reason:
; -faster/constant mouse redraw even if poor
; render speed
;*******************************************
.data
ALIGN 4
;
lp_thread_id dd 0
thread_handle dd 0
thread_counter dd 0
.code
Thread_Flip_Setup:
mov eax,offset lp_thread_id
push eax
mov eax,0 ;creation flags
push eax
mov eax,0 ;lparam
push eax
mov eax,offset Thread_Flip_CallBack
push eax
mov eax,0 ;stack size
push eax
mov eax,0 ;security attributes
push eax ; i wonder if this is ok for Win2K ?
call CreateThread
mov [thread_handle],eax
;==========================================
; set priority level
; this will be realtime but it will sleep a lot
; after its job is done
;==========================================
call SetThreadPriority,[thread_handle],THREAD_BASE_PRIORITY_MAX
ret
;*********************************************
; THREAD CALLBACK
;*********************************************
.data
ALIGN 4
;
thread_can_flip dd 0
thread_dies_now dd 0
.code
ALIGN 4
Thread_Flip_CallBack proc uses ecx , param01:DWORD
loop_thread:
mov eax,offset csection_flip
push eax
Call EnterCriticalSection
mov eax,[thread_dies_now]
cmp eax,0
jz thread_goes_on
xor eax,eax
ret
thread_goes_on:
mov eax,[thread_can_flip]
cmp eax,0
jz thread_flip_sleep
;=======================================
; Our Only actions on this thread are here
;=======================================
;==========================================
; read the mouse and cache values for latter read
; this is also esential for the Thread_paint
;==========================================
call Read_Mouse_Flip
call paint_back_video_to_back
call paint_mouse
call FlipBuffers ; flip back/primary surfaces
thread_flip_sleep:
;===========================================
; prepare to sleep but
; first end critical section
;===========================================
mov eax,offset csection_flip
push eax
Call LeaveCriticalSection
;=========================================
; this regulates the mouse speed
; mouse will go better / game slower if smaller then 35 ?
;=========================================
; mov eax,16
; mov eax,25
mov eax,16
call Sleep,eax
;=========================================
jmp loop_thread
;------------------------------
; we never reach here
;------------------------------
ret
Thread_Flip_CallBack endp