Recent

Author Topic: How to sharing 10 resources of same types between 20 threads ?  (Read 7002 times)

mohsenti

  • Jr. Member
  • **
  • Posts: 58
How to sharing 10 resources of same types between 20 threads ?
« on: November 14, 2015, 04:53:52 pm »
Hi all;
I have 10 resources of same type and I have share they with 20 threads and manage with critical sections.
how to implement that ?

Leledumbo

  • Hero Member
  • *****
  • Posts: 8836
  • Programming + Glam Metal + Tae Kwon Do = Me
Re: How to sharing 10 resources of same types between 20 threads ?
« Reply #1 on: November 14, 2015, 05:42:28 pm »
Just as if you share 1 resource with 2 threads and manage the access with a critical section. No difference for more resources or threads.

mohsenti

  • Jr. Member
  • **
  • Posts: 58
Re: How to sharing 10 resources of same types between 20 threads ?
« Reply #2 on: November 15, 2015, 01:43:58 pm »
I know that but maybe one resource release faster than other and threads run base on user request.
I need routine to assign resource to thread when thread request resource and release resource when thread release that.

Leledumbo

  • Hero Member
  • *****
  • Posts: 8836
  • Programming + Glam Metal + Tae Kwon Do = Me
Re: How to sharing 10 resources of same types between 20 threads ?
« Reply #3 on: November 15, 2015, 01:55:13 pm »
I know that but maybe one resource release faster than other and threads run base on user request.
I need routine to assign resource to thread when thread request resource and release resource when thread release that.
Don't use single critical section for all resources, use different one for each.

taazz

  • Hero Member
  • *****
  • Posts: 5368
Re: How to sharing 10 resources of same types between 20 threads ?
« Reply #4 on: November 15, 2015, 02:16:37 pm »
I know that but maybe one resource release faster than other and threads run base on user request.
I need routine to assign resource to thread when thread request resource and release resource when thread release that.
What kind of resources are we talking about? Are those resources asked by some kind of ID or they are shared on a availability bases? In short if the resources are unique then there are a number of consideration to be cleared. For example do they need to be accessed on a specific order or any order will be just fine? For now I'll assume that 1. the resources are unique so each thread can decide for it self if it needs access to any resource and 2) the order is predetermined and needs to be followed. In that case you protect each resource with its own section. eg
Code: Pascal  [Select][+][-]
  1. type
  2.  
  3.   { TThreadedResource }
  4.  
  5.   TThreadedResource = class
  6.   private
  7.     FResource :TObject;
  8.     FLock     :TCriticalSection;
  9.     function GetResource :TObject;
  10.   public
  11.     function Acquire(Wait:Boolean = True):Boolean;
  12.     procedure Release;
  13.     constructor Create(aResource:TObject);
  14.     destructor Destroy; override;
  15.     property Resource:TObject read GetResource; //make sure that the others are not allowed to access it.
  16.   end;
  17.  
  18. implementation
  19.  
  20. {$REGION ' TThreadedResource '}
  21.  
  22. function TThreadedResource.GetResource :TObject;
  23. begin
  24.   if Assigned(FLock) then begin
  25.     if FLock.TryEnter then Result := FResource else Result := Nil;
  26.   end;
  27. end;
  28.  
  29. function TThreadedResource.Acquire(Wait :Boolean) :Boolean;
  30. begin
  31.   Result := False;
  32.   if Assigned(FLock) then begin
  33.     if Wait then begin
  34.       FLock.Enter;
  35.       Result := True;
  36.     end else
  37.       Result := FLock.TryEnter;
  38.   end;
  39. end;
  40.  
  41. procedure TThreadedResource.Release;
  42. begin
  43.   if Assigned(FLock) then FLock.Leave;
  44. end;
  45.  
  46. constructor TThreadedResource.Create(aResource:TObject);
  47. begin
  48.   inherited Create;
  49.   FLock := TCriticalSection.Create;
  50.   FResource := aResource
  51. end;
  52.  
  53. destructor TThreadedResource.Destroy;
  54. var
  55.   vTmp : TCriticalSection;
  56. begin
  57.   FLock.Enter;
  58.   try
  59.     vTmp := FLock;
  60.     FLock := Nil;
  61.   finally
  62.     FLock.Free;
  63.   end;
  64.   inherited Destroy;
  65. end;
  66.  
  67. {$ENDREGION}
  68.  
