cpptime icon indicating copy to clipboard operation
cpptime copied to clipboard

mutithread erro coredump

Open jiudianren opened this issue 1 year ago • 1 comments

qestion1 :

thread1 add a period timer and save time_id. thread2 is the timer thread.

afer thread2 unlock . thread 1 use the time_id to call Timer::remove() , in Timer::remove , delete the time_id and set handler to nullptr, but thread 1 is executing the handler. the coredump happen.

lock.unlock();   
events[te.ref].handler(te.ref);
lock.lock();

what should i do to avoid the problem?

question 2: thread1 add a period timer and save time_id. thread2 is the timer thread.

thread 1 will call Timer::remove() in handler callback. while the handler is executing ,it call Timer::remove, But in Timer::remove set handler to nullptr . and so the program core dump.

	events[id].valid = false;
	events[id].handler = nullptr;

jiudianren avatar Feb 23 '24 10:02 jiudianren

One way is to execute remove tid in the handler.

jiudianren avatar Feb 26 '24 01:02 jiudianren