简体   繁体   English

C++:互斥和解除分配

[英]C++: Mutex and deallocation

I am writing a small program as part of a University course.我正在编写一个小程序作为大学课程的一部分。 In this program, I have a global Boost Mutex which helps me to coordinate two threads.在这个程序中,我有一个全局 Boost Mutex,它可以帮助我协调两个线程。

In my small library, I have to write a cleanup function that, when invoked, literally cleans up after itself.在我的小型库中,我必须编写一个清理程序 function ,当调用它时,它会自行清理。

What I am asking now is: if I create a Mutex, am I supposed to deallocate it as well when I do not need it anymore?我现在要问的是:如果我创建了一个 Mutex,我是否应该在不再需要它时也将其释放?

The code I use is simply我使用的代码很简单

boost::mutex mymutex;

Thanks a lot非常感谢

It will be deallocated when running out of scope which is defined by the curly braces.当用完由花括号定义的 scope 时,它将被释放。 You should be careful to unlock the mutex.您应该小心解锁互斥锁。

Boost mutexes appear to be written to perform all their cleanup in their destructors. Boost 互斥锁似乎被编写为在其析构函数中执行所有清理。 If you want to ready that same mutex for resuse, probably you'd want to call .unlock() on it once.如果您想准备好相同的互斥锁以供重用,可能您想在其上调用.unlock()一次。

If you really feel the need to manually get rid of it, I suppose you could make it a pointer and create it with a new .如果您真的觉得需要手动摆脱它,我想您可以将其设为指针并使用new的 . That way you can manually control running of its destructor in your cleanup routine by calling delete on the pointer.这样,您可以通过在指针上调用delete来手动控制其析构函数在清理例程中的运行。 However, pointers are kind of error-prone, so using one just so you can show it manually being cleaned up (instead of having it automatically happen when the object goes out of scope at the end of your program) is stoopid .但是,指针很容易出错,因此使用指针只是为了显示它被手动清理(而不是在程序结束时 object 退出 scope 时自动发生)是愚蠢的。 If you get docked a couple of points for not doing that, I'd consider it a small price to pay for designing things right instead.如果您因为不这样做而被扣分,我认为为正确设计事物付出的代价很小。

If you are really worried about losing points for not manually cleaning up your automatically destructed resources, if I were you I'd go ask my instructor what I need to do .如果你真的担心因为不手动清理自动销毁的资源而失去积分,如果我是你,我会 go 询问我的导师我需要做什么 School ain't free (unlike SO), and such consulting is part of what you are paying them all that money for.学校不是免费的(不像 SO),这样的咨询是你付给他们所有钱的一部分。 Might as well get some value for it.还不如从中获得一些价值。

Nope.没有。 Objects in C++ clean up after themselves. C++ 中的对象会自行清理。 You would only need to clean up the mutex if you had allocated it dynamically with new .如果您使用new动态分配了互斥锁,则只需要清理它。

In the clean up function you might need to call release/reset methods of the boost::mutex object to make sure you are not leaving it in signalled state.在清理 function 时,您可能需要调用boost::mutex object 的释放/重置方法,以确保您没有将其留在有信号的 state 中。

@Danilo: I'm pretty sure the course doesn't require you to use boost, and the teach expects you to use the pthreads API directly. @Danilo:我很确定该课程不需要您使用 boost,并且老师希望您直接使用 pthreads API。

In that case you'd want to call pthread_mutex_init and pthread_mutex_destroy , which boost does automatically for you在这种情况下,您需要调用pthread_mutex_initpthread_mutex_destroy , boost 会自动为您执行

When you define an object (as opposed to a pointer), such as in --当您定义一个 object(与指针相对)时,例如在 --

boost::mutex mymutex;

-- C++ will call the object's destructor when the object goes out of scope. -- C++ 将在 object 超出 scope 时调用对象的析构函数。

What you need to do now is check the documentation of boost::mutex on what it'll do when it's d'tor is called.您现在需要做的是检查 boost::mutex 的文档,了解调用 d'tor 时它会做什么。 However, you can be very sure that any class inside boost will clean up its resources in it's dtor (that's what a dtor is there for after all).但是,您可以非常确定 boost 中的任何 class 都会清理其 dtor 中的资源(毕竟这就是 dtor 的用途)。 What you do not know without checking the docs is what the Mutex's behaviour wrt.如果不检查文档,您不知道 Mutex 的行为是什么。 it's lock() function is when the dtor is being called.它是 lock() function 是在调用 dtor 时。

Since you have a global object, its dtor will be called after main() returns which should be fine as far as resource deallocation goes.由于您有一个全局 object,它的 dtor 将在 main() 返回后被调用,就资源释放而言应该没问题。

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

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