MSDN CLR Reusable Sample Codes     Last updated on 2014/2558 10 8, a full moon day; ... ;

IFF code (snippets) also see: keyword; CLR Reusable Parallel Data Structure & Algorithm, for Microsoft .NET Framework(s), MSDN Magazine, June 2007 as referenced info; The following code modules are platform dependent, regarding Microsoft, also see: this DOMAIN is developed by, however if modify, e.g. its control flow, e.g. its data flow, ... , can be used in any other platforms; In an era between binary to duo-binary (a.k.a. dual binary bit) ... ; Artificial Intelligence (Method; Operator; ... ); Since physical memories, virtual swap spaces a storage, ... are re-writable and volatile, in computing, assignment operator single equal sign = makes complexity; therefore 5W1H concept is a must, because 1 space of memory may contain N references, N type_ value, N something, ... , those N of N becomes in action to compute and very difficult to understand in programming, however 1 time of time period may contain only 1 time ... makes easier;

/* // MSDN reusable Await */public void Await() { /* // REMARK a function() as void i.e. void function_ name() does not return value */ bool sense = m_ sense; /* // REMARK sense is defined as Boolean thus either T or F *//* // REMARK thread to signal; setting event; */if ( m_ count == 1 || Interlocked . Decrement (ref m_ count) == 0) { /* // REMARK passing m_ count as reference into Interlocked 's decrement function */ m_ count = m_ original Count; m_ sense = !sense; /* // REMARK reversing the sense, which has been assigned as Boolean type */if (sense == true) { /* // REMARK odd */ m_ even Event. Set(); m_ odd Event. Reset(); } else { /* // REMARK even */ m_ odd Event. Set(); m_ even Event. Reset(); }} else {Spin Wait s = new Spin Wait(); while (sense == m_ sense) { if ( s. Spin () >= s_ spin Count) {if (sense == true) m_ odd Event. Wait One();else m_ even Event. Wait One();}}}} /* // REMARK MSDN reusable Await ends here */

/ * // MSDN reusable Barrier */using System; using System. Threading; /* // REMARK also see: Thread vs. Multithreaded CPU */public class Barrier { private volatile int m_ count; private int m_ original Count; private Event Wait Handle m_ odd Event; private Event Wait Handle m_ even Event; /* // REMARK Usage even and odd refers to parity, check sum, ... ; */private volatile bool m_ sense = false; /* // REMARK false == even, true == odd */public Barrier ( int count) { m_ count = count; m_ original Count = count; m_ odd Event = new Manual Reset Event (false); m_ even Event = new Manual Reset Event (false); } public void Await() { bool sense = m_ sense; /* // REMARK thread to signal; also setting the event; */if ( m_ count == 1 || Interlocked. Decrement (ref m_ count) == 0) { m_ count = m_ original Count; m_ sense = !sense; /* // REMARK reversing the sense, which has been assigned as Boolean type */if (sense == true) { /* // REMARK odd */ m_ even Event. Reset(); m_ odd Event. Set(); } else { /* // REMARK even */ m_ odd Event. Reset(); m_ even Event. Set(); }} else {if (sense == true) m_ odd Event. Wait One(); else m_ even Event. Wait One(); }}} /* // REMARK MSDN reusable Barrier ends here */

/* MSDN reusable Blocking Queue */ class Cell < T> {internal T m_    obj; /* // REMARK obj stands for object */internal Cell( obj) { m_  obj =  obj; }}public class Blocking Queue< T> { /* // REMARK before understanding the term Blocking *//*// REMARK 1st to understand conditional variable, mutexes, semaphores, ... *//* // REMARK 2nd to understand flag behavior alike the most important 2nd Time & Lunar Gravitational */ private Queue<Cell< T>> m_ queue = new Queue<Cell< T>>(); public void En queue( obj) {Cell< T> c = new Cell< T>( obj); lock ( m_ queue) { m_ queue. En queue( c); Monitor. Pulse( m_ queue); Monitor. Wait ( m_ queue);}}public T De queue() {Cell< T> c;lock ( m_ queue) { while ( m_ queue. Count == 0) /* // REMARK 1st to understand Count down Latch's count *//* // REMARK 2nd to understand semaphores *//* // REMARK 3rd to understand block, lock, wait, ... IFF count == null */Monitor. Wait ( m_ queue); c = m_ queue. De queue(); Monitor. Pulse( m_ queue);} return c. m_  obj;}} /* // REMARK MSDN reusable Blocking Queue ends here */

