简体   繁体   English

在C ++中使用WinApi进行多线程

[英]Multi-threading with WinApi in C++

I've been struggling with multi-threading for a while now...managed to work it out last night on my current program, what I wanted is to have a simple timer running while I'm still able to work with my application. 我已经在多线程上苦苦挣扎了一段时间……设法在昨晚在当前程序上解决这个问题,我想要的是在我仍然可以使用我的应用程序的同时运行一个简单的计时器。

I used msdn reference . 我用msdn 参考

I managed to make it work fine ( or at least I think it works fine ) with this code: 我设法通过以下代码使其正常工作(或者至少我认为它可以正常工作):

case ID_MENUBUTTON0:

        hRunMutex = CreateMutex(NULL, TRUE, NULL);
        _beginthread(Sistem::timeFrame, 0, NULL);
        break;

case ID_MENUBUTTON4:
        ReleaseMutex(hRunMutex);
        PostQuitMessage(0);
        break;

Sistem::timeFrame func calculates time with intervals of 100ms Sleep(100); Sistem :: timeFrame函数以100ms的间隔计算时间Sleep(100); and outputs it in a static window and updates the window every 100ms. 并将其输出到静态窗口中,并每100毫秒更新一次窗口。

Now what I wanted to ask since I didn't really understand all the fuss about this is why is there so many extra stuff ? 现在,我想问的是,因为我不太了解所有这些大惊小怪的原因,为什么会有这么多额外的东西? Such as WaitForSingleObject , I've seen a lot of it but can't figure out it's practical use. 例如WaitForSingleObject ,我已经看了很多,但无法弄清楚它的实际用途。 Is there something I should watch out for except for ReleaseMutex() ? 除了ReleaseMutex()外,我还有什么需要注意的吗?

WaitForSingleObject and WaitForMultipleObjects are used to make a thread wait until another thread has done something. WaitForSingleObject和WaitForMultipleObjects用于使一个线程等待,直到另一个线程完成了某件事。 The practical use is coordination, like don't land the plane until the wheels are down. 实际用途是协调,例如在轮子放下之前不要着陆。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM