linked list as parameter to thread

Just starting out? Need help? Post your questions and find answers here.
AndyMK
Enthusiast
Enthusiast
Posts: 582
Joined: Wed Jul 12, 2006 4:38 pm
Location: UK

linked list as parameter to thread

Post by AndyMK »

Is it possible to pass a linked list as a parameter to a thread?
User avatar
Zefz
New User
New User
Posts: 8
Joined: Wed Mar 30, 2011 11:41 pm
Location: Norway

Re: linked list as parameter to thread

Post by Zefz »

Shouldnt something simple like this work?

Code: Select all

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

NewList MyList()
CreateThread( @MyFunction(), @MyList() )
AndyMK
Enthusiast
Enthusiast
Posts: 582
Joined: Wed Jul 12, 2006 4:38 pm
Location: UK

Re: linked list as parameter to thread

Post 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?
User avatar
luis
Addict
Addict
Posts: 3895
Joined: Wed Aug 31, 2005 11:09 pm
Location: Italy

Re: linked list as parameter to thread

Post 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.
"Have you tried turning it off and on again ?"
AndyMK
Enthusiast
Enthusiast
Posts: 582
Joined: Wed Jul 12, 2006 4:38 pm
Location: UK

Re: linked list as parameter to thread

Post by AndyMK »

I am going to go with a global list and mutex, thank you
User avatar
STARGÅTE
Addict
Addict
Posts: 2260
Joined: Thu Jan 10, 2008 1:30 pm
Location: Germany, Glienicke
Contact:

Re: linked list as parameter to thread

Post 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
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 moreTypeface - Sprite-based font include/module
AndyMK
Enthusiast
Enthusiast
Posts: 582
Joined: Wed Jul 12, 2006 4:38 pm
Location: UK

Re: linked list as parameter to thread

Post 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?
User avatar
luis
Addict
Addict
Posts: 3895
Joined: Wed Aug 31, 2005 11:09 pm
Location: Italy

Re: linked list as parameter to thread

Post 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.
"Have you tried turning it off and on again ?"
AndyMK
Enthusiast
Enthusiast
Posts: 582
Joined: Wed Jul 12, 2006 4:38 pm
Location: UK

Re: linked list as parameter to thread

Post 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.
PMV
Enthusiast
Enthusiast
Posts: 727
Joined: Sat Feb 24, 2007 3:15 pm
Location: Germany

Re: linked list as parameter to thread

Post 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
User avatar
luis
Addict
Addict
Posts: 3895
Joined: Wed Aug 31, 2005 11:09 pm
Location: Italy

Re: linked list as parameter to thread

Post 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.
"Have you tried turning it off and on again ?"
AndyMK
Enthusiast
Enthusiast
Posts: 582
Joined: Wed Jul 12, 2006 4:38 pm
Location: UK

Re: linked list as parameter to thread

Post 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
Post Reply