flat assembler
Message board for the users of flat assembler.
Index
> Main > thread synchronization Goto page 1, 2 Next |
Author |
|
shoorick 01 Mar 2010, 05:49
OS? events, mutexes, criticalsections...
|
|||
01 Mar 2010, 05:49 |
|
sinsi 01 Mar 2010, 05:59
Isn't there an opcode (prefix?) hint for a spinlock? Depends on what OS you are using I guess...
|
|||
01 Mar 2010, 05:59 |
|
revolution 01 Mar 2010, 06:30
tthsqe: Do you want thread synchronization or thread communication or both. Because they are entirely different things. To synchronise means to ensure order. To communicate means to pass information. You can have one or the other or both, it depends upon your application.
It would help if you gave more information on what you are trying to do otherwise any advice given here will likely be not what you are looking for. And remember to say which OS you are intending to target, that makes a big difference. |
|||
01 Mar 2010, 06:30 |
|
tthsqe 01 Mar 2010, 17:37
Oops, I don't think I meant a CREW machine... anyways, the following situation shoudl suffice:
I have a global variable n=0. When each thread executes n++, I would want it to behave as expected, that is n reflects the total number of times the statement was excuted - so no side effects caused by two threads reading the same value of n at the same time then both writing back n+1 to n when it really should be n+2. Also, windows. |
|||
01 Mar 2010, 17:37 |
|
MHajduk 01 Mar 2010, 17:50
Perhaps you may just use semaphores here? It could be realized with such atomic assembler instructions as XADD and CMPXCHG (check Intel manuals and other resources for proper examples ).
|
|||
01 Mar 2010, 17:50 |
|
baldr 01 Mar 2010, 22:53
MHajduk,
Why not lock inc? Both xadd and cmpxchg look like an overkill (in this particular case). __________ tthsqe, Win32's way to do it is InterlockedIncrement() (XP SP2 implements it via xadd ) |
|||
01 Mar 2010, 22:53 |
|
revolution 01 Mar 2010, 23:05
Note that both xadd and cmpxchg need a lock prefix to make them atomic.
Further note that not all processors support xadd or cmpxchg. Also note that lock inc is fine if you don't care about the previous value. Lastly note that any instruction that involves a lock prefix will seriously degrade performance. For variables that need to be updated frequently this may be a consideration. |
|||
01 Mar 2010, 23:05 |
|
LocoDelAssembly 01 Mar 2010, 23:24
Quote: Further note that not all processors support xadd or cmpxchg. |
|||
01 Mar 2010, 23:24 |
|
baldr 01 Mar 2010, 23:30
revolution,
To be atomic, it have to employ #LOCK protocol. |
|||
01 Mar 2010, 23:30 |
|
revolution 01 Mar 2010, 23:34
baldr wrote: revolution, revolution wrote: Note that both xadd and cmpxchg need a lock prefix to make them atomic. |
|||
01 Mar 2010, 23:34 |
|
revolution 01 Mar 2010, 23:38
LocoDelAssembly wrote:
|
|||
01 Mar 2010, 23:38 |
|
baldr 02 Mar 2010, 00:13
revolution,
Not an echo, just repercussion. My comment was about performance degradation: #LOCK is a must for them to be atomic. |
|||
02 Mar 2010, 00:13 |
|
tthsqe 02 Mar 2010, 08:16
How about if the updating function is not a single instruction like inc that can be used with lock? I was thinking of something like:
Code: start: t = [CT] x = [N] update x if t = [CT] ; if [N] has not been updated in between [N] = x [CT]++ else goto start endif here, N, and CT = 0 are global; x and t are local. But I'm not sure exactly sure what instructions need to be made atomic or how to do this. |
|||
02 Mar 2010, 08:16 |
|
baldr 02 Mar 2010, 08:34
tthsqe,
That's what synchronization objects are for. Look up critical section in MSDN. |
|||
02 Mar 2010, 08:34 |
|
revolution 02 Mar 2010, 08:36
tthsqe: You need a proper spinlock to do atomically.
In the above pseudo-code: between 'if t=[CT]' and '[N]=x' you have a period of vulnerability. It may help you think about what would happen if two CPUs are in perfect lock-step and see what would happen to the memory values. |
|||
02 Mar 2010, 08:36 |
|
tthsqe 02 Mar 2010, 09:47
Ok, I'll look into that more. How about a simple tast synchronization - making sure all thread finish a task before any thread starts the next. I was think of something like this:
Code: ThreadStart: ; start with ct1 = 0 task1 mov [ct2],0 lock inc [ct1] @@: cmp [ct1],[ThreadCount] jl @b task2 mov [ct3],0 lock inc [ct2] @@: cmp [ct2],[ThreadCount] jl @b task3 mov [ct1],0 lock inc [ct3] @@: cmp [ct3],[ThreadCount] jl @b ... It seems that we need to cycle through three counters to avoid any possible side effects. |
|||
02 Mar 2010, 09:47 |
|
revolution 02 Mar 2010, 10:01
tthsqe: I think you are trying to reinvent the wheel.
Perhaps you would like to read up about WaitForMultipleObjects and CreateSemaphore. Don't try to fight the OS, use it for what it was designed for. |
|||
02 Mar 2010, 10:01 |
|
revolution 02 Mar 2010, 10:27
tthsqe wrote: How about if the updating function is not a single instruction like inc that can be used with lock? I was thinking of something like: ¹ Using xchg has an implicit lock, so that also won't allow you to avoid the bus lock. |
|||
02 Mar 2010, 10:27 |
|
hopcode 02 Mar 2010, 13:28
perhaps useful
http://www.azillionmonkeys.com/qed/asmexample.html browse to para 15 .63 bit atomic counter. Cheerz, hopcode |
|||
02 Mar 2010, 13:28 |
|
Goto page 1, 2 Next < Last Thread | Next Thread > |
Forum Rules:
|
Copyright © 1999-2025, Tomasz Grysztar. Also on GitHub, YouTube.
Website powered by rwasa.