linked list as parameter to thread
linked list as parameter to thread
Is it possible to pass a linked list as a parameter to a thread?
Re: linked list as parameter to thread
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
didn't work.
I wan't the thread to have access to the linked list. Should i just use a global linked list?
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())
Re: linked list as parameter to thread
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.
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.
"Have you tried turning it off and on again ?"
Re: linked list as parameter to thread
I am going to go with a global list and mutex, thank you
Re: linked list as parameter to thread
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
PB 6.01 ― Win 10, 21H2 ― Ryzen 9 3900X, 32 GB ― NVIDIA GeForce RTX 3080 ― Vivaldi 6.0 ― www.unionbytes.de
Lizard - Script language for symbolic calculations and more ― Typeface - Sprite-based font include/module
Lizard - Script language for symbolic calculations and more ― Typeface - Sprite-based font include/module
Re: linked list as parameter to thread
Thanks Stargate. If i am manipulating the list outside of the thread, do i need to use mutex?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
Re: linked list as parameter to thread
The mutex is needed if you access the list from more than one thread and not in readonly mode.Thanks Stargate. If i am manipulating the list outside of the thread, do i need to use mutex?
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.
"Have you tried turning it off and on again ?"
Re: linked list as parameter to thread
Thanks for the confirmation.luis wrote:The mutex is needed if you access the list from more than one thread and not in readonly mode.Thanks Stargate. If i am manipulating the list outside of the thread, do i need to use mutex?
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
If a linked list is accessed by two threads, regardless ifluis wrote:The mutex is needed if you access the list from more than one thread and not in readonly mode.Thanks Stargate. If i am manipulating the list outside of the thread, do i need to use mutex?
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
@PMV
Yes, I have overlooked that, thank you
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.
Yes, I have overlooked that, thank you
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.
"Have you tried turning it off and on again ?"
Re: linked list as parameter to thread
no probs, i got it workingluis wrote:@PMV
Yes, I have overlooked that, thank you![]()
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.


