简体   繁体   English

在销毁对象之前对其进行互斥锁定会释放内存或其他意外情况

[英]Lock mutex of object before destroy it will deallocate memory or some other unexpected

Is it a good and safe practice to lock mutex of the c++ object before delete this object (without unlocking) ? delete该对象(不进行解锁)之前锁定c ++对象的互斥体是一种好的安全方法吗? I do this to protect possible using of object at this milliseconds from other thread (very low probability). 我这样做是为了保护在其他毫秒之内(从可能性极低的情况下)可以使用对象。

Will the memory unallocated after that? 此后是否会取消分配内存? Is it a good practice? 这是一个好习惯吗?

Example: 例:

    ptr_to_delete->MUTEX.lock_writing(); // can not delete session if its already locked. (additational protection)
    ptr_to_delete->cleanup();
    delete ptr_to_delete;

"I do this to protect possible using of object at this milliseconds from other thread". “我这样做是为了保护在其他毫秒之内可能无法使用其他线程的对象”。 In this instance, it doesn't really matter if the object's mutex is locked or not - if you've deleted the object in one thread and other threads still have a pointer to it which may be used, you're going to run into trouble. 在这种情况下,对象的互斥对象是否被锁定并不重要-如果您已在一个线程中删除了该对象,而其他线程仍然具有指向该对象的指针,则可能会遇到麻烦。

EDIT : 编辑

It's still problematic. 还是有问题的。 As piokuc said, you need to be releasing and freeing the mutex at some point, otherwise your program will be leaking. 正如piokuc所说,您需要在某个时候释放并释放互斥锁,否则您的程序将会泄漏。

There's still the situation where a worker thread and the cleaning thread can obtain a pointer to the object simultaneously. 仍然存在工作线程和清理线程可以同时获取指向该对象的指针的情况。 If the cleaning thread deletes the object before the worker thread uses it, you will get undefined behaviour (ie a crash). 如果清理线程在工作线程使用它之前删除了该对象,您将得到未定义的行为(即崩溃)。

You should create an accessor function for your array of pointers that only returns a pointer if it's not locked (and locks it before returning). 您应该为指针数组创建一个访问器函数,该函数仅在未锁定的情况下才返回指针(并在返回之前将其锁定)。 That way, only one thread at a time may have the pointer, and there is no chance of a worker thread obtaining/using a pointer that has been deleted by your cleaning thread. 这样,一次只能有一个线程具有该指针,并且工作线程没有机会获取/使用已被您的清洁线程删除的指针。

Pthreads doesn't have a lock_writing function, so it's not possible to review your code without knowing what that does. Pthreads没有lock_writing函数,因此无法在不知道其作用的情况下查看代码。

However it is undefined behavior to call pthread_mutex_destroy on a mutex that is locked. 但是,在锁定的互斥锁上调用pthread_mutex_destroy是未定义的行为。 So, assuming that the destructor of the object does destroy its mutex (either explicitly or as part of the destructor of a data member), you must unlock the mutex before that happens. 因此,假设对象的析构函数确实销毁了它的互斥锁(无论是显式的还是作为数据成员的析构函数的一部分),您必须在该互斥锁解锁之前将其解锁。

If you unlock it in the correct place then your code is correct. 如果在正确的位置将其解锁,则您的代码是正确的。 If you unlock it in the wrong place then it's incorrect. 如果在错误的位置解锁,则不正确。 I can't tell you exactly where the correct place is. 我无法确切告诉您正确的位置在哪里。

Other thread can get pointer to object from array at the same time when other thread can get it to delete from array and memory. 当其他线程可以将其从数组和内存中删除时,其他线程可以同时从该数组获取指向对象的指针。 "Work" thread will always get pointer, then lock mutex, make some work then unlock mutex and loss pointer forever. “工作”线程将始终获取指针,然后锁定互斥锁,进行一些工作,然后永远解锁互斥锁并丢失指针。

Possibly you need a mutex to protect the whole data structure, not just one object in it. 可能需要一个互斥锁来保护整个数据结构,而不仅仅是其中的一个对象。 It seems to me there is a possible race condition: 在我看来,可能存在比赛状况:

Thread 1                                Thread 2
                                    get pointer to object from data structure
get ptr from data structure
lock object
cleanup
unlock object
destroy object
                                    lock object (oops, it doesn't exist any more)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM