win32中的线程的正确同步方法是什么 [英] What is the correct synchronization method for the thread in win32

查看:97
本文介绍了win32中的线程的正确同步方法是什么的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已为线程创建以下内容

I have created following things for thread

int Data_Of_Thread_1 = 1;
int Data_Of_Thread_2 = 2;
Handle_Of_Thread_1 = 0;
Handle_Of_Thread_2 = 0;
HANDLE Array_Of_Thread_Handles[2];

Handle_Of_Thread_1 = CreateThread( NULL, 0,ModbusRead, &Data_Of_Thread_1, 0, NULL);  

Handle_Of_Thread_2 = CreateThread( NULL, 0,ModbusWrite, &Data_Of_Thread_2, 0, NULL);  

现在我必须控制这两个线程的执行。条件如下:

Now i have to control the execution of these two threads. The condition is as follows:

function ModbusWrite
{
 if (condition1 true)
   {
     Pause thread1

     if(condition2 true)
           {
               resume thread1
           }
     }
 }

我浏览了这些网站。他们说同步元素作为事件,互斥,信号量。等等。我想我必须使用事件或互斥。但我不太清楚如何使用它们。首先我们创建创建事件或创建互斥,然后如何应用这些事件或互斥在我的上述条件。我也不清楚WaitForSingleObject函数。在哪里和如何实现。如果任何人可以帮助我的代码,那么它会感激。

I have gone through the sites. they say synchronisation element as event, mutex, semaphore. etc. I think i have to use either event or mutex. But i am not quite clear about how to use them both. First we create either create event or create mutex then how to apply those event or mutex in my above condition. Also i am not clear about "WaitForSingleObject" function. where and how to implement . If anyone can help me with the code then it would be grateful.

推荐答案

在Windows上,通常使用事件对象等待条件不浪费CPU。如果你正在连接的外部软件提供某种异步回调机制,那么你想做这样的事情:

On Windows, one typically uses event objects to wait for conditions without wasting CPU. If the external software you're interfacing with provides some sort of asynchronous callback mechanism, then you'd want to do something like this:

// Create an anonymous auto-reset event, initial state unsignaled
HANDLE hEvent = CreateEvent(NULL, FALSE, FALSE, NULL);
...

void ThreadProcedure()
{
    while (threadShouldContinueRunning())
    {
        // Wait until event is signaled
        WaitForSingleObject(hEvent, INFINITE);

        // Now the thread has woken up, check the condition and respond
        // accordingly
    }
}
...

void OnExternalCallback()
{
    // Called from external library when the condition becomes true -- signal
    // the worker thread to resume
    SetEvent(hEvent);
}
...

// Don't forget to cleanup
CloseHandle(hEvent);

现在,如果外部库没有 提供任何类型的回调机制以便在条件成真时通知您,您遇到麻烦。在这种情况下,检测条件为真的唯一方法是连续轮询它,可选地在它们之间休眠以避免烧毁CPU时间。当然,这样做的主要缺点是,在检测条件变化(延迟量是休眠时间)时引入了不必要的延迟,或者浪费了大量CPU(因此浪费了电源/电池寿命)。

Now, if the external library does not provide any sort of callback mechanism to inform you when the condition becomes true, you're in trouble. In that case, the only way to detect when the condition becomes true is to continuously poll it, optionally sleeping in between to avoid burning CPU time. The major downside of this, of course, is that you introduce unnecessary latency in detecting the condition change (the latency amount is the sleep time), or you waste a lot of CPU (and therefore power/battery life) spinning.

void ThreadProcedure()
{
    while (threadShouldContinueRunning())
    {
        // Avoid polling if at all possible -- this adds latency and/or wastes
        // CPU and power/battery life
        if (externalConditionIsTrue())
        {
            // Handle
        }
        else
        {
            Sleep(50);  // Tune this number to balance latency vs. CPU load
        }
    }
}

这篇关于win32中的线程的正确同步方法是什么的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