Now a thread can acquire the resource and until it finishes with it all others will either wait or move on to other tasks and come back to it later. If the resources are of the same type and you do not care which one is accessed by which thread then a simple pool implementation would be better suited for your task. For a more specific answer we need more info.
Good judgement is the result of experience … Experience is the result of bad judgement.

OS : Windows 7 64 bit
Laz: Lazarus 1.4.4 FPC 2.6.4 i386-win32-win32/win64

mohsenti

  • Jr. Member
  • **
  • Posts: 58
Re: How to sharing 10 resources of same types between 20 threads ?
« Reply #5 on: November 15, 2015, 02:37:38 pm »
thank you for answers.

my resources are of same type and I do not care which one is accessed by which thread.
I create a list of resources and assign one lock to list when a thread request resource Request method enter to critical section and search list for a free resource if that exists assign it to thread if all resources are in use enter to list lock critical section and when a resource release try enter to list lock and release lock but this routine cause exceptions in critical sections and i don't know why ?

another bug of that routine is if I have two resources lists; lock a list cause lock all requests to another lists.
« Last Edit: November 15, 2015, 02:54:34 pm by mohsenti »

taazz

  • Hero Member
  • *****
  • Posts: 5368
Re: How to sharing 10 resources of same types between 20 threads ?
« Reply #6 on: November 15, 2015, 03:07:56 pm »
my resources are of same type and I do not care which one is accessed by which thread.
Ok we have a limited number of resources that do the same thing eg 10 pencils and you need to make sure that every thread that requests one either gets one or waits until one becomes available. In that case you need to use a counting semaphore protecting a TQueue.

I create a list of resources and assign one lock to list when a thread request resource Request method enter to critical section and search list for a free resource if that exists assign it to thread if all resources are in use enter to list lock critical section and when a resource release try enter to list lock and release lock but this routine cause exceptions in critical sections and i don't know why ?

another bug of that routine is if I have two resources lists; lock a list cause lock all requests to another lists.
Not knowing how you protect things or with out any code to glare at, there is no chance to even begin to understand where the problem is.

EDIT:
Ok it seems that fpc does not have any semaphore implementation so you need to write your own or use the system objects, if you can't figure things post a small example of your current implementation, along with information on the target OS and application bitness and I'll try to enhance it for you.
« Last Edit: November 15, 2015, 04:07:36 pm by taazz »
Good judgement is the result of experience … Experience is the result of bad judgement.

OS : Windows 7 64 bit
Laz: Lazarus 1.4.4 FPC 2.6.4 i386-win32-win32/win64

mohsenti

  • Jr. Member
  • **
  • Posts: 58
Re: How to sharing 10 resources of same types between 20 threads ?
« Reply #7 on: November 16, 2015, 07:56:13 am »
Hi

I attach a simple demo to post.


taazz

  • Hero Member
  • *****
  • Posts: 5368
Re: How to sharing 10 resources of same types between 20 threads ?
« Reply #8 on: November 16, 2015, 04:06:01 pm »
I got the sample I'll take a closer look later today.

Edit:
Are you trying to manage a number of Resources or a number of resource queues? Your sample is a bit confusing on that. I'm going to work on the assumption that you want to manage a number of resources and I'll remove the list of queues as a miscalculation.

« Last Edit: November 16, 2015, 05:44:01 pm by taazz »
Good judgement is the result of experience … Experience is the result of bad judgement.

OS : Windows 7 64 bit
Laz: Lazarus 1.4.4 FPC 2.6.4 i386-win32-win32/win64