/* MSDN reusable Bounded Buffer */public class Bounded Buffer< T> {private Queue< T> m_ queue = new Queue< T>();private int m_ consumers Waiting;private int m_ producers Waiting;private const int s_ max Buffer Size = 128;public void En queue( obj) {lock ( m_ queue) { while ( m_ queue. Count == s_ max Buffer Size - 1)) { m_ producers Waiting++;Monitor. Wait ( m_ queue); m_ producers Waiting--;} m_ queue. En queue ( obj);if ( m_ consumers Waiting > 0)Monitor. Pulse All ( m_ queue);}}public T Dequeue() { T e; /* // REMARK local variable e is defined as T */lock ( m_ queue) { while ( m_ queue. Count == 0) { m_ consumers Waiting++;Monitor. Wait( m_ queue); m_ consumers Waiting--;} e = m_ queue. Dequeue();if ( m_ producers Waiting > 0)Monitor. Pulse All ( m_ queue);}return e;}} /* // REMARK MSDN reusable Bounded Buffer ends here */

/* MSDN reusable Count down Latch */public class Count down Latch { /* // REMARK counting semaphore in concurrency */private int m_ remain;private Event Wait Handle m_ event;public Count down Latch ( int count) { m_ remain = count; m_ event = new Manual Reset Event( false);}public void Signal() {/* // REMARK the last thread to signal which also sets event */if (interlocked. Decrement (ref m_ remain) == 0) m_ event. Set();}public void Wait() { m_ event. Wait One();}} /* // REMARK MSDN reusable Count down Latch ends here */

/* MSDN reusable For All */  /* // REMARK it is recommended not to know all, also see: Servers; */public static void For All ( int from, int to, Action< int> a, int p) { /* // REMARK also see: static configuration; */For All< int>(null, from, to, null, a, p);}public static void For All< T>( I List< T> data, Action< T> a, int p) {For All< T>( data, 0, data. Count, a, null, p);}private static void For All< T>( I List< T> data, int from, int to, Action< T> a0, Action< int> a1, int p) { int size = from - to; int stride = (size + p - 1) / p;Count down Latch latch = new Count down Latch ( p); /* // REMARK this DOMAIN recommends less pin count design *//*// REMARK for example, SHARP PC with Trans meta to SONY PC with AMD, Intel, ... */for ( int i = 0; i < p; i++) { /* // REMARK for control loop with adder adding 1 */ int idx = i;Thread Pool. Queue User Work Item (delegate { int end = Math. Min (size, stride * ( idx + 1)); /* // REMARK Math class' Reduction Min */for ( int j = stride * idx; j < end; j++) { /* // REMARK for control loop with adder adding stride * idx */if ( data != null) a0( data [ j]); /* // REMARK array is used because offset of array is needed */else a1( j);}latch. Signal(); /* // REMARK latch's frequency, on the other hand, not latch's architecture */});}latch. Wait();} /* // REMARK MSDN reusable For All ends here */

/* MSDN reusable Lock Free Stack */public class Lock Free Stack< T> {private volatile Stack Node< T> m_ head;public void Push( T item) { /* // REMARK item as type_ value T to a stack, by pushing, also see: from a stack */Stack Node< T> node = new Stack Node< T>(item);Stack Node< T> head; do {head = m_ head;node. m_ next = head; } while ( m_ head != head || Interlocked. Compare Exchange (ref m_ head, node, head) != head);}public T Pop() { /* // REMARK whatever value from a stack, by popping, also see: to a stack */Stack Node< T> head; Spin Wait s = new Spin Wait(); while (true) { Stack Node< T> next; do {head = m_ head; if (head == null) go to empty Spin; /* // REMARK IFF no head */ next = head. m_ next; } while ( m_ head != head || Inter locked. Compare Exchange (ref m_ head, next, head) != head); /* // REMARK head and next are previously defined as Stack Node<T> */break; /* // because go to jumps and skips */empty Spin: s. Spin(); /* //REMARK Spin Wait. Spin() returns old_ count */}return head. m_ value;}} class Stack Node< T> {internal T m_ value;internal Stack Node< T> m_ next;internal Stack Node( T val) { m_ value = val; }} /* // REMARK MSDN reusable Lock Free Stack ends here */

