Page 1 of 1

linked list as parameter to thread

Posted: Mon Sep 19, 2011 2:59 pm
by AndyMK
Is it possible to pass a linked list as a parameter to a thread?

Re: linked list as parameter to thread

Posted: Mon Sep 19, 2011 3:18 pm
by Zefz
Shouldnt something simple like this work?

Code: Select all

Procedure MyFunction(List Parameter)
  ProcedureReturn ListSize( Parameter() )
EndProcedure

NewList MyList()
CreateThread( @MyFunction(), @MyList() )

Re: linked list as parameter to thread

Posted: Mon Sep 19, 2011 3:26 pm
by AndyMK
didn't work.

Code: Select all

Structure Packets
  string.s
EndStructure

NewList Packet.Packets()
AddElement(Packet())
Packet()\string = "hi"

Procedure Network(???)
  Debug packet()\string
EndProcedure

CreateThread(@Network(), @Packet())
I wan't the thread to have access to the linked list. Should i just use a global linked list?

Re: linked list as parameter to thread

Posted: Mon Sep 19, 2011 4:39 pm
by luis
AFAIK the supported parameter you can pass to a thread it's a number.

It can be used the way you like, even as a pointer. But lists, maps, etc, all have a dedicate keyword to be used in the receiving procedure, and only using that keyword you can then use the PB commands to access/modify the list, map, etc.

So I would say you can't pass them to a thread and you have to declare them as global to access them, using a mutex if needed.

Different would be with a list you made using pointers and some code you wrote to manage it. You would need only a pointer then.

Re: linked list as parameter to thread

Posted: Mon Sep 19, 2011 4:45 pm
by AndyMK
I am going to go with a global list and mutex, thank you

Re: linked list as parameter to thread

Posted: Mon Sep 19, 2011 9:12 pm
by STARGÅTE
use a list inside a structure:

Code: Select all

Structure Packets
  string.s
EndStructure

Structure PacketsList
	List Packet.Packets()
EndStructure

Define PacketsList.PacketsList

AddElement(PacketsList\Packet())
PacketsList\Packet()\string = "hi"

Procedure Network(*PacketsList.PacketsList)
  Debug *PacketsList\Packet()\string
  AddElement(*PacketsList\Packet())
  *PacketsList\Packet()\string = "next hi"
EndProcedure

Define Thread = CreateThread(@Network(), @PacketsList)

WaitThread(Thread)

Debug "--"
ForEach PacketsList\Packet()
	Debug PacketsList\Packet()\string
Next

Re: linked list as parameter to thread

Posted: Tue Sep 20, 2011 8:25 am
by AndyMK
STARGÅTE wrote:use a list inside a structure:

Code: Select all

Structure Packets
  string.s
EndStructure

Structure PacketsList
	List Packet.Packets()
EndStructure

Define PacketsList.PacketsList

AddElement(PacketsList\Packet())
PacketsList\Packet()\string = "hi"


Thanks Stargate  :D Do i still need to use a mutex if i am manipulating the list outside of the thread?
Procedure Network(*PacketsList.PacketsList)
  Debug *PacketsList\Packet()\string
  AddElement(*PacketsList\Packet())
  *PacketsList\Packet()\string = "next hi"
EndProcedure

Define Thread = CreateThread(@Network(), @PacketsList)

WaitThread(Thread)

Debug "--"
ForEach PacketsList\Packet()
	Debug PacketsList\Packet()\string
Next
Thanks Stargate. If i am manipulating the list outside of the thread, do i need to use mutex?

Re: linked list as parameter to thread

Posted: Tue Sep 20, 2011 10:55 am
by luis
Thanks Stargate. If i am manipulating the list outside of the thread, do i need to use mutex?
The mutex is needed if you access the list from more than one thread and not in readonly mode.
It's needed if at least one thread is modifying the list and some other thread is reading or modifying it too.

Just imagine to NOT use a mutex. Can some of your code running in a thread find itself reading some wrong data because another thread is halfway to complete an alteration to the same data ? If the answer is yes, than you need a mutex.

Re: linked list as parameter to thread

Posted: Tue Sep 20, 2011 11:39 am
by AndyMK
luis wrote:
Thanks Stargate. If i am manipulating the list outside of the thread, do i need to use mutex?
The mutex is needed if you access the list from more than one thread and not in readonly mode.
It's needed if at least one thread is modifying the list and some other thread is reading or modifying it too.

Just imagine to NOT use a mutex. Can some of your code running in a thread find itself reading some wrong data because another thread is halfway to complete an alteration to the same data ? If the answer is yes, than you need a mutex.
Thanks for the confirmation.

Re: linked list as parameter to thread

Posted: Tue Sep 20, 2011 1:51 pm
by PMV
luis wrote:
Thanks Stargate. If i am manipulating the list outside of the thread, do i need to use mutex?
The mutex is needed if you access the list from more than one thread and not in readonly mode.
If a linked list is accessed by two threads, regardless if
only read and/or write, it needs mutexprotection. A linked
list has only one possible current element, so if one
thread reads one element and the other wants to read
another element ... it will result in undefined results. :)

MFG PMV

Re: linked list as parameter to thread

Posted: Tue Sep 20, 2011 4:49 pm
by luis
@PMV

Yes, I have overlooked that, thank you :oops:

The fact is the list contains "states" even if data is not modified.

From the point of view of a thread even if the user-data is not changed in read-only mode, internal states can change, so there is some "change" to the data-structure. That's enough to create havoc.

@AndyMK: I apologize for the imprecision.

Re: linked list as parameter to thread

Posted: Tue Sep 20, 2011 7:30 pm
by AndyMK
luis wrote:@PMV

Yes, I have overlooked that, thank you :oops:

The fact is the list contains "states" even if data is not modified.

From the point of view of a thread even if the user-data is not changed in read-only mode, internal states can change, so there is some "change" to the data-structure. That's enough to create havoc.

@AndyMK: I apologize for the imprecision.
no probs, i got it working :D