<thread> 在 C++/CLI 代码中包含的代码中使用。寻找更好的解决方案
Using <thread> in code which is included in C++/CLI code. Looking for a better solution
native C++ "a.h":
#include <thread>
class a
{
//...
private:
// ...
std::mutex mutex;
};
C++/命令行界面:
#include "a.h" // error cause thread is included
我的解决方案:
母语C++"A.H":
class a
{
a();
~a();
//...
private:
// ...
void* mutex;
};
"阿.cpp"
#include <thread>
a::a()
{
mutex = new std::mutex;
}
~a::a()
{
delete mutex;
}
C++/命令行界面:
#include "a.h" // no error
- 如何在没有动态内存分配的情况下执行此操作?
- 这安全吗?我不知何故在 C++/CLI 环境中使用来自<线程>的包装内容,只是不在标头中。线程>
- 有没有更好的方法?每次使用的新/删除和强制转换似乎很糟糕。我想每个类的对象都有一个互斥锁。
模板在这里为您提供帮助!
啊:
template <class MutextType>
class a
{
//...
private:
// ...
MutextType mutex;
};
然后,在您的主要或任何地方:
#inculde <thread>
//...
a<std::mutex> instance;
如果需要,您还可以使用 using 语句进行清理,以获得更干净的代码:
#inculde <thread>
//...
using MutexA = a<std::mutex>;
//...
MutexA instance;
相关文章:
- QT在错误的班级中寻找空位
- C++我的数学有什么问题,为什么我的代码不能正确循环
- 代码在main()中运行,但在函数中出现错误
- 在VS代码中交叉编译Windows与Linux上的MinGW的SDL程序
- 编译包含字符串的代码时遇到问题
- 我在c++代码中生成了一个运行时#3异常
- 如何在linux终端中同时编译和运行c++代码
- 为cl.exe(Visual Studio代码)指定命令行C++版本
- 我只是在寻找模板,在我的书中找到了这段代码,这显示了隔离错误?
- 寻找有关为什么此C++代码在没有引用的情况下不起作用的解释
- 初学者代码寻找改进
- <thread> 在 C++/CLI 代码中包含的代码中使用。寻找更好的解决方案
- 在我的C 代码上寻找功能和数组的错误
- 寻找汉明数——不是代码或距离
- 寻找一个素数代码c++
- 寻找8x8(或nxn)离散余弦变换(DCT)/IDCT伪代码
- 寻找一个代码解决方案来导入.mdb表到另一个.mdb表
- 寻找c++代码示例资源
- 寻找在c#中创建c++ DLL然后P/Invoke的代码示例(适用于Windows CE 5/6)
- C ++链表不释放内存 - 寻找快速代码审查