免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 1645 | 回复: 7
打印 上一主题 下一主题

[其他] 怎么没有C#模板啊?论坛里面有群么? [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2015-09-11 08:22 |只看该作者 |倒序浏览
怎么没有C#模板啊?论坛里面有群么?

663de81190ef76c6028d9c1a9e16fdfaae516780.png (23.02 KB, 下载次数: 38)

663de81190ef76c6028d9c1a9e16fdfaae516780.png

论坛徽章:
0
2 [报告]
发表于 2015-09-17 11:12 |只看该作者
没人回答么?

论坛徽章:
0
3 [报告]
发表于 2015-09-17 11:13 |只看该作者
自己给自己顶!

论坛徽章:
0
4 [报告]
发表于 2015-09-17 11:14 |只看该作者
人呢?出来

论坛徽章:
0
5 [报告]
发表于 2015-09-17 11:16 |只看该作者

论坛徽章:
59
2015年亚洲杯之约旦
日期:2015-01-27 21:27:392015年亚洲杯之日本
日期:2015-02-06 22:09:41拜羊年徽章
日期:2015-03-03 16:15:432015年辞旧岁徽章
日期:2015-03-03 16:54:152015年迎新春徽章
日期:2015-03-04 09:50:282015元宵节徽章
日期:2015-03-06 15:50:392015年亚洲杯之阿联酋
日期:2015-03-19 17:39:302015年亚洲杯之中国
日期:2015-03-23 18:52:23巳蛇
日期:2014-12-14 22:44:03双子座
日期:2014-12-10 21:39:16处女座
日期:2014-12-02 08:03:17天蝎座
日期:2014-07-21 19:08:47
6 [报告]
发表于 2015-09-17 18:14 |只看该作者
有噬,  m:n线程模板
  1. // -- Multi-threads --
  2. namespace ntrt
  3. {
  4.     /// <summary>
  5.     ///     -- sample code (parallel exec with 64 threads) --
  6.     ///        ntrt.ThreadAddress<int, int> thstart = delegate(int obj)
  7.     ///        {
  8.     ///            Console.Write(string.Format("{0}\n", obj));
  9.     ///            return obj;
  10.     ///        };

  11.     ///        ntrt.ParallelThread<int, int> thrds = new ntrt.ParallelThread<int, int>();
  12.     ///        for (int iC = 0; iC < 2048; iC++)
  13.     ///        {
  14.     ///            thrds.Add(thstart, iC);
  15.     ///        }
  16.     ///        int[] rts = ntrt.ParallelThread<int, int>.wait(thrds.exec(64));   //Environment.ProcessorCount *8);
  17.     ///        Console.WriteLine("\n-------------------------------------------------");
  18.     ///        foreach(int iret in rts){
  19.     ///            Console.Write(string.Format("{0}\n",iret));
  20.     ///        }
  21.     /// </summary>
  22.     /// <typeparam name="TRET">return value type</typeparam>
  23.     /// <typeparam name="TPARAM">parameter type</typeparam>
  24.     public delegate TRET ThreadAddress<TRET, TPARAM>(TPARAM obj); // where TRET : class;
  25.     public delegate void FinishEventHandler<TRET, TPARAM>(ParallelThread<TRET, TPARAM> sender, TPARAM args, TRET ret, params object[] envs);
  26.     public class ParallelThread<TRET, TPARAM> : List<KeyValuePair<ThreadAddress<TRET, TPARAM>, TPARAM>>
  27.     {
  28.         private const int IDX_SELF = 0;
  29.         private const int IDX_NPARALLEL = 1;
  30.         private const int IDX_COUNTER = 2;
  31.         private const int IDX_RETURNS = 3;
  32.         //private const int IDX_HANDLE = 1;

  33.         public object exec(int nThread, TRET defBadRetval, params object[] envs)
  34.         {
  35.             this.isCancelled = false;
  36.             ParameterizedThreadStart inner_start = delegate (object argobj)
  37.             {
  38.                 object[] arglist = argobj as object[];
  39.                 System.Diagnostics.Debug.Assert(arglist.Length == 4);
  40.                 System.Diagnostics.Debug.Assert(arglist[IDX_SELF] is ParallelThread<TRET, TPARAM>);
  41.                 //System.Diagnostics.Debug.Assert(arglist[IDX_HANDLE] is IntPtr);
  42.                 System.Diagnostics.Debug.Assert(arglist[IDX_NPARALLEL] is int);
  43.                 System.Diagnostics.Debug.Assert(arglist[IDX_COUNTER] is InnerCounter);
  44.                 System.Diagnostics.Debug.Assert(arglist[IDX_RETURNS] is TRET[]);

  45.                 ParallelThread<TRET, TPARAM> thrds = arglist[IDX_SELF] as ParallelThread<TRET, TPARAM>;
  46.                 //IntPtr hGcHandle = (IntPtr)arglist[IDX_HANDLE];
  47.                 int nThreads = (int)arglist[IDX_NPARALLEL];
  48.                 InnerCounter counter = (InnerCounter)arglist[IDX_COUNTER];
  49.                 TRET[] retvals = arglist[IDX_RETURNS] as TRET[];
  50.                 do
  51.                 {
  52.                     int iCount = 0;
  53.                     lock (counter)
  54.                     {
  55.                         counter.iCount++;
  56.                         iCount = counter.iCount;
  57.                     }
  58.                     //int iCount = Interlocked.Increment(ref counter.iCount);   //<- C# is unreliable... :-(.

  59.                     if (iCount < counter.nCount)
  60.                     {
  61.                         if (thrds[iCount].Key != null)
  62.                         {
  63.                             retvals[iCount] = thrds[iCount].Key(thrds[iCount].Value);
  64.                             if (thrds.Onfinished != null)
  65.                             {
  66.                                 thrds.Onfinished(thrds, thrds[iCount].Value, retvals[iCount], envs);
  67.                             }
  68.                         }
  69.                     }
  70.                     else
  71.                     {
  72.                         break;
  73.                     }
  74.                 } while (this.isCancelled == false);

  75.                 int nComplete = 0;
  76.                 lock (counter)
  77.                 {
  78.                     counter.nComplete++;
  79.                     nComplete = counter.nComplete;
  80.                 }
  81.                 if (nComplete == nThreads)
  82.                 {
  83.                     if (thrds.OnCompeleted != null)
  84.                     {
  85.                         thrds.OnCompeleted(thrds, new EventArgs());
  86.                     }
  87.                 }
  88.             };

  89.             TRET[] rets = new TRET[this.Count];
  90.             for (int iRet = 0; iRet < rets.Length; iRet++)
  91.             {
  92.                 rets[iRet] = defBadRetval;
  93.             }

  94.             int nRealThread = nThread;
  95.             if (nRealThread > this.Count)
  96.             {
  97.                 nRealThread = this.Count;
  98.             }
  99.             InnerCounter ic = new InnerCounter() { iCount = -1, nComplete = 0, nCount = this.Count };
  100.             Thread[] threads = new Thread[nRealThread];
  101.             for (int iThd = 0; iThd < threads.Length; iThd++)
  102.             {
  103.                 threads[iThd] = new Thread(inner_start);
  104.                 threads[iThd].Start(new object[] { this, nRealThread, ic, rets });
  105.             }

  106.             return new object[] { rets, threads } as object;

  107.             //GCHandle gh = GCHandle.Alloc(new object[] { rets, threads });
  108.             //IntPtr hgh = GCHandle.ToIntPtr(gh);
  109.             //return hgh;
  110.         }//end function: exec

  111.         public void Add(ThreadAddress<TRET, TPARAM> thaddr, TPARAM args)
  112.         {
  113.             this.Add(new KeyValuePair<ThreadAddress<TRET, TPARAM>, TPARAM>(thaddr, args));
  114.         }
  115.         public bool isCancelled { set; get; }
  116.         public event FinishEventHandler<TRET, TPARAM> Onfinished;
  117.         public event EventHandler OnCompeleted;
  118.         public object UserData { set; get; }

  119.         // -- Helper function --
  120.         public void abort(object h)
  121.         {
  122.             object[] args = h as object[];

  123.             System.Diagnostics.Debug.Assert(args.Length == 2);
  124.             System.Diagnostics.Debug.Assert(args[0] is TRET[]);
  125.             System.Diagnostics.Debug.Assert(args[1] is Thread[]);
  126.             foreach (Thread thd in args[1] as Thread[])
  127.             {
  128.                 try
  129.                 {
  130.                     thd.Abort();
  131.                 }
  132.                 catch (Exception ex)
  133.                 {
  134.                     System.Diagnostics.Debug.Assert(ex != null);
  135.                 }
  136.             }

  137.             if (this.OnCompeleted != null)
  138.             {
  139.                 this.OnCompeleted(this, new EventArgs());
  140.             }
  141.         }
  142.         public static TRET[] wait(object h)
  143.         {
  144.             object[] args = h as object[];

  145.             System.Diagnostics.Debug.Assert(args.Length == 2);
  146.             System.Diagnostics.Debug.Assert(args[0] is TRET[]);
  147.             System.Diagnostics.Debug.Assert(args[1] is Thread[]);

  148.             Thread[] threads = args[1] as Thread[];
  149.             foreach (Thread thd in threads)
  150.             {
  151.                 thd.Join(); //<- Wait for all thread exits
  152.             }

  153.             TRET[] rets = args[0] as TRET[];
  154.             return rets;
  155.         }//end function: wait

  156.         public static int nLogicCPUCores { get { return Environment.ProcessorCount; } }
  157.         private class InnerCounter
  158.         {
  159.             public volatile int iCount = 0;
  160.             public int nCount = 0;
  161.             public volatile int nComplete = 0;
  162.         }
  163.     }//end class: class ParallelThread<TRET, TPARAM>
  164. }//end namespace: ntrt
复制代码

论坛徽章:
1
2015年辞旧岁徽章
日期:2015-03-03 16:54:15
7 [报告]
发表于 2015-09-20 21:25 |只看该作者
c#的泛型没啥好说的,非图灵完备,基本就是一坨屎。没有分支没有赋值,神马都玩不出来。

论坛徽章:
89
水瓶座
日期:2014-04-01 08:53:31天蝎座
日期:2014-04-01 08:53:53天秤座
日期:2014-04-01 08:54:02射手座
日期:2014-04-01 08:54:15子鼠
日期:2014-04-01 08:55:35辰龙
日期:2014-04-01 08:56:36未羊
日期:2014-04-01 08:56:27戌狗
日期:2014-04-01 08:56:13亥猪
日期:2014-04-01 08:56:02亥猪
日期:2014-04-08 08:38:58程序设计版块每日发帖之星
日期:2016-01-05 06:20:00程序设计版块每日发帖之星
日期:2016-01-07 06:20:00
8 [报告]
发表于 2015-09-21 08:52 |只看该作者
本帖最后由 fender0107401 于 2015-09-21 08:52 编辑

Unix-Like下面能写C#吗?
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP