You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@nifi.apache.org by "Ferenc Gerlits (Jira)" <ji...@apache.org> on 2022/02/14 08:22:00 UTC

[jira] [Updated] (MINIFICPP-1753) Remove the possibility of multiple initialization from controller services

     [ https://issues.apache.org/jira/browse/MINIFICPP-1753?page=com.atlassian.jira.plugin.system.issuetabpanels:all-tabpanel ]

Ferenc Gerlits updated MINIFICPP-1753:
--------------------------------------
    Labels: MiNiFi-CPP-Hygiene  (was: )

> Remove the possibility of multiple initialization from controller services
> --------------------------------------------------------------------------
>
>                 Key: MINIFICPP-1753
>                 URL: https://issues.apache.org/jira/browse/MINIFICPP-1753
>             Project: Apache NiFi MiNiFi C++
>          Issue Type: Improvement
>            Reporter: Ferenc Gerlits
>            Priority: Minor
>              Labels: MiNiFi-CPP-Hygiene
>
> Quite a few controller services* use the pattern
> {noformat}
> void SomeService::initialize() {
>   if (initialized_)
>     return;
>   std::lock_guard<std::mutex> lock(initialization_mutex_);
>   ControllerService::initialize();
>   initializeProperties();
>   initialized_ = true;
> }
> {noformat}
> with an atomic {{initialized_}} variable.  This is probably intended as an optimization to avoid locking the mutex, but that doesn't save much time, and it can lead to the initialization being run multiple times.
> Put the {{if (initialized_)}} check behind the lock, and change the type of {{initialized_}} to a plain {{{}bool{}}}.  This will make the code simpler and it will make multiple initialization impossible, at the cost of making {{initialize()}} run slightly slower when called a second or third time.
> [*] CoapConnector, JavaControllerService, MQTTControllerService, DatabaseService and SSLContextService do this for sure; please check if there are any others



--
This message was sent by Atlassian Jira
(v8.20.1#820001)