Page 1 of 2

Threads and Strings ?

Posted: Fri Jun 03, 2005 8:21 pm
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!

Re: Threads and Strings ?

Posted: Fri Jun 03, 2005 8:47 pm
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!

Posted: Fri Jun 03, 2005 9:36 pm
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?

Posted: Fri Jun 03, 2005 11:34 pm
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)

Posted: Sat Jun 04, 2005 12:23 am
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.

Posted: Sat Jun 04, 2005 12:29 am
by dell_jockey
Merci bien, KarLKoX

Posted: Sat Jun 04, 2005 4:34 am
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?

Posted: Tue Jun 07, 2005 12:17 am
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

Posted: Tue Jun 07, 2005 12:57 am
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...

Posted: Tue Jun 07, 2005 2:32 am
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

Posted: Tue Jun 07, 2005 10:06 am
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.

Posted: Tue Jun 07, 2005 2:23 pm
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.

Posted: Tue Jun 07, 2005 2:31 pm
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.

Posted: Tue Jun 07, 2005 3:59 pm
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.

Posted: Tue Jun 07, 2005 9:55 pm
by dell_jockey
This dummy would be a taker too!