Critical Section非托管C ++无法正常工作 [英] Critical Section unmanaged C++ not working
问题描述
我在C ++ CLR程序中有一些代码,其中包含以下Critical Section代码。
I have some code in a C++ CLR program with the below Critical Section code.
<pre lang="xml">
///Test.cpp
static CRITICAL_SECTION PrintCriticalSection;
void Test(int sleep1,int threadNumber)
{
cout<<"Thread "<<threadNumber<<" Starts. Sleeping for "<<sleep1<<" seconds.\n";
EnterCriticalSection(&PrintCriticalSection);
Sleep(sleep1);
LeaveCriticalSection(&PrintCriticalSection);
cout<<"Thread "<<threadNumber<<" Ends.\n";
}</pre>
我使用2个主题调用此代码
I am calling this code using 2 Threads
<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>
控制台窗口中的输出应为:
线程2开始。睡觉100秒。
线程1开始。睡了10000秒。
///这里应该暂停10,000秒
线程2结束。
线程1结束。
但我得到的输出是随机的。有时我会得到上述内容,但有时我会得到以下输出。太奇怪了。为什么会这样?我可以用什么来解决这个问题呢?救命!!!!!它像六分之一,我得到低于输出。我很高兴我抓住了这个。我还注意到,如果我将变量设置为global而不是static,我每次都会得到以下值(Critical Section不起作用)
控制台窗口中的输出有时候:
线程2开始。睡觉100秒。
线程1开始。睡觉10000秒。
线程2结束。
///这里现在发生10秒暂停...
线程1结束。
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.
推荐答案
这正是预期的结果。
你滥用CriticalSection
以一种我从未想象过的有趣方式。你用它保护什么?致电睡眠
!
结案时间。
我无法想象你想到的方式。除非这只是一手牌,否则我会说你不知道CriticalSection
用于什么。试着理解它。为此,请阅读: http://en.wikipedia.org/wiki/Mutual_exclusion [ ^ ]。
更好现在? :-)
-SA
This is exactly the expected result.
You misuseCriticalSection
in such a funny way which I never even imagined. What do you "protect" with it? The call toSleep
!
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 whatCriticalSection
is used for. Try to understand it. For this purpose, read this: http://en.wikipedia.org/wiki/Mutual_exclusion[^].
Better now? :-)
—SA
通过将关键部分移动到测试代码顶部来修复
并添加
Fixed by moving critical section to top of the test code
and adding
thread1.Start();
Thread.Sleep(1000);
thread2.Start();
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);
}
}
}
这篇关于Critical Section非托管C ++无法正常工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!