如何用托管锁替换c ++非托管类mutex.lock? [英] How can I replace c++ unmanaged class mutex.lock with managed lock?

查看:114
本文介绍了如何用托管锁替换c ++非托管类mutex.lock?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果你能给我一个关于下面问题的小费,我将非常感激。



thisClass.cpp现在符合clr,所以<互斥>不再允许了!

我用新的msclr / lock.h替换了互斥锁,但是不能给锁定一个托管对象,因为这是一个非托管对象!



I'll appreciate it very much if you could give me a tip with the problem below.

thisClass.cpp is now complied with "clr", so < mutex > is not any more allowed!
I have replaced mutex with new msclr/lock.h, but cannot give the lock a managed object, since this is an unmanaged object!

#include "thisClass.h"
// #include < mutex >
#include < msclr/lock.h >

// std::mutex mutexLock;


void * thisClass::fo(){
      // mutexLock.lock();
      lock l(this);   <<<<< this is not Ok, since "this" is an unmanaged object!!!

      :::::
                                   
     // mutexLock.unlock();    

}

推荐答案

不是一个很好的解决方案,但我决定给出一个空的托管对象上课。它似乎起作用,但对我来说是一个奇怪的解决方案!



还有更好的方法吗?我可以避免重复gcnew以某种方式!?



Not a nice solution but I decided to give an empty managed object to the class. It seems to function, but to me a strange solution!

Is there a better way? Could I avoid repeated "gcnew somehow!"?

#include < msclr/lock.h >

using namespace msclr;    // << added missing namespace

ref class lockRef{
    static lockRef^ _lockRef;
}

void * thisClass::fo(){
  lockRef^ x = gcnew lockRef();  // <<  outside definitions were flagged as error!!!
  lock l(x)
  :::::
}

这篇关于如何用托管锁替换c ++非托管类mutex.lock?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