Is It Safe to Use -1 to Set All Bits to True

volatile in java with long, int, boolean, and many different cases of write

a. your answer is fine (i.e. volatile is not enough)

b. "it is NEVER an atomic operation because long takes two write cycles for the upper and lower 32 bit writes and reads" => that is too strong for non volatile variables: non volatile long assignment may or may not be atomic. It is generally atomic on 64-bit processors but the Java Memory Model does not give any guarantees. However volatile long assignment is guaranteed to be atomic.

c. your answer is fine (although I don't understand what you mean by "before an interrupt occurs")

d. your answer is fine

How does C handle non-booleans in an if statement?

your guess is right:

from §6.8.4.1 of WG14/N1256

the first substatement is executed if the expression compares unequal to 0

Why I getting an Expression always true warning?

If your code enters the first else block it means that (varKnownFolder == null) was evaluated to false.

So the second check is useless as varKnownFolder could never be null in this block.

public async Task<Metadata> GetFolderAsync(string strDirectoryPathName, dynamic varKnownFolder = null)
{
using (await _FolderPathToInfoMapSync.EnterAsync().ConfigureAwait(false))
{
Metadata objFolder;
string strPathName = strDirectoryPathName;

if (varKnownFolder == null)
{
// This would happen if varKnownFolder is null

objFolder = await _Storage.Client.Files.GetMetadataAsync(strPathName);
}
else
{
// The code enters HERE BECAUSE varKnownFolder is not null

if (varKnownFolder != null) // <-- So this check is useless
_FolderPathToInfoMap.Add(strDirectoryPathName, varKnownFolder);
else
objFolder = null;
}

return objFolder;
}

}

Also, it means that you could replace it with this :

public async Task<Metadata> GetFolderAsync(string strDirectoryPathName, dynamic varKnownFolder = null)
{
using (await _FolderPathToInfoMapSync.EnterAsync().ConfigureAwait(false))
{
Metadata objFolder = null;
string strPathName = strDirectoryPathName;

if (varKnownFolder == null)
{
objFolder = await _Storage.Client.Files.GetMetadataAsync(strPathName);
}
else
{
// The code enters HERE BECAUSE varKnownFolder is not null
_FolderPathToInfoMap.Add(strDirectoryPathName, varKnownFolder);
}

return objFolder;
}

}


Related Topics



Leave a reply



Submit