Is a bad practice to declare static variables into functions/member functions?

前端 未结 4 1056
小蘑菇
小蘑菇 2021-01-01 20:18

Recently a fellow worker showed to me a code like this:

void SomeClass::function()
{
    static bool init = false;

    if (!init)
    {
        // hundreds          


        
4条回答
  •  遥遥无期
    2021-01-01 21:04

    Think multi-threading. This type of code is problematic when function() can be called concurrently by multiple threads. Without locking, you're open to race conditions; with locking, concurrency can suffer for no real gain.

提交回复
热议问题