taazz

  • Hero Member
  • *****
  • Posts: 5368
Re: How to sharing 10 resources of same types between 20 threads ?
« Reply #9 on: November 17, 2015, 02:43:50 am »
Find attached an enhanced version of your code. It is lightly tested and requires a lot more testing before release but it gives you a working sample to start you off. Most of the changes are in the unit ResourceManager.pas specifically I change the TResourceQueue to work as a pool of resources, enhanced the methods to support classes instead of class names extended the resourcemanager to support both classes and class names, I added a light semaphore wrapper around the system semaphores there is code to support windows and linux the semaphore is lightly tested on windows I would appreciate any feedback or test cases you might think (code is not required) so I can add them to my test suit.


Good judgement is the result of experience … Experience is the result of bad judgement.

OS : Windows 7 64 bit
Laz: Lazarus 1.4.4 FPC 2.6.4 i386-win32-win32/win64

mohsenti

  • Jr. Member
  • **
  • Posts: 58
Re: How to sharing 10 resources of same types between 20 threads ?
« Reply #10 on: November 17, 2015, 10:02:42 am »
thank you,taazz

I look sample; in your sample you create a queue with max count 10 to allow thread request maximum 10 resources of ResourceClass Type and don't add any object of resource.
I try to run 20 threads in your sample but it trapped.
I think in your sample count of resources are more than threads!

taazz

  • Hero Member
  • *****
  • Posts: 5368
Re: How to sharing 10 resources of same types between 20 threads ?
« Reply #11 on: November 17, 2015, 02:44:49 pm »
thank you,taazz

I look sample; in your sample you create a queue with max count 10 to allow thread request maximum 10 resources of ResourceClass Type and don't add any object of resource.
I try to run 20 threads in your sample but it trapped.
I think in your sample count of resources are more than threads!
your sample application is flawed. You call synchronize when you acquire the resource which makes all the code written so far mute and then the logresource.log method is accessing lcl controls from what ever thread has control over it. You can't do that. You have to makes sure that the log method is thread safe too you either synchronize the access to memo1 or use some other form of communication that is thread safe (postmessage comes to mind).
Good judgement is the result of experience … Experience is the result of bad judgement.

OS : Windows 7 64 bit
Laz: Lazarus 1.4.4 FPC 2.6.4 i386-win32-win32/win64

mohsenti

  • Jr. Member
  • **
  • Posts: 58
Re: How to sharing 10 resources of same types between 20 threads ?
« Reply #12 on: November 17, 2015, 04:44:45 pm »
all of resources is thread safe and LogResource is just a sample;
excuse me for that.
my resources are streams and some of streams are read-only and some of others are writable and others are online streams and ...
I need list of list of streams and when a thread request writable stream Request method must return a writable stream or lock thread to a stream release and don't care about which one of writable stream return by Request method this routine repeat for all streams list.
 I should  lock list of resource because I should lock threads when all resources of requested resource type are in use.

taazz

  • Hero Member
  • *****
  • Posts: 5368
Re: How to sharing 10 resources of same types between 20 threads ?
« Reply #13 on: November 17, 2015, 05:11:00 pm »
all of resources is thread safe and LogResource is just a sample;
excuse me for that.
Well I can't test what I can't see or understand.

I need list of list of streams and when a thread request writable stream Request method must return a writable stream or lock thread to a stream release and don't care about which one of writable stream return by Request method this routine repeat for all streams list.


if you say so.

I should  lock list of resource because I should lock threads when all resources of requested resource type are in use.
No, you should never lock the list outside the absolutely necessary time needed to access its data, semaphores will lock all threads trying to get a depleted resource until one becomes available.
Good judgement is the result of experience … Experience is the result of bad judgement.

OS : Windows 7 64 bit
Laz: Lazarus 1.4.4 FPC 2.6.4 i386-win32-win32/win64

 

TinyPortal © 2005-2018