hello,everybody.

my question is about the CreateThread. for example.
;============
func1 proc
    ....
    ret
func1 endp
;============
func2 proc
    ....
    ret
func2 endp
;============

start:
    .....
    invoke func1; A
    invoke CreateThread,NULL,0,addr func2,ecx,THREAD_PRIORITY_NORMAL,NULL ;B
    .....
;====================================================================
1. what's the difference of above A and B usage?
2. which the case should be use B?

regards.

Posted on 2005-03-22 20:48:52 by dcskm4200
In your example "func1" is executed before "func2"

If you create the thread "func2" before calling "func1"
invoke CreateThread,NULL,0,addr func2,ecx,THREAD_PRIORITY_NORMAL,NULL ;B
invoke func1; A

the 2 procedures will be running at the same time (OS will share the time to run them)

You can uses threads for multiple things (tasks): a big one is related to non blocking things.
Lot of programs uses threads to executed the "saving operation".
Example: when somebody saves a document in MS Word: the program does not get blocked to perform this operation.
Why? Because it creates the thread "save_document_proc" (or something like that).

See the Iczelion's tutorial number 15 (check 14 and 16 too) to learn how to use them (http://win32asm.cjb.net/).

Multithreading is a litle more complicated that my explanation, but it is enought to start and get involved with it. Hope it helps.

Regards.

Kecol.-
Posted on 2005-03-22 23:23:01 by Kecol
hey, Kecol.-

Thanks for your help.

regards.
Posted on 2005-03-23 00:28:37 by dcskm4200
hey,Kecol.

I read the Iczelion's tutorial 14,15,16 again. I comprehendid the following:
A process consists of at least one thread: the primary thread.? the primary thread is created with "start:" by the windows.

if I give them a sequence running, I should write the following:
;=============
start:
...
invoke func1
invoke func2
...
;=============
at the time, there is only one thread: the primary thread.

;*********************************************************************

if I give them a simultaneity running, I should write the following:
;=============
start:
...
invoke CreateThread,NULL,0,addr func1,ecx,THREAD_PRIORITY_NORMAL,NULL
invoke CreateThread,NULL,0,addr func2,ecx,THREAD_PRIORITY_NORMAL,NULL
...
;=============
at the time, there are three threads: the primary thread,the func1 thread and the func2 thread.


Is it right ?


regards
Posted on 2005-03-23 08:16:44 by dcskm4200
Yes, and there are 3 thread running, two that are your functions and the main thread ;) (the one that launch them)

Also from the WinAPI

A process, in the simplest terms, is an executing program. One or more threads run in the context of the process. A thread is the basic unit to which the operating system allocates processor time. A thread can execute any part of the process code, including parts currently being executed by another thread.

... Each process provides the resources needed to execute a program. ...


I think is a little more clear than a process consis at less from a thread (and what more?... is the left question), because in that way is only talking about a piece of data that can be executed (in a time slice), but not about the resources for have a such piece, like the memory space, data, code, handles, in general the enviroment where the threads are running.
Posted on 2005-03-23 09:23:36 by rea
Here's another way to look at those two procedures.

The "invoke proc1" will not return control to the main app until proc1 has completed its task. Meanwhile, all messages sent to the main app would simply accumulate in the message queue and cannot be acted upon unless proc1 also peeks at the message queue.

The "CreateThread proc2" returns control immediately to the main app. The proc2 then shares CPU time with the main app until proc2 terminates. Any message sent to the main app can be acted upon "immediately" and used to terminate proc2 prematurily if necessary. Some additional code is thus required in both the main app and proc2 for communication purposes between them.

A thread is useful whenever a part of the code would take a relatively significant amount of time to execute and messages must continue to be processed without interruption. Gaming programs would typically use threads.

Raymond
Posted on 2005-03-23 11:56:39 by Raymond

A process consists of at least one thread: the primary thread.? the primary thread is created with "start:" by the windows.

The primary thread is not created by "start:", it is created by Windows after your program has been "loaded". After your program is loaded, the primary thread starts at whatever the start address is. (It can be specified either with END in MASM code, or /ENTRY: in linker options.)

Another view of multithreading is that it's a trick or technique for making a single processor look like multiple processors.
Posted on 2005-03-23 16:57:12 by tenkey
Hey, rea, Raymond,tenkey.

thanks you for your help. I'v been improved some.

the primary thread starts at whatever the start address is. (It can be specified either with END in MASM code, ...

I don't understand the above explain. I think the primary thread starts at "start:" where is been written by a coder.

There are three of the communication methods between threads:

Using global variables
Windows messages
Event

1. what's the communication methods between processes?( A.exe and B.exe)

;=============================================================
long time ago, I played the chess on a SUN SPARC TWO workstation. I setted computer vs me in one window of the chess, another window of the chess, I setted me vs computer. Actually, it was the computer vs computer. All was worked fine.

today, I will play the game on my pc( os: XP) again. but I can't do that.( msg: the chess.exe already has running.)? so I think UNIX is better than XP on the point. XP is not actual time-partition system. for example:
if a proc is running look like as following:

;--------------------------------------------
start:
.....
;====================
func1 proc
loop1:
? ? ? ? mov? ecx,600000000
loop2:
? ? ? ? mov? eax,02h
? ? ? ? dec? ecx
? ? ? ? jz? ?loop1
? ? ? ? jmp? Loop2
? ? ? ? ret? ? ?
func1 endp
;====================
....
invoke func1
....

;---------------------------------------------

we will can't do anything. only power off. On UNIX, we can do something.

regards.

Posted on 2005-03-24 00:11:40 by dcskm4200
The thing with a programm instance already running is a design of the application, some ones only whant that one instance of their application is runned at a time.... (search the board for single instance)

By the way, if you load the first thread of your application with such a loop, until terminate such secuence the first thread will not do anything more because is what you have programmed ;) cant dispatch messages or start other threads... because is only a decrement and some jumps.... if you start a thread with such secuence, then there will be 2 threads, the first thread (launched by the OS) and the second thread launched by you, then because in the first thread is doing other thing than sub and jump (in the other thread), you can interact with the first thread, like snd a message for stop the other thread  or terminate the other thread.

The point is that depend on how you design your application and understand the concept of the threads, the only thread created by the OS directly is the first thread, all the other threads in your app will be launched by the design of the rogrammer ;).
Posted on 2005-03-24 10:17:35 by rea
hey, rea

Thanks for your help.
I will study single instance.

best regards
Posted on 2005-03-25 01:31:29 by dcskm4200