Click here to Skip to main content
15,867,686 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a component that keeps a cache on a per-thread basis, but in some applications i need to share the cache across multiple threads.

I don't mind hosting the component on a thread and then marshalling calls over to/onto that thread in order to share the cache between other threads - in fact that's my plan. So it's hosted on thread A, and accessed by B,C,(and A) by marshalling all calls from the other threads down to A.

but i need to know the easiest way to make that happen.

I'm very new to synchronization contexts in .NET.

Usually what i do to accomplish sinking and posting calls across threads is ISynchronizeInvoke.Invoke/BeginInvoke

The trouble is, i don't want to wrap this component's classes. There are 30 different entities each with a myriad of members.

I know .NET can wrap this for me. I know it can "marshal" - even if I *have to* give it an ISynchronizeInvoke or equivelant mechanism somewhere. I will keep one if i have to.

But how do i get .NET to wrap calls for me and marshall them across threads without having to manually create a wrapper?

What I have tried:

I've considered .NET remoting but that's really heavy handed and not really what i want.

I want something inproc and relatively light. I don't even know where to begin to look for this. My google searches aren't giving me what i want.
Posted
Updated 4-Sep-19 22:40pm
v2

 
Share this answer
 
Comments
honey the codewitch 2-Sep-19 23:38pm    
things like concurrentdictionary are not what i want. I'm not looking to use synchronized dictionaries. I'm looking to martial calls and sink them on a particular thread, hopefully with .NET doing the marshalling for me like it does with ISychronizeInvoke

You can use lock to restrict the updating of a variable to one thread at a time. Other threads calling the method will be blocked until the lock is released.

C#
private readonly object totalLock = new object();
private double total;
public void AdjustTotal(double amount)
{
    lock (totalLock)
    {
        total += amount;
    }

}

As an alternative, you can use the SemaphoreSlim class to provide the same functionality without blocking any threads in the process
C#
private readonly SemaphoreSlim semaphoreSlim = new SemaphoreSlim(1);
private double total;
public async Task AdjustTotalAsync(double amount)
{
    await semaphoreSlim.WaitAsync();
    try
    {
        total += amount;
    }
    finally
    {
        //must release the lock
        semaphoreSlim.Release();
    }
}

 
Share this answer
 
Comments
honey the codewitch 5-Sep-19 4:42am    
Unfortunately I can't lock the source classes because they aren't mine.

In order to do so I'd have to make wrappers for every single item.

Basically I want .NET to do it for me, except .NET uses a different technique to accomplish the same (it uses a form of message pumping/signalling to synchronize calls)
George Swan 6-Sep-19 2:04am    
Are you able to publish some pseudo code detailing what you are trying to achieve, please?
honey the codewitch 6-Sep-19 2:07am    
Not so easily, it's a lot of code. The whole point of this exercise is to avoid having to write it.

If you don't get what I'm asking, that's totally cool. I found another solution anyway that sidesteps this whole issue.
George Swan 6-Sep-19 2:38am    
I'm pleased that you have solved your problem. Best wishes.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900