Opened 9 years ago

Last modified 9 years ago

#6223 closed defect

CPLGenerateTempFilename volatite and/or CPLAtomicInc — at Initial Version

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

Description

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 (0)

Note: See TracTickets for help on using tickets.