Is PeekA and PokeA Threadsave?

Just starting out? Need help? Post your questions and find answers here.
Cyllceaux
Enthusiast
Enthusiast
Posts: 458
Joined: Mon Jun 23, 2014 1:18 pm
Contact:

Is PeekA and PokeA Threadsave?

Post by Cyllceaux »

In this example I experimented with threads and Memory. I tested with an threadsave executable.

The CompareMemory always gives 0 by #VAL greater than 600.

I'm not sure if this is a bug or I'm doing something wrong.

Code: Select all

EnableExplicit
Define g

#VAL=4000

Procedure _Code_MemoryDecodeSingleExt(*mem,size,seed.q)
	Protected.q i
	For i=0 To (size-1):PokeA(*mem+i,PeekA(*mem+i)-seed):Next
EndProcedure


Define *mem=AllocateMemory(#VAL)
Define *mem2=AllocateMemory(#VAL)

Define start=ElapsedMilliseconds()
For g=0 To 10
	_Code_MemoryDecodeSingleExt(*mem,#VAL,g)
Next
Define ende=ElapsedMilliseconds()


Structure seed
	seed.i
	tt.i
	thread.i
	*mem
EndStructure


Procedure rThread(*data.seed)	
	_Code_MemoryDecodeSingleExt(*data\mem,#VAL,*data\seed)
EndProcedure

NewList tt.seed()

Define start2=ElapsedMilliseconds()
For g=0 To 10
	Define *t.seed=AddElement(tt())
	*t\seed=g
	*t\mem=*mem2
	*t\thread=CreateThread(@rThread(),*t)
Next

ForEach tt()
	If IsThread(tt()\thread)
		WaitThread(tt()\thread)
	EndIf
Next
Define ende2=ElapsedMilliseconds()

Debug "Normal: "+Str(ende-start)
Debug "Thread: "+Str(ende2-start2)

Debug CompareMemory(*mem,*mem2,#VAL)

fryquez
Enthusiast
Enthusiast
Posts: 362
Joined: Mon Dec 21, 2015 8:12 pm

Re: Is PeekA and PokeA Threadsave?

Post by fryquez »

Code: Select all

Define *t.seed=AddElement(tt())
Can change the pointer before the thread has read it.
Make a copy, pass this one to the thread and free it in the thread.
Cyllceaux
Enthusiast
Enthusiast
Posts: 458
Joined: Mon Jun 23, 2014 1:18 pm
Contact:

Re: Is PeekA and PokeA Threadsave?

Post by Cyllceaux »

Hey @fryquez

Thx... But I think it is another problem.

Code: Select all

EnableExplicit

Define *mem=AllocateMemory(2)
Define *mem2=AllocateMemory(2)
Define *mem3=AllocateMemory(2)
Define *mem4=AllocateMemory(2)

PokeA(*mem+1,PeekA(*mem)-1)
PokeA(*mem+1,PeekA(*mem)-2)
PokeA(*mem+1,PeekA(*mem)-3)


Debug PeekI(*mem); 64768


PokeA(*mem2+1,PeekA(*mem2)-3)
PokeA(*mem2+1,PeekA(*mem2)-2)
PokeA(*mem2+1,PeekA(*mem2)-1)


Debug PeekI(*mem2) ;65280


PokeA(*mem3+1,PeekA(*mem3)-2)
PokeA(*mem3+1,PeekA(*mem3)-3)
PokeA(*mem3+1,PeekA(*mem3)-1)


Debug PeekI(*mem3) ;65280

PokeA(*mem4+1,PeekA(*mem4)-1)
PokeA(*mem4+1,PeekA(*mem4)-3)
PokeA(*mem4+1,PeekA(*mem4)-2)


Debug PeekI(*mem4) ;65024
I thought PokeA only chances 1 Value. So these have to be the same... But it isn't.

So the threads are running different fast and the poke is not the right order :/
infratec
Always Here
Always Here
Posts: 6817
Joined: Sun Sep 07, 2008 12:45 pm
Location: Germany

Re: Is PeekA and PokeA Threadsave?

Post by infratec »

How long is an integer :?:

I think minimum 4 bytes.

But you allocate only 2 : :wink:

So your PeekI() returns nothing valid.

Enable the Purifier with Granularity 1,1,1,1.


In general if you want to access same memory locations in threads, you need, like with other variables too, a mutex to prevent
accesses at the same time.
Cyllceaux
Enthusiast
Enthusiast
Posts: 458
Joined: Mon Jun 23, 2014 1:18 pm
Contact:

Re: Is PeekA and PokeA Threadsave?

Post by Cyllceaux »

Hey infratec

I changed to this

Code: Select all

PurifierGranularity(1,1,1,1)

Define *mem=AllocateMemory(SizeOf(Integer))
Define *mem2=AllocateMemory(SizeOf(Integer))
Define *mem3=AllocateMemory(SizeOf(Integer))
Define *mem4=AllocateMemory(SizeOf(Integer))
same different results. Purifier doesn't change anything.
User avatar
NicTheQuick
Addict
Addict
Posts: 1224
Joined: Sun Jun 22, 2003 7:43 pm
Location: Germany, Saarbrücken
Contact:

Re: Is PeekA and PokeA Threadsave?

Post by NicTheQuick »

Usually an integer has 8 bytes except when you are working on vintage systems with 32 bit
Cyllceaux wrote: Fri Jan 21, 2022 3:21 pm So the threads are running different fast and the poke is not the right order :/
Threads are managed by the scheduler of your operating system. You have to be aware of all the possible context switches that can occur. If you want to control that correctly, use Mutex's and Semaphores.
The english grammar is freeware, you can use it freely - But it's not Open Source, i.e. you can not change it or publish it in altered way.
Cyllceaux
Enthusiast
Enthusiast
Posts: 458
Joined: Mon Jun 23, 2014 1:18 pm
Contact:

Re: Is PeekA and PokeA Threadsave?

Post by Cyllceaux »

I understand...
Thanks @all

I'm still surprised that

Code: Select all

PokeA(*mem+1,PeekA(*mem)-1)
PokeA(*mem+1,PeekA(*mem)-2)
PokeA(*mem+1,PeekA(*mem)-3)
gives a different result than

Code: Select all

PokeA(*mem2+1,PeekA(*mem2)-3)
PokeA(*mem2+1,PeekA(*mem2)-2)
PokeA(*mem2+1,PeekA(*mem2)-1)
----------------------------------------------------------
but

Code: Select all

PokeA(*mem2+1,PeekA(*mem2)-3)
PokeA(*mem2+1,PeekA(*mem2)-2)
PokeA(*mem2+1,PeekA(*mem2)-1)
gives same results as

Code: Select all

PokeA(*mem3+1,PeekA(*mem3)-2)
PokeA(*mem3+1,PeekA(*mem3)-3)
PokeA(*mem3+1,PeekA(*mem3)-1)
User avatar
NicTheQuick
Addict
Addict
Posts: 1224
Joined: Sun Jun 22, 2003 7:43 pm
Location: Germany, Saarbrücken
Contact:

Re: Is PeekA and PokeA Threadsave?

Post by NicTheQuick »

Nothing to be surprised of. You always substract a number from zero and store it in the same byte. In the end the last substraction is what you are seeing.

Code: Select all

EnableExplicit

Macro DataType
	Integer
EndMacro

Macro hexword(ptr)
	RSet(Hex(PeekU(ptr), #PB_Quad), SizeOf(DataType) * 2, "0")
EndMacro

Define *mem=AllocateMemory(SizeOf(DataType))
Define *mem2=AllocateMemory(SizeOf(DataType))
Define *mem3=AllocateMemory(SizeOf(DataType))
Define *mem4=AllocateMemory(SizeOf(DataType))

Debug "0a: " + hexword(*mem)
PokeA(*mem+1,PeekA(*mem)-1)
Debug "0b: " + hexword(*mem)
PokeA(*mem+1,PeekA(*mem)-2)
Debug "0c: " + hexword(*mem)
PokeA(*mem+1,PeekA(*mem)-3)
Debug "0d: " + hexword(*mem)

Debug ""

Debug "2a: " + hexword(*mem2)
PokeA(*mem2+1,PeekA(*mem2)-3)
Debug "2b: " + hexword(*mem2)
PokeA(*mem2+1,PeekA(*mem2)-2)
Debug "2c: " + hexword(*mem2)
PokeA(*mem2+1,PeekA(*mem2)-1)
Debug "2d: " + hexword(*mem2)

Debug ""

Debug "3a: " + hexword(*mem3)
PokeA(*mem3+1,PeekA(*mem3)-2)
Debug "3b: " + hexword(*mem3)
PokeA(*mem3+1,PeekA(*mem3)-3)
Debug "3c: " + hexword(*mem3)
PokeA(*mem3+1,PeekA(*mem3)-1)
Debug "3d: " + hexword(*mem3)

Debug ""

Debug "4a: " + hexword(*mem4)
PokeA(*mem4+1,PeekA(*mem4)-1)
Debug "4b: " + hexword(*mem4)
PokeA(*mem4+1,PeekA(*mem4)-3)
Debug "4c: " + hexword(*mem4)
PokeA(*mem4+1,PeekA(*mem4)-2)
Debug "4d: " + hexword(*mem4)
The english grammar is freeware, you can use it freely - But it's not Open Source, i.e. you can not change it or publish it in altered way.
Cyllceaux
Enthusiast
Enthusiast
Posts: 458
Joined: Mon Jun 23, 2014 1:18 pm
Contact:

Re: Is PeekA and PokeA Threadsave?

Post by Cyllceaux »

:oops: :oops:

Yes... you are right... now I see it... oh... how embarrassing...
Post Reply