News:

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

Threading

Started by Astro, July 14, 2009, 08:36:46 PM

Previous topic - Next topic

Astro

Hi,

By default is an app written using MASM single-threaded? In other words, I'd have to deliberately code it to be multi-threaded?

This is important as my software MUST be single-threaded.

Thanks!!  :bg

bruce1948

Yes! To be be multi-threaded you would have to invoke another thread.

dedndave

normally, an app is single threaded
if you have a multi-core computer, things may be different
newer pentiums allow for "out-of-order" execution of instructions - this can result in a process using another core
i am not certain if this actually falls in the catagory of "another thread", per se
but, i have experienced problems while timing code due to the program reading time stamp counter values from two different cores

you can use GetProcessAffinityMask and SetProcessAffinityMask to read/control which/how many of the cores are used
this is a simple program that limits execution to a single core, then switches back to the initial setting prior to exit
while the program is running, you may verify the core selection by using the Task Manager

        INCLUDE \masm32\include\masm32rt.inc

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

        .DATA?

hProc   dd ?            ;process handle
AffSyst dd ?            ;system affinity mask
AffProc dd ?            ;process affinity mask

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

        .CODE

_main   PROC

;get and store the process handle

        INVOKE  GetCurrentProcess
        mov     hProc,eax

;get the original affinity masks

        INVOKE  GetProcessAffinityMask,
                eax,
                ADDR AffProc,
                ADDR AffSyst

;select only "core 0"

        INVOKE  SetProcessAffinityMask,
                hProc,
                1

;wait for a keypress

        inkey

;set affinity to original condition

        INVOKE  SetProcessAffinityMask,
                hProc,
                AffProc

;terminate

        exit

_main   ENDP

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

        END     _main

Astro

Excellent. Thank you.

Quotethen switches back to the initial setting prior to exit
Is this required? What is the effect of not doing this?

dedndave

no - not a requirement, as the process only controls it's own affinity and is terminated
i put that in there as example code, is all
you may have parts of the program that you want to run with all cores and parts that are limited to one core

bruce1948

It is not actually multithreading. According to microsoft:-

The scheduler maintains a queue of executable threads for each priority level. These are known as ready threads. When a processor becomes available, the system performs a context switch. The steps in a context switch are:

                1.   Save the context of the thread that just finished executing.
   2.   Place the thread that just finished executing at the end of the queue for its priority.
   3.   Find the highest priority queue that contains ready threads.
   4.   Remove the thread at the head of the queue, load its context, and execute it.

So your 1 thread  will run on whichever processor/core becomes availble first, leastways that's how I read it.

dedndave

from experience, i can tell you that a single thread can run on more than one core
it is still considered a single thread
this is all it takes to confine it to a single core, however

        INVOKE  GetCurrentProcess
        mov     hProc,eax
        INVOKE  SetProcessAffinityMask,
                hProc,
                1

bruce1948

Quote from: dedndave on July 14, 2009, 10:40:14 PM
from experience, i can tell you that a single thread can run on more than one core
it is still considered a single thread


What I said.

Astro

OK - so I start my app, and even though it is single threaded, could run on any processor/core when it's turn to execute next comes up?

Interesting.

I don't think it will affect my app - it only states that it is single threaded, it doesn't say it must run on the same processor/core as another thread.

This is what I'm working on currently: http://msdn.microsoft.com/en-us/library/aa375198(VS.85).aspx#stub

EDIT: Hmmmmm....... actually.....

QuoteAll calls between a GINA and Winlogon must be within a single thread.
Does this mean my app could actually be MULTI threaded, so long as the CALLS are not split across threads?

Something is nagging in the back of my mind about concurrency and the fact that it is possible to have threads execute out of order, thus meaning a thread requiring the result of another thread could execute first (out of order). Sometimes the system can detect this and queue them accordingly, other times it won't, and can cause a ?race condition?. I'm not sure of this could generate a threading exception error (sorry if I just made that up - it's late!).  :dazzled:

That would be bad in my case. If it was single threaded however, if it is splitting across processors, could this affect the result??? It's not going to leave my app hanging mid-routine is it?

My head is going to explode...  :eek

Back tomorrow! hehe!

dedndave

no - a sequence of instructions may be performed out of order if they are not dependant
if an instruction is dependant on a previous instruction to be completed,
the processor waits for the first one to be completed (or looks for something else to do)
all that seems a bit silly to me, as the overhead for keeping track would seem worse than the penalty to begin with
but, they seem to have found ways to do it that improve overall performance, because they keep doing it - lol

for what you want to do, running on multiple cores may not be an issue
i mentioned the affinity functions because i have had troubles with it
they are fairly simple, although ms has found a way to over-complicate them for Win7 - lol

sinsi

Don't confuse multi-threading and multi-cpu's. Even a 386 can multi-thread with a single CPU - it's all how the OS does it.
Regardless of which CPU your single-thread app runs on, the environment is the same (memory, next instruction etc.) with a few exceptions (rdtsc for example).
Light travels faster than sound, that's why some people seem bright until you hear them.

Astro

Hi,

QuoteDon't confuse multi-threading and multi-cpu's.
My impression was that a single threaded app started executing on "a" processor/core, then continued to execute exclusively on that processor/core until it was terminated.

Quotefor what you want to do, running on multiple cores may not be an issue
It will be interesting to see. I also want to write a driver. One thing at a time though!  :dazzled:

Quotei mentioned the affinity functions because i have had troubles with it
they are fairly simple, although ms has found a way to over-complicate them for Win7 - lol
Good ol' MS!  ::)

Tedd

To avoid confusions people seem to be injecting...

Unless you create extra threads, your program is singe-threaded.
That single thread will run on ONE single processor core at a time.
If the machine contains multiple cores/processors, your program will still only run on ONE at any instance in time.
It may migrate between cores during context switches, but this should be transparent to the running of your program, unless you specifically check which core you're running on.
You can fix your thread to run only on a specific core by setting its affinity to that core. However, this may limit the running of your thread since that core will also be used by other threads, even while the other cores are free and ready to run.
No snowflake in an avalanche feels responsible.

Astro

Hi Tedd,

Regarding one thread running on one core, that is what I thought (only I thought the entire program ran on the same core/processor all the time).  Thanks for clarifying. :U

dedndave

that directly contradicts what i was saying
only one of us can be right - lol
a single-threaded process may use more than one core
you may verify this by writing a little test program
use the CPUID instruction to get the core number
read it several times
you will see it change
i have a dual-core "prescott" processor
give me a few minutes and i will write it and post results.....