Threads and Strings ?
Threads and Strings ?
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!
"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!
-
- PureBasic Expert
- Posts: 2810
- Joined: Fri Apr 25, 2003 4:51 pm
- Location: Portugal, Lisbon
- Contact:
Re: Threads and Strings ?
By string that can be modified by both!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?
Exactly!I only need to worry about strings that are shared by the main app and the threaded procedure.
-
- Enthusiast
- Posts: 767
- Joined: Sat Jan 24, 2004 6:56 pm
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)
You can alternatively use CriticalSection. (Enter/Leave)
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! Thanks again.
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! Thanks again.
-
- Enthusiast
- Posts: 767
- Joined: Sat Jan 24, 2004 6:56 pm
-
- Enthusiast
- Posts: 665
- Joined: Fri Sep 12, 2003 10:40 pm
- 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?
*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
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
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
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...
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
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
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
-
- Enthusiast
- Posts: 665
- Joined: Fri Sep 12, 2003 10:40 pm
- 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.
(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
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.
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 ..
I second that! Otherwise for now I will continue to use my left foot to step on my right food.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.
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
-
- Enthusiast
- Posts: 767
- Joined: Sat Jan 24, 2004 6:56 pm