Click here to Skip to main content
15,920,896 members
Home / Discussions / C#
   

C#

 
QuestionInstalling application using C# Pin
Samar Aarkotti30-Aug-05 5:53
Samar Aarkotti30-Aug-05 5:53 
AnswerRe: Installing application using C# Pin
Guinness4Strength30-Aug-05 9:31
Guinness4Strength30-Aug-05 9:31 
GeneralRe: Installing application using C# Pin
Anonymous30-Aug-05 12:19
Anonymous30-Aug-05 12:19 
GeneralRe: Installing application using C# Pin
Guinness4Strength31-Aug-05 3:54
Guinness4Strength31-Aug-05 3:54 
QuestionQuestion about Mutex Pin
Alex Cutovoi30-Aug-05 5:17
Alex Cutovoi30-Aug-05 5:17 
AnswerRe: Question about Mutex Pin
Andy Brummer30-Aug-05 8:15
sitebuilderAndy Brummer30-Aug-05 8:15 
GeneralRe: Question about Mutex Pin
Alex Cutovoi30-Aug-05 9:25
Alex Cutovoi30-Aug-05 9:25 
GeneralRe: Question about Mutex Pin
Andy Brummer30-Aug-05 10:30
sitebuilderAndy Brummer30-Aug-05 10:30 
I think this might make things clearer.
Your code works because C# is smart enough to release the mutex when the thread ends allowing the other thread to continue, but this is a really bad habit to get into since it can lead to deadlocks. Every call to WaitOne should be paired with a call to ReleaseMutex, and this includes the mutex creation because in some processes the mutex static constructor could be called on a separeate thread from main.

ReWrite your sample methods so they look like this, then try it with the Release line commented out and in place.
		static void Main(string[] args)
		{
			sMutex = new System.Threading.Mutex(true);
			thread1 = new Thread(new ThreadStart(t1));
			thread2 = new Thread(new ThreadStart(t2));
			thread1.Start();
			thread2.Start();
			sMutex.ReleaseMutex();

			thread1.Join();
			thread2.Join();
		}

		private static System.Threading.Thread thread1;
		private static System.Threading.Thread thread2;
		private static System.Threading.Mutex sMutex;

		static void t1()
		{
			for(int i=0; i<10; ++i)
			{
				sMutex.WaitOne();
				Console.WriteLine("done1");
//				sMutex.ReleaseMutex();
				Thread.Sleep(0);
			}
		}

		static void t2()
		{
			for(int i=0; i<10; ++i)
			{
				sMutex.WaitOne();
				Console.WriteLine("done2");
//				sMutex.ReleaseMutex();
				Thread.Sleep(0);
			}
		}


One way you will get alternating done1 and done2 lines and the other you will get 10 donex followed by 10 doney, where y or x are 1 or 2.


I can imagine the sinking feeling one would have after ordering my book,
only to find a laughably ridiculous theory with demented logic once the book arrives - Mark McCutcheon

GeneralRe: Question about Mutex Pin
Alex Cutovoi30-Aug-05 14:17
Alex Cutovoi30-Aug-05 14:17 
GeneralRe: Question about Mutex Pin
Andy Brummer30-Aug-05 19:30
sitebuilderAndy Brummer30-Aug-05 19:30 
GeneralRe: Question about Mutex Pin
Alex Cutovoi31-Aug-05 2:53
Alex Cutovoi31-Aug-05 2:53 
GeneralRe: Question about Mutex Pin
Alex Cutovoi31-Aug-05 2:56
Alex Cutovoi31-Aug-05 2:56 
QuestionMDI Child with flicker Pin
enricrt30-Aug-05 5:15
enricrt30-Aug-05 5:15 
QuestionListView with column and images Pin
Sasuko30-Aug-05 4:36
Sasuko30-Aug-05 4:36 
AnswerRe: ListView with column and images Pin
Luis Alonso Ramos30-Aug-05 6:43
Luis Alonso Ramos30-Aug-05 6:43 
GeneralRe: ListView with column and images Pin
Sasuko30-Aug-05 7:56
Sasuko30-Aug-05 7:56 
GeneralRe: ListView with column and images Pin
Luis Alonso Ramos30-Aug-05 8:22
Luis Alonso Ramos30-Aug-05 8:22 
QuestionImage dislaying and processing in C# Pin
silka_c#30-Aug-05 4:21
silka_c#30-Aug-05 4:21 
AnswerRe: Image dislaying and processing in C# Pin
Andrew Kirillov30-Aug-05 4:59
Andrew Kirillov30-Aug-05 4:59 
QuestionRe: Image dislaying and processing in C# Pin
silka_c#31-Aug-05 0:50
silka_c#31-Aug-05 0:50 
Answer[Message Deleted] Pin
Judah Gabriel Himango30-Aug-05 5:01
sponsorJudah Gabriel Himango30-Aug-05 5:01 
AnswerRe: Image dislaying and processing in C# Pin
HumanOsc30-Aug-05 7:33
HumanOsc30-Aug-05 7:33 
QuestionJava Pin
VickyC#30-Aug-05 4:02
VickyC#30-Aug-05 4:02 
AnswerRe: Java Pin
Judah Gabriel Himango30-Aug-05 4:47
sponsorJudah Gabriel Himango30-Aug-05 4:47 
GeneralRe: Java Pin
VickyC#30-Aug-05 4:51
VickyC#30-Aug-05 4:51 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.