It is currently Tue Dec 01, 2020 9:11 am

All times are UTC + 1 hour




Post new topic Reply to topic  [ 21 posts ]  Go to page 1, 2  Next
Author Message
 Post subject: Threads and Strings ?
PostPosted: Fri Jun 03, 2005 8:21 pm 
Offline
Addict
Addict
User avatar

Joined: Wed Mar 24, 2004 11:04 pm
Posts: 902
Location: Seattle, USA
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!


Top
 Profile  
Reply with quote  
 Post subject: Re: Threads and Strings ?
PostPosted: Fri Jun 03, 2005 8:47 pm 
Offline
PureBasic Expert
PureBasic Expert
User avatar

Joined: Fri Apr 25, 2003 4:51 pm
Posts: 2809
Location: Portugal, Lisbon
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!

Quote:
I only need to worry about strings that are shared by the main app and the threaded procedure.


Exactly!

_________________
Elevator Maintenance: http://central-elevadores.pt
Good web hosting![/size]


Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: Fri Jun 03, 2005 9:36 pm 
Offline
Enthusiast
Enthusiast

Joined: Sat Jan 24, 2004 6:56 pm
Posts: 768
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


Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: Fri Jun 03, 2005 11:34 pm 
Offline
Enthusiast
Enthusiast
User avatar

Joined: Mon Oct 06, 2003 7:13 pm
Posts: 672
Location: France
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/


Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: Sat Jun 04, 2005 12:23 am 
Offline
Addict
Addict
User avatar

Joined: Wed Mar 24, 2004 11:04 pm
Posts: 902
Location: Seattle, USA
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.


Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: Sat Jun 04, 2005 12:29 am 
Offline
Enthusiast
Enthusiast

Joined: Sat Jan 24, 2004 6:56 pm
Posts: 768
Merci bien, KarLKoX

_________________
cheers,
dell_jockey
________
http://blog.forex-trading-ideas.com


Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: Sat Jun 04, 2005 4:34 am 
Offline
Enthusiast
Enthusiast

Joined: Fri Sep 12, 2003 10:40 pm
Posts: 665
Location: Tallahassee, Florida
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:
!.WHILE status != dwPassedOut
! Invoke AllocateDrink, dwBeerAmount
!MOV Mug, Beer
!Invoke Drink, Mug, dwBeerAmount
!.endw


Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: Tue Jun 07, 2005 12:17 am 
Offline
User
User
User avatar

Joined: Fri Feb 27, 2004 9:19 pm
Posts: 77
Location: SP - Brazil
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:

http://forums.purebasic.com/english/vie ... hp?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


Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: Tue Jun 07, 2005 12:57 am 
Offline
TailBite Expert
TailBite Expert

Joined: Fri Apr 25, 2003 6:09 pm
Posts: 1007
Location: Spain
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


Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: Tue Jun 07, 2005 2:32 am 
Offline
User
User
User avatar

Joined: Fri Feb 27, 2004 9:19 pm
Posts: 77
Location: SP - Brazil
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


Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: Tue Jun 07, 2005 10:06 am 
Offline
Addict
Addict
User avatar

Joined: Fri May 16, 2003 3:47 pm
Posts: 1311
Location: England
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.


Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: Tue Jun 07, 2005 2:23 pm 
Offline
Enthusiast
Enthusiast

Joined: Fri Sep 12, 2003 10:40 pm
Posts: 665
Location: Tallahassee, Florida
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:
!.WHILE status != dwPassedOut
! Invoke AllocateDrink, dwBeerAmount
!MOV Mug, Beer
!Invoke Drink, Mug, dwBeerAmount
!.endw


Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: Tue Jun 07, 2005 2:31 pm 
Offline
Moderator
Moderator

Joined: Sat Dec 27, 2003 3:55 am
Posts: 3321
Location: Great Southern Land
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 ..


Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: Tue Jun 07, 2005 3:59 pm 
Offline
Enthusiast
Enthusiast

Joined: Thu Jun 02, 2005 3:55 am
Posts: 501
Location: U.S.A.
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


Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: Tue Jun 07, 2005 9:55 pm 
Offline
Enthusiast
Enthusiast

Joined: Sat Jan 24, 2004 6:56 pm
Posts: 768
This dummy would be a taker too!

_________________
cheers,
dell_jockey
________
http://blog.forex-trading-ideas.com


Top
 Profile  
Reply with quote  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 21 posts ]  Go to page 1, 2  Next

All times are UTC + 1 hour


Who is online

Users browsing this forum: No registered users and 16 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
Jump to:  

 


Powered by phpBB © 2008 phpBB Group
subSilver+ theme by Canver Software, sponsor Sanal Modifiye