// OrtizOL - xCSw - http://o...content-available-to-author-only...t.com
using System;
using System.Threading;
using System.Threading.Tasks;
namespace Receta.Multithreading.R0208
{
public class UsoSpinWait
{
public static void Main()
{
Console.WriteLine(Environment.NewLine);
bool valorCentinela = false;
int numeroCedes = 0;
// Contamos con SpinWait mientras que la variable
// lógica centinela se hace igual a verdadero:
Task t1 = Task.Factory.StartNew(() =>
{
SpinWait sw = new SpinWait();
while (!valorCentinela)
{
// Determina si se cedió el control al procesador en lugar de
// de simple ~girar (spin):
if (sw.NextSpinWillYield)
{
++numeroCedes;
}
sw.SpinOnce();
}
Console.WriteLine("Número de llamadas de SpinWait: {0} - Cedido {1} veces.", sw.Count, numeroCedes);
}
);
// En otro thread cambiamos el valor de la variable centinela:
Task t2 = Task.Factory.StartNew(() =>
{
Thread.Sleep (200);
valorCentinela = true;
}
);
// Espera hasta que todos los threads finalicen:
Task.WaitAll(t1, t2);
Console.WriteLine(Environment.NewLine);
}
}
}
Ly8gT3J0aXpPTCAtIHhDU3cgLSBodHRwOi8vby4uLmNvbnRlbnQtYXZhaWxhYmxlLXRvLWF1dGhvci1vbmx5Li4udC5jb20KIAp1c2luZyBTeXN0ZW07CnVzaW5nIFN5c3RlbS5UaHJlYWRpbmc7CnVzaW5nIFN5c3RlbS5UaHJlYWRpbmcuVGFza3M7CiAKbmFtZXNwYWNlIFJlY2V0YS5NdWx0aXRocmVhZGluZy5SMDIwOAp7CiAgICBwdWJsaWMgY2xhc3MgVXNvU3BpbldhaXQKICAgIHsKICAgICAgICBwdWJsaWMgc3RhdGljIHZvaWQgTWFpbigpCiAgICAgICAgewogICAgICAgICAgICBDb25zb2xlLldyaXRlTGluZShFbnZpcm9ubWVudC5OZXdMaW5lKTsKICAgICAgICAgICAgCiAgICAgICAgICAgIGJvb2wgdmFsb3JDZW50aW5lbGEgPSBmYWxzZTsKICAgICAgICAgICAgaW50IG51bWVyb0NlZGVzID0gMDsKICAgICAgICAgICAgCiAgICAgICAgICAgIC8vIENvbnRhbW9zIGNvbiBTcGluV2FpdCBtaWVudHJhcyBxdWUgbGEgdmFyaWFibGUgCiAgICAgICAgICAgIC8vIGzDs2dpY2EgY2VudGluZWxhIHNlIGhhY2UgaWd1YWwgYSB2ZXJkYWRlcm86CiAgICAgICAgICAgIFRhc2sgdDEgPSBUYXNrLkZhY3RvcnkuU3RhcnROZXcoKCkgPT4KICAgICAgICAgICAgewogICAgICAgICAgICAgICAgU3BpbldhaXQgc3cgPSBuZXcgU3BpbldhaXQoKTsKICAgICAgICAgICAgICAgIAogICAgICAgICAgICAgICAgd2hpbGUgKCF2YWxvckNlbnRpbmVsYSkKICAgICAgICAgICAgICAgIHsKICAgICAgICAgICAgICAgICAgICAvLyBEZXRlcm1pbmEgc2kgc2UgY2VkacOzIGVsIGNvbnRyb2wgYWwgcHJvY2VzYWRvciBlbiBsdWdhciBkZSAKICAgICAgICAgICAgICAgICAgICAvLyBkZSBzaW1wbGUgfmdpcmFyIChzcGluKToKICAgICAgICAgICAgICAgICAgICBpZiAoc3cuTmV4dFNwaW5XaWxsWWllbGQpCiAgICAgICAgICAgICAgICAgICAgewogICAgICAgICAgICAgICAgICAgICAgICArK251bWVyb0NlZGVzOwogICAgICAgICAgICAgICAgICAgIH0KICAgICAgICAgICAgICAgICAgICAKICAgICAgICAgICAgICAgICAgICBzdy5TcGluT25jZSgpOwogICAgICAgICAgICAgICAgfQogICAgICAgICAgICAgICAgCiAgICAgICAgICAgICAgICBDb25zb2xlLldyaXRlTGluZSgiTsO6bWVybyBkZSBsbGFtYWRhcyBkZSBTcGluV2FpdDogezB9IC0gQ2VkaWRvIHsxfSB2ZWNlcy4iLCBzdy5Db3VudCwgbnVtZXJvQ2VkZXMpOwogICAgICAgICAgICB9CiAgICAgICAgICAgICk7CiAgICAgICAgICAgIAogICAgICAgICAgICAvLyBFbiBvdHJvIHRocmVhZCBjYW1iaWFtb3MgZWwgdmFsb3IgZGUgbGEgdmFyaWFibGUgY2VudGluZWxhOgogICAgICAgICAgICBUYXNrIHQyID0gVGFzay5GYWN0b3J5LlN0YXJ0TmV3KCgpID0+IAogICAgICAgICAgICB7CiAgICAgICAgICAgICAgICBUaHJlYWQuU2xlZXAgKDIwMCk7CiAgICAgICAgICAgICAgICB2YWxvckNlbnRpbmVsYSA9IHRydWU7CiAgICAgICAgICAgIH0KICAgICAgICAgICAgKTsKICAgICAgICAgICAgCiAgICAgICAgICAgIC8vIEVzcGVyYSBoYXN0YSBxdWUgdG9kb3MgbG9zIHRocmVhZHMgZmluYWxpY2VuOgogICAgICAgICAgICBUYXNrLldhaXRBbGwodDEsIHQyKTsKICAgICAgICAgICAgCiAgICAgICAgICAgIENvbnNvbGUuV3JpdGVMaW5lKEVudmlyb25tZW50Lk5ld0xpbmUpOwogICAgICAgIH0KICAgIH0KfQ==