Skip to main content
Code Review

Return to Question

Notice removed Draw attention by Community Bot
Bounty Ended with no winning answer by Community Bot
added 85 characters in body
Source Link
private void InvokeAndComplete (Action<T> act, TaskCompletionSource<byte> promise)
{
 try {
 act (internalValue);
 } finally {
 // some value we will ignore
 promise.SetResult (1);
 }
}
public Task Alter (Action<T> alterAction)
{
 TaskCompletionSource<byte> promise = new TaskCompletionSource<byte> ();
 Task localLastTask;
 do {
 // Not sure if Volatile.Read is necessary
 localLastTask = Volatile.Read (ref lastTask);
 } while (Interlocked.CompareExchange (ref lastTask, promise.Task, localLastTask) != localLastTask);
 // only this thread has a reference to the object referenced by localLastTask
 return localLastTask.ContinueWith ((currentTask) => {
 InvokeAndComplete (alterAction, promise);
 }, taskScheduler);
}
private void InvokeAndComplete (Action<T> act, TaskCompletionSource<byte> promise)
{
 try {
 act (internalValue);
 } finally {
 // some value we will ignore
 promise.SetResult (1);
 }
}
public Task Alter (Action<T> alterAction)
{
 TaskCompletionSource<byte> promise = new TaskCompletionSource<byte> ();
 Task localLastTask;
 do {
 // Not sure if Volatile.Read is necessary
 localLastTask = Volatile.Read (ref lastTask);
 } while (Interlocked.CompareExchange (ref lastTask, promise.Task, localLastTask) != localLastTask);
 return localLastTask.ContinueWith ((currentTask) => {
 InvokeAndComplete (alterAction, promise);
 }, taskScheduler);
}
private void InvokeAndComplete (Action<T> act, TaskCompletionSource<byte> promise)
{
 try {
 act (internalValue);
 } finally {
 // some value we will ignore
 promise.SetResult (1);
 }
}
public Task Alter (Action<T> alterAction)
{
 TaskCompletionSource<byte> promise = new TaskCompletionSource<byte> ();
 Task localLastTask;
 do {
 // Not sure if Volatile.Read is necessary
 localLastTask = Volatile.Read (ref lastTask);
 } while (Interlocked.CompareExchange (ref lastTask, promise.Task, localLastTask) != localLastTask);
 // only this thread has a reference to the object referenced by localLastTask
 return localLastTask.ContinueWith ((currentTask) => {
 InvokeAndComplete (alterAction, promise);
 }, taskScheduler);
}
added 857 characters in body
Source Link

Doing this now

private void InvokeAndComplete (Action<T> act, TaskCompletionSource<byte> promise)
{
 try {
 act (internalValue);
 } finally {
 // some value we will ignore
 promise.SetResult (1);
 }
}
public Task Alter (Action<T> alterAction)
{
 TaskCompletionSource<byte> promise = new TaskCompletionSource<byte> ();
 Task localLastTask;
 do {
 // Not sure if Volatile.Read is necessary
 localLastTask = Volatile.Read (ref lastTask);
 } while (Interlocked.CompareExchange (ref lastTask, promise.Task, localLastTask) != localLastTask);
 return localLastTask.ContinueWith ((currentTask) => {
 InvokeAndComplete (alterAction, promise);
 }, taskScheduler);
}

Doing this now

private void InvokeAndComplete (Action<T> act, TaskCompletionSource<byte> promise)
{
 try {
 act (internalValue);
 } finally {
 // some value we will ignore
 promise.SetResult (1);
 }
}
public Task Alter (Action<T> alterAction)
{
 TaskCompletionSource<byte> promise = new TaskCompletionSource<byte> ();
 Task localLastTask;
 do {
 // Not sure if Volatile.Read is necessary
 localLastTask = Volatile.Read (ref lastTask);
 } while (Interlocked.CompareExchange (ref lastTask, promise.Task, localLastTask) != localLastTask);
 return localLastTask.ContinueWith ((currentTask) => {
 InvokeAndComplete (alterAction, promise);
 }, taskScheduler);
}
added 67 characters in body
Source Link
  • I'd like to be able to ContinueWith the Task task directly, instead of having to start it within a different submitted Action. I feel like I need to create the new Task so that I can atomically exchange the lastTask field's value.
  • Lambda capturing of local variables, partly alleviated with state objects where possible.
  • How can I keep a reference to the currently executing Task, so I can short-circuit execution? Or should I use a poison pill to signal termination and not chain any new tasks?
  • Interlocked for atomicity. Should I use a ConcurrentQueue? How would I manage not having a dedicated thread and only running one Task at a time with a ConcurrentQueue?
  • Is a Volatile.Read of lastTask necessary for localCurrent = lastTask;?
  • I'd like to be able to ContinueWith the Task task directly, instead of having to start it within a different submitted Action. I feel like I need to create the new Task so that I can atomically exchange the lastTask field's value.
  • Lambda capturing of local variables, partly alleviated with state objects where possible.
  • How can I keep a reference to the currently executing Task, so I can short-circuit execution? Or should I use a poison pill to signal termination and not chain any new tasks?
  • Interlocked for atomicity. Should I use a ConcurrentQueue? How would I manage not having a dedicated thread and only running one Task at a time with a ConcurrentQueue?
  • I'd like to be able to ContinueWith the Task task directly, instead of having to start it within a different submitted Action. I feel like I need to create the new Task so that I can atomically exchange the lastTask field's value.
  • Lambda capturing of local variables, partly alleviated with state objects where possible.
  • How can I keep a reference to the currently executing Task, so I can short-circuit execution? Or should I use a poison pill to signal termination and not chain any new tasks?
  • Interlocked for atomicity. Should I use a ConcurrentQueue? How would I manage not having a dedicated thread and only running one Task at a time with a ConcurrentQueue?
  • Is a Volatile.Read of lastTask necessary for localCurrent = lastTask;?
Tweeted twitter.com/#!/StackCodeReview/status/622903990305558528
Notice added Draw attention by Sotirios Delimanolis
Bounty Started worth 50 reputation by Sotirios Delimanolis
Source Link
Loading
lang-cs

AltStyle によって変換されたページ (->オリジナル) /