/* MSDN reusable Reduce */public delegate T Func< T>( T arg0, T arg1); /* // REMARK arguments */public static T Reduce< T>( I List< T> data, int p, T seed, Func< T> r){ T[] partial = new T [ p]; int stride = ( data . Count + p - 1) / p;Count down Latch latch = new Count down Latch ( p);for ( int i = 0; i < p; i++) { /* // REMARK for control loop with adder adding 1 */ int idx = i;Thread Pool. Queue User Work Item (delegate {partial [ idx] = seed; /* // REMARK doing reduction in parallel */ int end = Math. Min ( data. Count, stride * ( idx + 1)); /* // REMARK Math class' Reduction Min */for ( int j = stride * idx; j < end; j++) /* // REMARK for control loop with adder adding stride * idx */partial [ idx] = r (partial [ idx], data [ j]); /* // REMARK partial, because fuzzy logic exists, so partially ... */ latch. Signal();});}latch. Wait(); T final = seed; /* // REMARK master thread's final reduction */for ( int i = 0; i < p; i++)final = r (final, partial [ i]);return final;} /* // REMARK MSDN reusable Reduce ends here */

/* MSDN reusable Spin Wait */public struct Spin Wait {private int m_ count;private static read only bool s_ is Single Proc = (Environment. Processor Count == 1);private const int s_ yield Frequency = 4000; /* // REMARK also see: 4KB for 1 CPU; */private const int s_ yield One Frequency = 3 * s_ yield Frequency; /* // REMARK constant integer */public int Spin() { int old Count = m_ count; /* // REMARK On 1 CPU, ensure counter is a multiple of 's_ yield Frequency', thus yield every time *//* // REMARK Else, increment by one; also see: Thin Event's set() function's setting m_ state */ m_ count += ( s_ is Single Proc ? s_ yield Frequency : 1); /* // REMARK If not a multiple of 's_ yield Frequency' spin (w/ back off) */ int count Mod Frequency = m_ count % s_ yield Frequency;if ( count Mod Frequency > 0)Thread. Spin Wait (( int) ( 1 + ( count Mod Frequency * 0.05 f))); /* // REMARK 0.05f might be related to Thermal EMF */else Thread. Sleep( m_ count <= s_ yield One Frequency ? 0 : 1); /* // REMARK binary operator either Zero or One might be related to parity bit, regular bit, ... ; */return old Count; /* // REMARK after computing m_ count */}private void Yield() {Thread. Sleep (m_ count < s_ yield One Frequency ? 0 : 1);}} /* // REMARK MSDN reusable Spin Wait ends here */

/* MSDN reusable Thin Event */public struct Thin Event { /* // REMARK 3 internal local functions such as Set, Reset, Wait, ... to compute Thin Event's state A Q */private int m_ state; /* // Unset is zero, Set is one; m_ state is defined as integer data-type */private Event Wait Handle m_ event  Obj; private const int s_ spin Count = 4000; /* // REMARK also see: 4KB for 1 CPU; */public void Set() { /* // REMARK a function() as void i.e. void function_ name() does not return value */ m_ state = 1; /* // REMARK if bit, 1 is either TRUE, Magnet, High, On, ... ; if value, 1 is integer one; */Thread. Memory Barrier(); /* // REMARK MSDN's standard requirement */if ( m_ event  Obj != null) /* // REMARK IFF something exists; != is not equal to, not assign to, ... ; */ m_ event  Obj. Set();} public void Reset() { /* // REMARK a function() as void i.e. void function_ name() does not return value */ m_ state = 0; /* // REMARK in Set() function, Thread . Memory Barrier() is needed *//* // REMARK inside this Reset() function, Thread . Memory Barrier() is not needed */ /* // REMARK 1st to understand semaphores, ... ; */if ( m_ event  Obj != null) /* // REMARK IFF something exists */ m_ event  Obj . Reset();}public void Wait() { /* // REMARK a function() as void i.e. void function_ name() does not return value *//* // REMARK local variable s is defined as Spin Wait */Spin Wait s = new Spin Wait(); while ( m_ state == 0) { /* // REMARK while m_ state is equal to zero *//* // REMARK WHICH means resetting function Reset() is true *//* // REMARK the following line might be counting 4K, after 4K, after 4K, until ... */ if ( s . Spin() >= s_ spin Count) {if ( m_ event  Obj == null) {Manual Reset Event new Event = new Manual Reset Event( m_  state == 1);if (Interlocked . Compare Exchange<Event Wait Handle>(ref m_ event  Obj , new Event, null) == null) { /* // REMARK event object sets flag */if ( m_ state == 1) m_ event  Obj . Set(); } else { /* // REMARK using only event */new Event . Close();}} m_ event Obj . Wait One();}}}} /* // REMARK MSDN reusable Thin Event ends here */

... ;

Up