Click here to Skip to main content
15,895,084 members
Please Sign up or sign in to vote.
3.00/5 (2 votes)
See more:
I have some code in a C++ CLR program with the below Critical Section code.
C#
<pre lang="xml">
///Test.cpp
static CRITICAL_SECTION	PrintCriticalSection;
void Test(int sleep1,int threadNumber)
    {
        cout&lt;&lt;&quot;Thread &quot;&lt;&lt;threadNumber&lt;&lt;&quot; Starts. Sleeping for &quot;&lt;&lt;sleep1&lt;&lt;&quot; seconds.\n&quot;;
        EnterCriticalSection(&amp;PrintCriticalSection);
        Sleep(sleep1);
        LeaveCriticalSection(&amp;PrintCriticalSection);
        cout&lt;&lt;&quot;Thread &quot;&lt;&lt;threadNumber&lt;&lt;&quot; Ends.\n&quot;;

    }</pre>


I am calling this code using 2 Threads
C#
<pre lang="xml">
class Program
    {
         Thread thread1;
         Thread thread2;
         PhoneCLR.PhonePortCLR test1;
         PhoneCLR.PhonePortCLR test2;
        static void Main(string[] args)
        {
            Program program1=new Program();
            program1.Test1();
             }

        void Test1()
        {
            test1 = new PhonePortCLR(1, 1, 1, 1, true, false, true, false, 1);
            test2 = new PhonePortCLR(1, 1, 1, 1, true, false, true, false, 1);
            PhonePortCLR.InitGlobalOnce(1, false, false, false, false);
            thread1 = new Thread(new ThreadStart(function1));
            thread2 = new Thread(new ThreadStart(function2));
            thread1.Start();
            thread2.Start();
           
            Console.ReadLine();
        }
       
         void function1()
        {
            test1.Test(10000, 1);
        }
         void function2()
        {
            test2.Test(100,2);
        }
    }
}
</pre>


The output in console window is supposed to be :

Thread 2 Starts. Sleeping for 100 seconds.
Thread 1 Starts. Sleeping for 10000 seconds.
///There is supposed to be a pause of 10,000 seconds here
Thread 2 Ends.
Thread 1 Ends.

But the output I am getting is random. Sometimes I get the above but sometimes I get below Output. It is so weird. Why is this the case? What can I use instead to fix this issue? Help!!!!! Its like 1 in 6 times I get below output. I am glad I caught this. I also notice that if I make the variable global instead of static, I get the below every time( Critical Section does not work)

The output in console window is sometimes:

Thread 2 Starts. Sleeping for 100 seconds.
Thread 1 Starts. Sleeping for 10000 seconds.
Thread 2 Ends.
///The 10 second pause happens now here...
Thread 1 Ends.
Posted
Updated 22-Jan-13 17:50pm
v2
Comments
Sergey Alexandrovich Kryukov 23-Jan-13 0:09am    
Please let me inform you that you've committed a serious abuse. Each and every of your "answers" in this forum is an abuse of different degree. All such "answers" are fake. Most of them are other questions. Worse, some of them you accepted by yourself, which can be qualified as cheating.

Is there anything you are going to do with that?
—SA
[no name] 23-Jan-13 0:44am    
I am not sure what you mean . You are talking about this thread?

This is exactly the expected result.

You misuse CriticalSection in such a funny way which I never even imagined. What do you "protect" with it? The call to Sleep!

Case closed.

I cannot imagine the way you could possibly think. Unless this is a slip of a hand, I would say you have no idea what CriticalSection is used for. Try to understand it. For this purpose, read this: http://en.wikipedia.org/wiki/Mutual_exclusion[^].

Better now? :-)

—SA
 
Share this answer
 
v3
Comments
[no name] 23-Jan-13 0:01am    
This is merely test code i wrote to prove that Thread 2 is able to get past Thread1. I want Thread 2 to not execute when it is entering Critical sections. I guess you have to follow the examples a little bit more. Does that make it more clear?
Sergey Alexandrovich Kryukov 23-Jan-13 0:05am    
Please tell me: do you sandwich Speep with critical section calls or not? I can see it right now.
—SA
[no name] 23-Jan-13 0:09am    
Yes I put a sleep there depending on the thread. The sleep is there to identify that Thread 2 is not able to get past the sleep time of Thread 1 and gives me visual confirmation.
Sergey Alexandrovich Kryukov 23-Jan-13 0:13am    
OK, that settles the case. I understand the possible purpose of sleep, but your critical section does nothing. Look, I'm too lazy to describe the scenario showing the output you got. You can do it by yourself. In other words, you don't do any synchronization, substitute it with so called "race condition" (read about it) instead.
—SA
[no name] 23-Jan-13 0:21am    
I think I found out why...See my comments. Is that what you mean?
Yes the sleep does nothing other than let me count that 10 seconds have elapsed and thread 2 is not outputting anything on the screen.
Fixed by moving critical section to top of the test code
and adding
C#
thread1.Start();
Thread.Sleep(1000);
thread2.Start();


C#
static CRITICAL_SECTION	PrintCriticalSection;
void Test(int sleep1,int threadNumber)
    {
         EnterCriticalSection(&PrintCriticalSection);
        cout<<"Thread "<<threadNumber<<" Starts. Sleeping for "<<sleep1<<" seconds.\n";
       
        Sleep(sleep1);
        
        cout<<"Thread "<<threadNumber<<" Ends.\n";
        LeaveCriticalSection(&PrintCriticalSection);
    }
 


class Program
    {
         Thread thread1;
         Thread thread2;
         PhoneCLR.PhonePortCLR test1;
         PhoneCLR.PhonePortCLR test2;
        static void Main(string[] args)
        {
            Program program1=new Program();
            program1.Test1();
             }
 
        void Test1()
        {
            test1 = new PhonePortCLR(1, 1, 1, 1, true, false, true, false, 1);
            test2 = new PhonePortCLR(1, 1, 1, 1, true, false, true, false, 1);
            PhonePortCLR.InitGlobalOnce(1, false, false, false, false);
            thread1 = new Thread(new ThreadStart(function1));
            thread2 = new Thread(new ThreadStart(function2));
            thread1.Start();
            Thread.Sleep(1000);
            thread2.Start();
           
            Console.ReadLine();
        }
       
         void function1()
        {
            test1.Test(10000, 1);
        }
         void function2()
        {
            test2.Test(100,2);
        }
    }
}
 
Share this answer
 

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

  Print Answers RSS
Top Experts
Last 24hrsThis month


CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900