dimanche 22 juillet 2018

Replacing `_InterlockedOr` with modern standard C++

I'm trying to convert some legacy code to standard C++ and I'm having some trouble converting _InterlockedOr to std::atomic_bool (which I think is a fair equivalent):

LONG someCondition = false;
SThread_Data threadsData[COUNT];
for(int i = 0; i < COUNT; ++i)
{
    threadsData[i].pSomeCondition = &someCondition;
    AfxBeginThread(checkCondition, &threadsData[i]);
}

UINT checkCondition(LPVOID threadData)
{
    LONG someCondition = false;
    //Update someCondition to correct value
   _InterlockedOr(((SThread_Data*)threadData)->pSomeCondition, someCondition);
    return 1;
}

I don't see a reason for using LONG for a boolean, and I'm not even sure about the conversion from false to LONG so I thought about using bool for the condition. And since multiple threads calculate some condition, I thought that std::atomic_bool is the way to go. But I can't seem to get my head around it since I can't use std::atomic::fetch_or for std::atomic_bool.

Is there some way to convert this using bool and atomic or do I have to introduce a mutex?

Aucun commentaire:

Enregistrer un commentaire