Skip to content

Conversation

@HanetakaChou
Copy link

@HanetakaChou HanetakaChou commented Mar 16, 2025

when another thread resizes the table, it will perform the following two steps:

  • ANOTHER-1. allocate the new meomry block
  • ANOTHER-2. copy the existing data from the old memory block to the new meomry block

there can be such timeline:

  • ANOTHER-1. another thread has allocated the new meomry block
  • CURRENT. current thread is reading the uninitialized data from the new memory block
  • ANOTHER-2. another thread will copy the existing data from the old memory block to the new meomry block, but this will not affect the uninitialized data read by the current thread
		int uniqueId = body.getWorldArrayIndex();
		const int INVALID_SOLVER_BODY_ID = -1;
		m_kinematicBodyUniqueIdToSolverBodyTableMutex.lock();
		if (m_kinematicBodyUniqueIdToSolverBodyTable.size() <= uniqueId)
		{
			// ~~m_kinematicBodyUniqueIdToSolverBodyTableMutex.lock();~~
			// now that we have the lock, check again
			if (m_kinematicBodyUniqueIdToSolverBodyTable.size() <= uniqueId)
			{
				m_kinematicBodyUniqueIdToSolverBodyTable.resize(uniqueId + 1, INVALID_SOLVER_BODY_ID);
			}
			// ~~m_kinematicBodyUniqueIdToSolverBodyTableMutex.unlock();~~
		}
                // ** the following read may have **uninitialized** data without lock ** !!!
		solverBodyId = m_kinematicBodyUniqueIdToSolverBodyTable[uniqueId];
		m_kinematicBodyUniqueIdToSolverBodyTableMutex.unlock();

@HanetakaChou
Copy link
Author

@erwincoumans

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant