winapi - 在线程之间共享对象

标签 winapi multithreading

如何设置线程之间共享的对象数据,并且需要在繁忙循环中的(例如)两个线程完成循环后更新一次?

CRITICAL_SECTION critical_section_;

int value; //needs to be updated once after the cycle of any number of threads running in busy loop

void ThreadsFunction(int i)
{

 while (true)
 {
  EnterCriticalSection(&critical_section_);
                /* Lines of Code */
  LeaveCriticalSection(&critical_section_);
 }
}

编辑:可以是任何类的对象。

最佳答案

两个建议:

  • 使对象本身线程安全。
  • 将对象作为实例数据传递到线程中

我将在我的示例中使用 C++ 作为引用。如果需要,您可以轻松地将其转换为纯 C。

//MyObject是线程间要共享的核心数据

struct MyObject
{
   int value;
   int othervalue;
   // all all the other members you want here
};


class MyThreadSafeObject
{
private:
    CRITICAL_SECTION _cs;
    MyObject _myojbect;
    bool _fLocked;
public:
    MyThreadSafeObject()
    {
        _fLocked = false
        InitializeCriticalSection();
    }
    ~MYThreadSafeObject()
    {
        DeleteCriticalSection();
    }

    // add "getter and setter" methods for each member in MyObject
    int SetValue(int x)
    {
         EnterCriticalSection(&_cs);
             _myobject.value = x;
         LeaveCriticalSection(&_cs);
    }

    int GetValue()
    {
         int x;
         EnterCriticalSection(&_cs);
             x = _myobject.value;
         LeaveCriticalSection(&_cs);
         return x;
    }

    // add "getter and setter" methods for each member in MyObject
    int SetOtherValue(int x)
    {
         EnterCriticalSection(&_cs);
             _myobject.othervalue = x;
         LeaveCriticalSection(&_cs);
    }

    int GetOtherValue()
    {
         int x;
         EnterCriticalSection(&_cs);
             x = _myobject.othervalue;
         LeaveCriticalSection(&_cs);
         return x;
    }


    // and if you need to access the whole object directly without using a critsec lock on each variable access, add lock/unlock methods
    bool Lock(MyObject** ppObject)
    {
        EnterCriticalSection(&_cs);
        *ppObject = &_myobject;
        _fLocked = true;
        return true;                
    }

    bool UnLock()
    {
        if (_fLocked == false)
            return false;

        _fLocked = false;
        LeaveCriticalSection();
        return true;
    }
};

然后,按如下方式创建对象和线程:

MyThreadSafeObject* pObjectThreadSafe;
MyObject* pObject = NULL;

// now initilaize your object
pObjectThreadSafe->Lock(&pObject);
   pObject->value = 0; // initailze value and all the other members of pObject to what you want them to be.
   pObject->othervalue = 0;
pObjectThreadSafe->Unlock();
pObject = NULL;


// Create your threads, passing the pointer to MyThreadSafeObject as your instance data
DWORD dwThreadID = 0;
HANDLE hThread = CreateThread(NULL, NULL, ThreadRoutine, pObjectThreadSafe, 0, &dwThreadID);


And your thread will operate as follows
DWORD __stdcall ThreadFunction(void* pData)
{
    MyThreadSafeObject* pObjectThreadSafe = (MyThreadSafeObject*)pData;
    MyObject* pObject = NULL;

    while (true)
    {
       /* lines of code */
           pObjectThreadSafe->SetValue(x);
       /* lines of code */         
    }

}

关于winapi - 在线程之间共享对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2983540/

相关文章:

multithreading - 多线程修改集合

java - 多线程 Android 应用程序中的组件应该如何交互?

c# - 如何在.NET 中调试跨线程异常?

c# - SetWindowsHookEx 在 C# 中似乎对我不起作用(WH_KEYBOARD_LL,全局)

两个线程可以使用相同的线程过程吗?

windows - 是什么导致 WriteFile 返回错误 38 (ERROR_HANDLE_EOF)?

java - 如何销毁一个线程?

c# - 如果窗口设置为 "Automatic Login",则 GetLastInputInfo API 不起作用

windows - 这些奇怪的环境变量是什么?

java - 如何编写从tomcat运行的java批处理