News:

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

Multi thread download demo.

Started by hutch--, May 30, 2007, 07:03:41 AM

Previous topic - Next topic

hutch--

This demo is a more conventional usage of multithreading in that it asynchronously downloads 5 file. It uses a structure address passed on the stack to not only pass the arguments but also a flag for the calling thread so it does not exit before the arguments are read into the thread procedure. The caller runs a simple spinlock with a SleepEx() call so it yields to other processes while its idling waiting for the flag to change in the last thread called.

The general drift is that multi threading is not rocket science in programming terms, once you have a general idea of how it works its reasonably straight forward to write. Its diffficult to write what you may call a typical demo because the range of variation in multi threading is so large. It just involves some reverse learning from the old days of single task processing that was done in sequence.

With the coming generation of multicore processors there will be big gains in processing speeds for certain types of tasks if they can be divide up between multiple threads that run in parallel.

[attachment deleted by admin]
Download site for MASM32      New MASM Forum
https://masm32.com          https://masm32.com/board/index.php

ecube

Hutch,
Thanks for the great example, i'm hoping this spin lock technique will help me with  problems i've had with multi-threading. One issue I had and overcame was at the end of a created thread I just ended it with "ret" and after awhile the application would crash randomly, I still don't know why but I fixed that problem with

invoke GetCurrentThread
invoke TerminateThread,eax,0

i've found issues in single threaded tests(not loops just single function calls) where code actually seems to execute *too fast and I get unexpected results or crashes, i'm thinking that may be a register preservation issue but thus far tossing in a invoke Sleep,num seems to fix that. In any case my point is i'm hoping your spinlock example cures this, so thanks  :bg


hutch--

Cube,

In tis context the spinlock does the job fine but keep in mind when synchronising multiple threads that you may be better off using what is called a CRITICAL SECTION which is provided by the OS for synchronising access to data so you don't get collisions.

Also note that this spinlock in not timing critical and depends on the resolution you an get with SleepEx() where if you have critical timings you may need to remove the SleepEx() call if you are working on much finer resolutions. A spinlock is more an OS timing technique but it has clean and simple logic where it can be used.
Download site for MASM32      New MASM Forum
https://masm32.com          https://masm32.com/board/index.php