linked list as parameter to thread
Posted: Mon Sep 19, 2011 2:59 pm
Is it possible to pass a linked list as a parameter to a thread?
http://www.purebasic.com
https://www.purebasic.fr/english/
Code: Select all
Procedure MyFunction(List Parameter)
ProcedureReturn ListSize( Parameter() )
EndProcedure
NewList MyList()
CreateThread( @MyFunction(), @MyList() )
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())
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
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
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?
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.
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?
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.