Skip to content

Instantly share code, notes, and snippets.

@rafacouto
Created January 2, 2015 03:18
Show Gist options
  • Save rafacouto/02b0413bd20935381fae to your computer and use it in GitHub Desktop.
Save rafacouto/02b0413bd20935381fae to your computer and use it in GitHub Desktop.
// compile: mono-csc Threads.cs
// execute: mono Threads.exe
using System;
using System.Threading;
namespace Test
{
class Threads
{
const int THREADS = 2;
const int MAX_COUNT = 10000000;
volatile static int counter = 0;
public static void Run(object nn) {
int n = (int)nn;
for (int i = 0; i < n; i++)
{
counter += 1;
}
}
public static void Main (string[] args)
{
Thread[] threads = new Thread[THREADS];
int i;
for (i=0; i< THREADS; i++) {
threads[i] = new Thread(new ParameterizedThreadStart(Threads.Run));
}
for (i=0; i< THREADS; i++) {
threads[i].Start(MAX_COUNT/THREADS);
}
for (i=0; i< THREADS; i++) {
threads[i].Join();
}
Console.WriteLine("Counter value: {0} Expected: {1}\n", counter, MAX_COUNT);
}
}
}
@rafacouto
Copy link
Author

La tunda sobre Java es notable ;) http://imgur.com/Z0cnKAC

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment