Opened 8 years ago

Last modified 8 years ago

#6223 closed defect

CPLGenerateTempFilename volatite and/or CPLAtomicInc — at Version 2

Reported by: Kurt Schwehr Owned by: Kurt Schwehr
Priority: normal Milestone:
Component: default Version: svn-trunk
Severity: normal Keywords: multithreading
Cc:

Description (last modified by Even Rouault)

In r15574, you put a volatile modifier on nTempFileCounter. This looks to be protect from multithreaded race cases between callers of CPLGenerateTempFilename where more than one thread might get the same tempfile number. Would it be better to call CPLAtomicInc and use the result of that? If so, should I drop the volatile?

e.g.

    static volatile int nTempFileCounter = 0;
    CPLString osFilename;
    osFilename.Printf( "%s%u_%d", pszStem,
                       (unsigned int)(CPLGetPID() & 0xFFFFFFFFU), nTempFileCounter++ );

Would become this.

    static int nTempFileCounter = 0;
    CPLString osFilename;
    osFilename.Printf( "%s%u_%d", pszStem,
                       static_cast<unsigned int>( CPLGetPID() & 0xFFFFFFFFU),
                       CPLAtomicInc(nTempFileCounter) );

A second question on this code:

Why the mask on the CPLGetPID()?

Change History (2)

comment:1 by Kurt Schwehr, 8 years ago

Description: modified (diff)
Status: newassigned

comment:2 by Even Rouault, 8 years ago

Description: modified (diff)

+1 for CPLAtomicInc (but use &nTempFileCounter as argument)

I think the mask was when using with some sanitzation runtime because CPLGetPID() could return a value larger than a unsigned int. But A better fix would be to replace %u with CPL_FRMT_GIB and dropping the cast

Note: See TracTickets for help on using tickets.