开发者

How are mutex created on Linux?

开发者 https://www.devze.com 2023-02-03 02:44 出处:网络
I\'d like to k开发者_如何转开发now how are mutex created on Linux? I figured out, that pthread_mutex_init() doesn\'t change value of pthread_mutex_t variable, so how it \"create\" mutex?

I'd like to k开发者_如何转开发now how are mutex created on Linux? I figured out, that pthread_mutex_init() doesn't change value of pthread_mutex_t variable, so how it "create" mutex?

Does it mark this variable as some kind of system resource or what?

I was implementing R-value constructor for class, which have a pthread_mutex_t field in it's body and I don't know how to move mutex frome one class to another...


You can see what pthread_mutex_init does here (warning, you brain will hurt).

It does memset() the mutex.

However, mutexes are implemented on top of the futex calls. This works on memory addresses, i.e. the address of one of the pthread_mutex_t members is used as a system resource. This means you cannot copy/move a pthread_mutex_t.


It seems like you want to pass ownership of the mutex to another class. Are you sure that is the correct way to solve your problem? If you absolutely need to do it though, you could create an auto_ptr to pass ownership around:

class A
{
   A(const A & other) mutex(other.mutex) { /* ... */ }

   auto_ptr<pthread_mutex_t> mutex;
}
0

精彩评论

暂无评论...
验证码 换一张
取 消