Threads - Same resource

BlitzMax Forums/BlitzMax Beginners Area/Threads - Same resource

Retimer(Posted 2009) [#1]
If two threads are accessing a resource at the same moment - what exactly happens to the resource?

I've tried messing with ways to totally screw up threading to better understand how they work and the consequences of improper usage of them, since i'de like to understand them better.

It's not a for sure thing that everyone would get the same results. Here is the source:



All the threads are adding a string to the list, and the listcount is being drawn into the window so you can see as it goes up. After a random interval (in my case anyways), it just stops increasing - no errors, no strange "boo" to the program itself...it just continues to run as if nothing happened.

If the list is recreated (mouse click), then the list is being added to again until the issue occurs again.

What exactly is occuring to the resource for it to just stop like that?


Zeke(Posted 2009) [#2]
i think that two threads are calling ListAdder() function sametime.. and then list(Tlist) internal links goes to invalid..

but adding:
Global mutex:TMutex=CreateMutex()


and

Function ListAdder()
	Delay 1
	LockMutex(mutex)
	list.AddLast("123456789")
	UnlockMutex(mutex)
End Function


there is no freezing...


Retimer(Posted 2009) [#3]
Thanks zeke. I knew it could be prevented, but I was curious what exactly was occuring to the resource for it to break like that.

You mention the internal links go to invalid - could you explain that at all? or maybe refer me to an article explaining that?

Thanks


Zeke(Posted 2009) [#4]
http://en.wikibooks.org/wiki/BlitzMax/Modules/Brl/LinkedList#TList

i dont know how to explain this..
but i think that when two threads are adding something to Tlist, then one of these (tlist _head:tlink, tlink _succ:tlink, _pred) is pointing to other thread info.. and it just breaks that internal "tree" (node1<->node2<->node3<->node4<->....)

and just look /mod/brl.mod/linkedlist.mod/linkedlist.bmx how this "Tlist tree" works.


Dreamora(Posted 2009) [#5]
you will get an access violation if you try to access the same memory at the same time from 2+ threads / processes.
This has been very well shown in the days when BM didn't have threading when people added "thread modules" and tested them indepth on single core systems to claim that they work, just to find out that they bombed the whole app within 30 seconds on a multicore machine :)


Retimer(Posted 2009) [#6]
Ok, I think i'm understanding this now :)

Thanks


Retimer(Posted 2009) [#7]
Yeah, I encountered deadlocking when looking up semaphores and conditional vars on google, so that's making a good amount of sense now to me.


I have another somewhat on-topic question:


LockMutex - prevents, or pauses activity of all threads to safely alter a resource, or am I wrong?

I want to lock threads from accessing a single resource (a single object) rather then all resources to ensure optimization in a live server - otherwise, there's really not much optimization going on.

I take it that this is not exactly possible with locking mutex'?

My guess is to use an array of conditions to:

1. check if a resource is being used by checking it's array condition value
2. wait until it is done being used
3. setting the condition to 'being used'
4. Alter object value(s)
5. setting conition to 'not being used'

Or is there a better method to do this?


Dreamora(Posted 2009) [#8]
LockMutex will only lock code that attempts to lock the same mutex (and thus would access the same data)

if they are different mutexes it won't lock but mutex have some performance overhead.



Your approach would not work because the array itself would be disallowed as no 2 threads can access the array object at all at the same time without using mutex to protect it


Retimer(Posted 2009) [#9]
Ahh, ok, that's not so bad then. Having a seperate mutex for each object (in my case, socketstream) would generally be the right idea I suppose to prevent inappropriate locking.

Your approach would not work because the array itself would be disallowed as no 2 threads can access the array object at all at the same time without using mutex to protect it


Just saw a post with an example of condvars, and they are completely off from what I assumed (that I could set the variable to be used for the condition).

Thanks for all the help! :)

I think i'm good to go now.