Click here to Skip to main content
15,895,142 members
Articles / Programming Languages / C#

Synchronize access to stream data section by section

Rate me:
Please Sign up or sign in to vote.
3.93/5 (9 votes)
23 Feb 2004CPOL4 min read 47.7K   490   11  
An article about locking sections of a stream for reading and writing.
using System;
using System.Collections;
using System.Threading;
using System.IO;


	/// <summary>
	/// Summary description for Class1.
	/// </summary>
	class Class1
	{
		static Byte[] bytes = new Byte[30];
		static ByteStream stream = new ByteStream(new Byte[30]);
		static FileStream file = new FileStream("StreamTest.txt", FileMode.Open, FileAccess.ReadWrite, FileShare.None);
		static MemoryMappedFile memFile = new MemoryMappedFile(file);

		/*
		static void ThreadProc1() {
			Console.Out.WriteLine("Entering thread {0}", Thread.CurrentThread.GetHashCode());
			SectionLock section = new SectionLock(bytes, 0, 14);
			using(section) {
				lock(section.SyncRoot) {
					Console.Out.WriteLine("Stream locked in thread {0}", Thread.CurrentThread.GetHashCode());
					for(int i=0; i<=14; i++) { 
						bytes[i] = 1;
						Thread.Sleep(10); // yield the thread
					}
				}
			}
		}

		static void ThreadProc2() {
			Console.Out.WriteLine("Entering thread {0}", Thread.CurrentThread.GetHashCode());
			SectionLock section = new SectionLock(bytes, 10, 24);
			using(section) {
				lock(section.SyncRoot) {
					Console.Out.WriteLine("Stream locked in thread {0}", Thread.CurrentThread.GetHashCode());
					for(int i=10; i<=24; i++) { 
						bytes[i] = 2;
						Thread.Sleep(10); // yield the thread
					}
				}
			}
		}
		static void ThreadProc3() {
			Console.Out.WriteLine("Entering thread {0}", Thread.CurrentThread.GetHashCode());
			SectionLock section = new SectionLock(bytes, 15, 29);
			using(section) {
				lock(section.SyncRoot) {
					Console.Out.WriteLine("Stream locked in thread {0}", Thread.CurrentThread.GetHashCode());
					for(int i=15; i<=29; i++) { 
						bytes[i] = 3;
						Thread.Sleep(10); // yield the thread
					}
				}
			}
		}
		*/

		/*
		static void ThreadProc1() {
			Console.Out.WriteLine("Entering thread {0}", Thread.CurrentThread.GetHashCode());
			Byte[] bytes = new Byte[15];
			for(int i=0; i<bytes.Length; i++)
				bytes[i] = 1;

			stream.Write(0, bytes, 0, bytes.Length);
		}
		static void ThreadProc2() {
			Console.Out.WriteLine("Entering thread {0}", Thread.CurrentThread.GetHashCode());
			Byte[] bytes = new Byte[15];
			for(int i=0; i<bytes.Length; i++)
				bytes[i] = 2;

			stream.Write(10, bytes, 0, bytes.Length);
		}
		static void ThreadProc3() {
			Console.Out.WriteLine("Entering thread {0}", Thread.CurrentThread.GetHashCode());
			Byte[] bytes = new Byte[15];
			for(int i=0; i<bytes.Length; i++)
				bytes[i] = 3;

			stream.Write(15, bytes, 0, bytes.Length);
		}
		*/

		static void ThreadProc1() {
			Console.Out.WriteLine("Entering thread {0}", Thread.CurrentThread.GetHashCode());
			Byte[] bytes = new Byte[15];
			for(int i=0; i<bytes.Length; i++)
				bytes[i] = 0x31;

			memFile.Write(0, bytes, 0, bytes.Length);
		}
		static void ThreadProc2() {
			Console.Out.WriteLine("Entering thread {0}", Thread.CurrentThread.GetHashCode());
			Byte[] bytes = new Byte[15];
			for(int i=0; i<bytes.Length; i++)
				bytes[i] = 0x32;

			memFile.Write(10, bytes, 0, bytes.Length);
		}
		static void ThreadProc3() {
			Console.Out.WriteLine("Entering thread {0}", Thread.CurrentThread.GetHashCode());
			Byte[] bytes = new Byte[15];
			for(int i=0; i<bytes.Length; i++)
				bytes[i] = 0x33;

			memFile.Write(30, bytes, 0, bytes.Length);
		}

		/// <summary>
		/// The main entry point for the application.
		/// </summary>
		[STAThread]
		static void Main(string[] args)
		{
			Thread thread1 = new Thread(new ThreadStart(ThreadProc1));
			Thread thread2 = new Thread(new ThreadStart(ThreadProc2));
			Thread thread3 = new Thread(new ThreadStart(ThreadProc3));

			thread1.Start();
			thread2.Start();
			thread3.Start();

			thread1.Join();
			thread2.Join();
			thread3.Join();

			bytes = stream.Buffer;
			for(int i=0; i<bytes.Length; i++) 
				Console.Out.WriteLine("{0} : {1}", i, bytes[i] );

			memFile.Dispose();
			file.Close();
		}
	}

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
Web Developer
United States United States
I am a consultant, trainer, software archtect/engineer, since the early 1980s, working in the greater area of Boston, MA, USA.

My work comprises the entire spectrum of software, shrink-wrapped applications, IT client-server, systems and protocol related work, compilers and operating systems, and more ....

I am currently focused on platform development for distributed computing in service oriented data centers.

Comments and Discussions