PageRenderTime 40ms CodeModel.GetById 13ms RepoModel.GetById 0ms app.codeStats 0ms

/base/Applications/Tests/ThreadTest/ThreadTest.cs

#
C# | 87 lines | 64 code | 15 blank | 8 comment | 3 complexity | c9789e527de6fb392d3c339ef10cf143 MD5 | raw file
  1. ////////////////////////////////////////////////////////////////////////////////
  2. //
  3. // Microsoft Research Singularity
  4. //
  5. // Copyright (c) Microsoft Corporation. All rights reserved.
  6. //
  7. // Note: Simple Singularity test program.
  8. //
  9. using System;
  10. using System.Threading;
  11. using Microsoft.Singularity.V1.Services;
  12. using Microsoft.Singularity.Channels;
  13. using Microsoft.Contracts;
  14. using Microsoft.SingSharp.Reflection;
  15. using Microsoft.Singularity.Applications;
  16. using Microsoft.Singularity.Io;
  17. using Microsoft.Singularity.Configuration;
  18. [assembly: Transform(typeof(ApplicationResourceTransform))]
  19. namespace Microsoft.Singularity.Applications
  20. {
  21. [ConsoleCategory(DefaultAction=true)]
  22. internal class Parameters {
  23. [InputEndpoint("data")]
  24. public readonly TRef<UnicodePipeContract.Exp:READY> Stdin;
  25. [OutputEndpoint("data")]
  26. public readonly TRef<UnicodePipeContract.Imp:READY> Stdout;
  27. reflective internal Parameters();
  28. internal int AppMain() {
  29. return ThreadTest.AppMain(this);
  30. }
  31. }
  32. public class ThreadTest
  33. {
  34. public static void FirstThreadMethod()
  35. {
  36. Console.WriteLine("First thread!");
  37. DebugStub.Print("First thread!\n");
  38. for (int i = 0; i < 10; i++) {
  39. Console.WriteLine("[0] ... ");
  40. Thread.Yield();
  41. }
  42. Console.WriteLine("First thread done!");
  43. DebugStub.Print("First thread done!\n");
  44. }
  45. public static void SecondThreadMethod()
  46. {
  47. Console.WriteLine("Second thread!");
  48. DebugStub.Print("Second thread!\n");
  49. for (int i = 0; i < 10; i++) {
  50. Console.WriteLine(" ... [1]");
  51. Thread.Yield();
  52. }
  53. Console.WriteLine("Second thread done!");
  54. DebugStub.Print("Second thread done!\n");
  55. }
  56. internal static int AppMain(Parameters! config)
  57. {
  58. Thread t1 = new Thread(new ThreadStart(FirstThreadMethod));
  59. Thread t2 = new Thread(new ThreadStart(SecondThreadMethod));
  60. Console.WriteLine("Starting first thread.");
  61. t1.Start();
  62. Console.WriteLine("Started first thread.");
  63. Console.WriteLine("Starting second thread.");
  64. t2.Start();
  65. Console.WriteLine("Started second thread.");
  66. for (int i = 0; i < 30; i++) {
  67. Thread.Yield();
  68. }
  69. return 0;
  70. }
  71. }
  72. }