gpt4 book ai didi

winapi - 在线程之间共享对象

转载 作者:行者123 更新时间:2023-12-03 12:53:11 25 4
gpt4 key购买 nike

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

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_);
}
}

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

最佳答案

两个建议:

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

    在我的示例中,我将使用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/

    25 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com