Linked lists and Threads

Just starting out? Need help? Post your questions and find answers here.
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by tranquil.

Howdy ho!

I have a - possible easy - problem of the following thingy:

Accessing one linkedlists from more running threads at the same time couses chaos to the actual selected element. How can I obtain that?

NextElement(), SelectElement(), LastElement(), FirstElement(), RestList() is not different in procedures, it seems that they are "Global" to all. Not realy good.

Mike

Tranquilizer/ Secretly!
http://www.secretly.de
Registred PureBasic User
System: Windows 2000 Server, 512 MB Ram, GeForce4200 TI 128 MB DDR, Hercules Theater 6.1 DTS Sound
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by Pupil.

I think there is no real easy way to takle this, but by coding smart and thinking ahead, using pointers to elements inside procedures and possible using messages to do certain things you could get closer to a working solution..

Best thing is if you have only one "mother" procedure that uses these linked list commands and the "child" procedures is passed pointers to those elements that they should process..
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by tranquil.

Fred, isnt it possible to implement local/ Protected pointers to lists in procedures? Would be help very much or isnt this standard in other languages!? Never used lists and threads in combination before. not realy easy. :)

Cheers
Mike

Tranquilizer/ Secretly!
http://www.secretly.de
Registred PureBasic User
System: Windows 2000 Server, 512 MB Ram, GeForce4200 TI 128 MB DDR, Hercules Theater 6.1 DTS Sound
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by MrVainSCL.

Mike, seems we have to find it out ourself how to solve the problem! ;( Many people have read this topic but seems nobody has an idea...


PIII450, 256MB Ram, 80GB HD + 6,4 GB, RivaTNT, DirectX8.1, SB AWE64, Win2000 + all Updates...

greetz
MrVainSCL! aka Thorsten
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by tinman.
Originally posted by MrVainSCL

Mike, seems we have to find it out ourself how to solve the problem! ;( Many people have read this topic but seems nobody has an idea...
Maybe this helps:

Code: Select all

NewList listy.w()
*foo.w = AddElement(listy())
listy() = 5

If OpenConsole()
	PrintN(Str(@listy()))
	PrintN(Str(*foo))
	PrintN(Str(PeekL(*foo)))
	PrintN(Str(PeekL(*foo+4)))
	PrintN(Str(PeekW(*foo+8)))
	Input()
	CloseConsole()
EndIf
End
I'm guessing that all the other commands return pointers to the list items. So what you could do in your threads is something like this:

get semaphore/mutex for controlling list access
*foo.mytype = list command
release semaphore/mutex
access data items via pointer

Using something for controlling access to the list might not be necessary, but I've added it for safety (for example, if the list commands store the current pointer in the list base and read it back from there before returning).

Of course, you may also have other problems if threads try to delete or modify list elements while another thread is using them. The safest course of action is to enclose all list access inside a semaphore/mutex and make those sections of code as short as possible.



--
It's not minimalist - I'm increasing efficiency by reducing input effort.
(Win98first ed. + SP1, PB3.40)
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by Froggerprogger.

Perhaps you could always use SelectElement() before all List-related-commands.
But, ok, this would not be very comfortable and not really bugfixed and so only a temporary solution.


Purebasic - what a nice name for a girl-friend
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by waffle.

here is the best idea i came up with....

1 - make a global flag that will be used to prevent access to your list

2 - before a thread access the list, the flag must be "cleared", otherwise the thread must go on to do "other things"

3 - when a thread access the list it must "set" the global flag.
whenever done, the thread must "clear" the flag.

Example:
if LIST_LOCKED
;don't do any thing
delay(10)
else
LIST_LOCKED=1
SelectElement(AList(),29);assumes this thread uses element 29
LocalStruct=Alist();copies structure to local var
LIST_LOCKED=0
;keep the list locked as short as possible
;work on data as required
;repeat above procedure to save data....
endif
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by fred.

This is the mutex basis, which is needed when working with threads..

Fred - AlphaSND
Post Reply