The Author Online Book Forums are Moving

The Author Online Book Forums will soon redirect to Manning's liveBook and liveVideo. All book forum content will migrate to liveBook's discussion forum and all video forum content will migrate to liveVideo. Log in to liveBook or liveVideo with your Manning credentials to join the discussion!

Thank you for your engagement in the AoF over the years! We look forward to offering you a more enhanced forum experience.

556315 (4) [Avatar] Offline
#1
Riccardo,

This is an excellent book that is packed w great stuff.

On second reading i believe that the Swap method of the Atom<T> class, should return value as opposed to returning original.

Returning value will make the return consistent w Listing 3.4:

if (onlineUsers.Swap(d => {
if (d.ContainsKey(connectionId)) return d;
return d.Add(connectionId, user.Identity.Name);
}) != temp)
{
RegisterUserConnection (connectionId, user.Identity.Name);
}

Thank you for your attention n for your great work.
Riccardo Terrell (31) [Avatar] Offline
#2
Thank you very much. I am glad you are enjoying the book.

Good catch. Listing 3.4 is sort of conceptual code from a real World scenario I run into it. In this case was not tested it.
However, I would rather update and fix the code listing 3.4 than the implementation of the Atom.
I feel that for consistency with the "Interlocked.CompareExchange" the Atom.Swap should return the original value.
What do you think ?
556315 (4) [Avatar] Offline
#3
Ricardo,

You are my Guru. If you think to change the client of the Atom<T> class then I concur.

Thank you for your work.

Avi Farah
556315 (4) [Avatar] Offline
#4
Riccardo,

On a second thought, if the Swap method of the Atom<T> class, returned original, then how will you modify Listing 3.4 w/out risking a race condition n w/out locking?

Avi
556315 (4) [Avatar] Offline
#5
Riccardo,

Humble apologies, no race condition, onlineUsers is an immutable dictionary.

After the Swap one will need to check onlineUsers.Value against temp.

Again many thx
Avi Farah
Riccardo Terrell (31) [Avatar] Offline
#6
I am glad you figured it out.
These days I traveling by car and I can answer when I stop at the hotel.

Your feedback was very much appreciated, thank you!
Winson (32) [Avatar] Offline
#7
I wrote following code to test the swap function (inside static Main method):

Atom<ImmutableDictionary<Guid, string>> onlineUsers =
new Atom<ImmutableDictionary<Guid, string>>(ImmutableDictionary<Guid, string>.Empty);

Guid connectionId = Guid.NewGuid();

var temp = onlineUsers.Value;

if (onlineUsers.Swap(d =>
{
if (d.ContainsKey(connectionId)) return d;
return d.Add(connectionId, "Winson");
}) != temp)
{
Console.WriteLine("Register User");
}

var temp2 = onlineUsers.Value;
Guid connectionId2 = Guid.NewGuid();
if (onlineUsers.Swap(d =>
{
if (d.ContainsKey(connectionId2)) return d;
return d.Add(connectionId2, "Dick");
}) != temp2)
{
Console.WriteLine("Register User");
}

Console.ReadLine();

If the swap function return "value", above code will print "Register User" message twice which is the expected result.