Click here to Skip to main content
15,884,353 members
Articles / Programming Languages / C#

Concurrency Hazards: False Sharing

Rate me:
Please Sign up or sign in to vote.
4.95/5 (110 votes)
10 Jan 2010CPOL13 min read 308.4K   1K   115  
False sharing and how to detect and avoid it.
namespace FalseSharingDemo
{
	partial class Form1
	{
		/// <summary>
		/// Required designer variable.
		/// </summary>
		private System.ComponentModel.IContainer components;

		/// <summary>
		/// Clean up any resources being used.
		/// </summary>
		/// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
		protected override void Dispose( bool disposing )
		{
			if ( disposing && ( components != null ) )
			{
				components.Dispose();
			}
			base.Dispose( disposing );
		}

		#region Windows Form Designer generated code

		/// <summary>
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{
			this.components = new System.ComponentModel.Container();
			this.zg = new ZedGraph.ZedGraphControl();
			this.btnTest = new System.Windows.Forms.Button();
			this.pb = new System.Windows.Forms.ProgressBar();
			this.tbAccuracy = new System.Windows.Forms.TrackBar();
			this.label2 = new System.Windows.Forms.Label();
			this.cbOneWriter = new System.Windows.Forms.CheckBox();
			this.groupBox1 = new System.Windows.Forms.GroupBox();
			this.label4 = new System.Windows.Forms.Label();
			this.label3 = new System.Windows.Forms.Label();
			this.gbGraphOptions = new System.Windows.Forms.GroupBox();
			this.rbEfficiency = new System.Windows.Forms.RadioButton();
			this.rbSpeedup = new System.Windows.Forms.RadioButton();
			this.groupBox1.SuspendLayout();
			this.gbGraphOptions.SuspendLayout();
			this.SuspendLayout();
			// 
			// zg
			// 
			this.zg.Anchor = ( ( System.Windows.Forms.AnchorStyles ) ( ( ( ( System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom )
						| System.Windows.Forms.AnchorStyles.Left )
						| System.Windows.Forms.AnchorStyles.Right ) ) );
			this.zg.Location = new System.Drawing.Point( 12, 157 );
			this.zg.Name = "zg";
			this.zg.ScrollGrace = 0D;
			this.zg.ScrollMaxX = 0D;
			this.zg.ScrollMaxY = 0D;
			this.zg.ScrollMaxY2 = 0D;
			this.zg.ScrollMinX = 0D;
			this.zg.ScrollMinY = 0D;
			this.zg.ScrollMinY2 = 0D;
			this.zg.Size = new System.Drawing.Size( 560, 393 );
			this.zg.TabIndex = 7;
			// 
			// btnTest
			// 
			this.btnTest.Location = new System.Drawing.Point( 134, 39 );
			this.btnTest.Name = "btnTest";
			this.btnTest.Size = new System.Drawing.Size( 75, 23 );
			this.btnTest.TabIndex = 3;
			this.btnTest.Text = "Start";
			this.btnTest.UseVisualStyleBackColor = true;
			this.btnTest.Click += new System.EventHandler( this.btnTest_Click );
			// 
			// pb
			// 
			this.pb.Anchor = ( ( System.Windows.Forms.AnchorStyles ) ( ( ( System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left )
						| System.Windows.Forms.AnchorStyles.Right ) ) );
			this.pb.Location = new System.Drawing.Point( 12, 128 );
			this.pb.Name = "pb";
			this.pb.Size = new System.Drawing.Size( 560, 23 );
			this.pb.TabIndex = 6;
			// 
			// tbAccuracy
			// 
			this.tbAccuracy.Location = new System.Drawing.Point( 6, 35 );
			this.tbAccuracy.Maximum = 9;
			this.tbAccuracy.Minimum = 1;
			this.tbAccuracy.Name = "tbAccuracy";
			this.tbAccuracy.Size = new System.Drawing.Size( 104, 45 );
			this.tbAccuracy.TabIndex = 1;
			this.tbAccuracy.Value = 3;
			// 
			// label2
			// 
			this.label2.AutoSize = true;
			this.label2.Location = new System.Drawing.Point( 33, 19 );
			this.label2.Name = "label2";
			this.label2.Size = new System.Drawing.Size( 51, 13 );
			this.label2.TabIndex = 0;
			this.label2.Text = "accuracy";
			// 
			// cbOneWriter
			// 
			this.cbOneWriter.AutoSize = true;
			this.cbOneWriter.Location = new System.Drawing.Point( 12, 96 );
			this.cbOneWriter.Name = "cbOneWriter";
			this.cbOneWriter.Size = new System.Drawing.Size( 197, 17 );
			this.cbOneWriter.TabIndex = 2;
			this.cbOneWriter.Text = "Only first thread writes ( others read )";
			this.cbOneWriter.UseVisualStyleBackColor = true;
			// 
			// groupBox1
			// 
			this.groupBox1.Controls.Add( this.label4 );
			this.groupBox1.Controls.Add( this.label3 );
			this.groupBox1.Location = new System.Drawing.Point( 239, 13 );
			this.groupBox1.Name = "groupBox1";
			this.groupBox1.Size = new System.Drawing.Size( 212, 69 );
			this.groupBox1.TabIndex = 4;
			this.groupBox1.TabStop = false;
			this.groupBox1.Text = " #define ";
			// 
			// label4
			// 
			this.label4.AutoSize = true;
			this.label4.Location = new System.Drawing.Point( 14, 42 );
			this.label4.Name = "label4";
			this.label4.Size = new System.Drawing.Size( 180, 13 );
			this.label4.TabIndex = 1;
			this.label4.Text = "Spacing: distance between counters";
			// 
			// label3
			// 
			this.label3.AutoSize = true;
			this.label3.Location = new System.Drawing.Point( 14, 22 );
			this.label3.Name = "label3";
			this.label3.Size = new System.Drawing.Size( 184, 13 );
			this.label3.TabIndex = 0;
			this.label3.Text = "Padding: index of first counter in array";
			// 
			// gbGraphOptions
			// 
			this.gbGraphOptions.Controls.Add( this.rbEfficiency );
			this.gbGraphOptions.Controls.Add( this.rbSpeedup );
			this.gbGraphOptions.Location = new System.Drawing.Point( 464, 13 );
			this.gbGraphOptions.Name = "gbGraphOptions";
			this.gbGraphOptions.Size = new System.Drawing.Size( 108, 100 );
			this.gbGraphOptions.TabIndex = 5;
			this.gbGraphOptions.TabStop = false;
			this.gbGraphOptions.Text = "Graph Options";
			// 
			// rbEfficiency
			// 
			this.rbEfficiency.AutoSize = true;
			this.rbEfficiency.Checked = true;
			this.rbEfficiency.Location = new System.Drawing.Point( 17, 52 );
			this.rbEfficiency.Name = "rbEfficiency";
			this.rbEfficiency.Size = new System.Drawing.Size( 71, 17 );
			this.rbEfficiency.TabIndex = 1;
			this.rbEfficiency.TabStop = true;
			this.rbEfficiency.Text = "Efficiency";
			this.rbEfficiency.UseVisualStyleBackColor = true;
			// 
			// rbSpeedup
			// 
			this.rbSpeedup.AutoSize = true;
			this.rbSpeedup.Location = new System.Drawing.Point( 17, 29 );
			this.rbSpeedup.Name = "rbSpeedup";
			this.rbSpeedup.Size = new System.Drawing.Size( 68, 17 );
			this.rbSpeedup.TabIndex = 0;
			this.rbSpeedup.Text = "Speedup";
			this.rbSpeedup.UseVisualStyleBackColor = true;
			// 
			// Form1
			// 
			this.AutoScaleDimensions = new System.Drawing.SizeF( 6F, 13F );
			this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
			this.ClientSize = new System.Drawing.Size( 584, 562 );
			this.Controls.Add( this.gbGraphOptions );
			this.Controls.Add( this.groupBox1 );
			this.Controls.Add( this.cbOneWriter );
			this.Controls.Add( this.label2 );
			this.Controls.Add( this.tbAccuracy );
			this.Controls.Add( this.pb );
			this.Controls.Add( this.btnTest );
			this.Controls.Add( this.zg );
			this.Name = "Form1";
			this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
			this.Text = "False Sharing Demo";
			this.groupBox1.ResumeLayout( false );
			this.groupBox1.PerformLayout();
			this.gbGraphOptions.ResumeLayout( false );
			this.gbGraphOptions.PerformLayout();
			this.ResumeLayout( false );
			this.PerformLayout();

		}

		#endregion

		private ZedGraph.ZedGraphControl zg;
		private System.Windows.Forms.Button btnTest;
		private System.Windows.Forms.ProgressBar pb;
		private System.Windows.Forms.TrackBar tbAccuracy;
		private System.Windows.Forms.Label label2;
		private System.Windows.Forms.CheckBox cbOneWriter;
		private System.Windows.Forms.GroupBox groupBox1;
		private System.Windows.Forms.Label label4;
		private System.Windows.Forms.Label label3;
		private System.Windows.Forms.GroupBox gbGraphOptions;
		private System.Windows.Forms.RadioButton rbEfficiency;
		private System.Windows.Forms.RadioButton rbSpeedup;
	}
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
United Kingdom United Kingdom
I discovered C# and .NET 1.0 Beta 1 in late 2000 and loved them immediately.
I have been writing software professionally in C# ever since

In real life, I have spent 3 years travelling abroad,
I have held a UK Private Pilots Licence for 20 years,
and I am a PADI Divemaster.

I now live near idyllic Bournemouth in England.

I can work 'virtually' anywhere!

Comments and Discussions