Threads and Strings ?

Everything else that doesn't fall into one of the other PB categories.
User avatar
USCode
Addict
Addict
Posts: 912
Joined: Wed Mar 24, 2004 11:04 pm
Location: Seattle, USA

Threads and Strings ?

Post by USCode »

I'm a little confused on using the Thread library. From the documentation, it states:

"Note: Threads need to be used carefully because it is possible that you can have multiple access to shared resources (memory, variables, files, etc) and you need to manually ensure that you do run into trouble because of this. For example, it is not safe to modify or write to strings from more than one thread because strings share the same internal memory buffer. If you only ever read from strings while your threads are running then it should be safe."

:?: Does this mean I need to be careful when assigning a value to a ANY string in the thread or only those strings that can be modified from both the main application and the thread?

IOW, can I safely update strings that are UNIQUE to the thread procedure without worrying about synchronisation of string updates? I only need to worry about strings that are shared by the main app and the threaded procedure.
OR can I only be updating ANY strings in the main app OR the threaded procedure at one time?

Thanks!
Num3
PureBasic Expert
PureBasic Expert
Posts: 2810
Joined: Fri Apr 25, 2003 4:51 pm
Location: Portugal, Lisbon
Contact:

Re: Threads and Strings ?

Post by Num3 »

USCode wrote: :?: Does this mean I need to be careful when assigning a value to a ANY string in the thread or only those strings that can be modified from both the main application and the thread?
By string that can be modified by both!
I only need to worry about strings that are shared by the main app and the threaded procedure.
Exactly!
dell_jockey
Enthusiast
Enthusiast
Posts: 767
Joined: Sat Jan 24, 2004 6:56 pm

Post by dell_jockey »

Come to think of it, has anyone tried to implement semaphores for individual strings that need to be manipulated by multiple threads?
cheers,
dell_jockey
________
http://blog.forex-trading-ideas.com
KarLKoX
Enthusiast
Enthusiast
Posts: 677
Joined: Mon Oct 06, 2003 7:13 pm
Location: France
Contact:

Post by KarLKoX »

There is is my answer in the french forum : http://purebasic.hmt-forum.com/viewtopic.php?t=2917

You can alternatively use CriticalSection. (Enter/Leave)
"Qui baise trop bouffe un poil." P. Desproges

http://karlkox.blogspot.com/
User avatar
USCode
Addict
Addict
Posts: 912
Joined: Wed Mar 24, 2004 11:04 pm
Location: Seattle, USA

Post by USCode »

Thanks Num3!

Based on your answer (and experience I assume) - I find what is in the PureBasic Thread documentation to be somewhat confusing. To me it lead me to believe I couldn't write to ANY strings from the threaded procedure without semaphores, etc. Whereas actually as long as I don't write to any strings declared in my main app (and vice versa from the threaded procedure) then I should be AOK ....

I'll try to come up with some alternative wording for the documentation and submit it to Fred...then again, maybe it's just me! :wink: Thanks again.
dell_jockey
Enthusiast
Enthusiast
Posts: 767
Joined: Sat Jan 24, 2004 6:56 pm

Post by dell_jockey »

Merci bien, KarLKoX
cheers,
dell_jockey
________
http://blog.forex-trading-ideas.com
localmotion34
Enthusiast
Enthusiast
Posts: 665
Joined: Fri Sep 12, 2003 10:40 pm
Location: Tallahassee, Florida

Post by localmotion34 »

so my understanding of pointers, arrays, and linked lists is that you are able to return their address with an "@" symbol. that is, you can manipulate or change any of those, get the address, and return it. so why cant you declare a pointer or array and do this:

*ptr.whatever=blah

procedure something(*anotherptr)
*anotherptr=doseomthing
endprocedure

procedure getThread return()
returnVariable=@anotherptr
procedurereturn returnvariable
endprocedure

createthread(@something(),*moreblah)

debug getthreadreturn()

shouldnt it be able to read that address and then return the variable?

Code: Select all

!.WHILE status != dwPassedOut
! Invoke AllocateDrink, dwBeerAmount
!MOV Mug, Beer
!Invoke Drink, Mug, dwBeerAmount
!.endw
User avatar
Derlidio
User
User
Posts: 77
Joined: Fri Feb 27, 2004 9:19 pm
Location: SP - Brazil

Post by Derlidio »

I've run into the same problem, and as far as I could notice, it is not safe to use PB strings (no matter if shared or not) in threads. This may not cause problems in all computers. When the user is running on single processor machines, you may never see the problem. But when they run in multi-processor machines, it is very likely that they'll experience often Access Violation in NTDLL (wich is responsible for allocating and releasing memory in Windows OS).

I've posted a sample code here:

viewtopic.php?t=15472

If PB string memory doesn't go Thread Safe, then none of the String commands will be safe to use on threads. May be I'm wrong, but I think that PB Heap is not using "serialization", wich warrants mutual exclusion when more than one thread tries to allocate/release resources from the Heap. To turn this feature on, it is just a matter of PB choosing the right flags when creating the Heap. This could even turn into a "compilation option" or something, in my point of view.

best wishes...

-PJoe
Derlidio Siqueira
El_Choni
TailBite Expert
TailBite Expert
Posts: 1007
Joined: Fri Apr 25, 2003 6:09 pm
Location: Spain

Post by El_Choni »

The problem, I think, is not only the flags you use when allocating and releasing memory for strings.

The problem is also that PB string handling routines use a global variable to point to the current string in the "string heap". This heap is also global for all PB threads.

The variable can be changed at the same time by two different threads, which does cause problems and make PB strings thread unsafe.

But using critical sections or that semaphore thing can't be that difficult. Or maybe it is, if your app is already very complex, I don't know. Maybe Fred can tell us if he has any plan on thread safety for the future...
El_Choni
User avatar
Derlidio
User
User
Posts: 77
Joined: Fri Feb 27, 2004 9:19 pm
Location: SP - Brazil

Post by Derlidio »

Yippe...

I don't know if PB works with different Heaps, one for Strings and other(s) for any other type of data. The problem I'm experiencing with unsafe strings may not be related only to strings. If PB Heap Allocation routines are not secured with any kind of serialization, then we may experience such problems with any kind of memory allocation involving threads.

See, I'm not telling that it should be safe to work with the same variable in 2 threads. Of course, when we must share resources among threads, it is up to us to provide ways to make the shared resources safe. What I'm telling is that it should be safe for 2 threads to allocate memory of their own without have to worry about such conflicts.

Best wishes...

- PJoe
Derlidio Siqueira
User avatar
GedB
Addict
Addict
Posts: 1312
Joined: Fri May 16, 2003 3:47 pm
Location: England
Contact:

Post by GedB »

The approach I'm taking at the moment is to only use local, stack based variables in threads. I then communicate back to the main process by sending windows messages and picking them up in the event loop.
localmotion34
Enthusiast
Enthusiast
Posts: 665
Joined: Fri Sep 12, 2003 10:40 pm
Location: Tallahassee, Florida

Post by localmotion34 »

yes, the postmessage_() function is a great way to communicate from inside a thread. you can use a local variable inside the thread procedure, post it inside the lparam or wparam of the message
(even use a full structure or linked list and pass it) and get it back with peekmessage_() and get the lparam or wparam back. it a great way to sort a huge linkedlist or do image manipulation, and then peek at the results and continue on.

Code: Select all

!.WHILE status != dwPassedOut
! Invoke AllocateDrink, dwBeerAmount
!MOV Mug, Beer
!Invoke Drink, Mug, dwBeerAmount
!.endw
Dare2
Moderator
Moderator
Posts: 3321
Joined: Sat Dec 27, 2003 3:55 am
Location: Great Southern Land

Post by Dare2 »

Could one of you thread gurus write a brief "using threads with purebasic (for dummies) tutorial or guide?

This dummy would really appreciate it. I know nix about threads and less about the ramifications of their use in purebasic. And I am sure I am not the only one who could benefit.
@}--`--,-- A rose by any other name ..
Intrigued
Enthusiast
Enthusiast
Posts: 501
Joined: Thu Jun 02, 2005 3:55 am
Location: U.S.A.

Post by Intrigued »

Dare2 wrote:Could one of you thread gurus write a brief "using threads with purebasic (for dummies) tutorial or guide?

This dummy would really appreciate it. I know nix about threads and less about the ramifications of their use in purebasic. And I am sure I am not the only one who could benefit.
I second that! Otherwise for now I will continue to use my left foot to step on my right food. ;-)

FWIW, It's only been a few days and I am very appreciative of the time of many of you members to post samples to get us younglings up and going. For example, based on some various code chunks here (and some Help file checking) I was able to create my first (though simple) .dll for other applications I create, on another platform.
Intrigued - Registered PureBasic, lifetime updates user
dell_jockey
Enthusiast
Enthusiast
Posts: 767
Joined: Sat Jan 24, 2004 6:56 pm

Post by dell_jockey »

This dummy would be a taker too!
cheers,
dell_jockey
________
http://blog.forex-trading-ideas.com
Post Reply