从线程中获取本机句柄
Getting Native Handle from Within a Thread?
>我正在使用VS2012,我想从正在运行的线程中设置线程优先级。目标是初始化具有最高优先级状态的所有线程。为此,我想对线程进行HANDLE
。
我在访问与thread
对象对应的指针时遇到一些问题。
这可能吗?
在调用主线程中,指针有效,从 C++11 线程中,指针设置为 CCCCCCCC
。以可预测的方式取消引用一些无意义的内存位置会导致崩溃。
下面的代码是显示问题的简化版本。
#include "stdafx.h"
#include <Windows.h>
#include <thread>
#include <mutex>
#include <condition_variable>
#include <iostream>
#include <atomic>
using namespace std;
class threadContainer
{
thread* mT;
condition_variable* con;
void lockMe()
{
mutex m;
unique_lock<std::mutex> lock(m);
con->wait(lock);//waits for host thread
cout << mT << endl;//CCCCCCCC
auto h = mT->native_handle();//causes a crash
con->wait(lock);//locks forever
}
public:
void run()
{
con = new condition_variable();
mT = new thread(&threadContainer::lockMe,*this);
cout << mT << endl; //00326420
con->notify_one();// Without this line everything locks as expected
mT->join();
}
};
int _tmain(int argc, _TCHAR* argv[])
{
threadContainer mContainer;
mContainer.run();
return 0;
}
#include <mutex>
#include <condition_variable>
#include <iostream>
#include <atomic>
#include <thread>
class threadContainer {
std::thread* mT;
std::mutex m;
void lockMe() {
// wait for mT to be assigned:
{
std::unique_lock<std::mutex> lock(m);
}
std::cout << "lockMe():" << mT << "n";
auto h = mT->native_handle();//causes a crash
std::cout << "Done lockMe!n";
}
public:
void run() {
// release lock only after mT assigned:
{
std::unique_lock<std::mutex> lock(m);
mT = new std::thread( [&](){ this->lockMe(); } );
}
std::cout << "run():" << mT << "n"; //00326420
mT->join();
}
};
int main() {
threadContainer mContainer;
mContainer.run();
return 0;
}
试试看。
0xcccccccc表示"变量未初始化"。 您的代码中存在线程争用错误。 线程在分配 "mT" 变量之前开始运行。 在分配完成之前,您将需要额外的同步来阻止线程,以便您可以安全地使用 mT。 这还将确保新线程可以看到 mT 的更新值,在多核机器上需要内存屏障。
这是一个包含 condition_variable
和 mutex
的示例代码。
class threadContainer
{
std::thread* mT;
std::mutex m;
std::condition_variable cv;
bool flag;
void lockMe() {
// 1. you must acquire lock of mutex.
unique_lock<std::mutex> lk(m);
// 2. and wait on `cv` for `flag==true`
cv.wait(lk, [&]{ return flag; });
cout << mT << endl;
auto h = mT->native_handle();
}
public:
void run()
{
flag = false;
mT = new std::thread( [&](){ this->lockMe(); } );
{
// 3. set `flag` and signal `cv`
lock_guard<decltype(m)> lk(m);
cout << mT << endl;
flag = true;
cv.notify_one();
}
mT->join();
}
};
如果你真正想做的是"初始化所有具有最高优先级状态的线程",那么这个简化的代码怎么样?无论如何,更改线程优先级取决于平台C++并且超出了标准库的范围。
class threadContainer
{
std::thread thd;
void work() {
// (1) change thread priority itself
::SetThreadPriority(::GetCurrentThread(), THREAD_PRIORITY_HIGHEST);
// do something...
}
public:
void run()
{
thd = std::thread( [&](){ this->work(); } );
// (2) or change thread priority from outside
::SetThreadPriority(thd.native_handle(), THREAD_PRIORITY_HIGHEST);
thd.join();
}
};
相关文章:
- 从C++本机插件更新Vector3数组
- GCC本机矩阵运算库
- VC++本机单元测试,找不到调试符号
- 在这种情况下,java对象是否可以调用本机函数
- PC中的程序和PHONE中的本机描述应用程序之间的数据连接
- 为什么导入Mixed native/CLR lib.dll的本机C++应用程序没有在Mixed lib.dll中的外部变
- 外壳包装器句柄/执行交互式命令管道C++ UNIX
- 无法将标准库导入本机 android 项目中的头文件
- 如何获取边缘窗口句柄 (HWND)?
- 枚举进程模块在有效句柄上返回无效句柄
- 在 c/c++ 中打开 PhysicalDrive 的句柄并写入 MBR 的 linux 等效是什么?
- 从运行服务的应用程序代码中提取窗口句柄
- 使用本机 C++ 应用程序中的 C# 代码
- 在nodejs中使用本机代码的最佳方法是什么?
- 如何告诉本机节点模块所需的dll存储在哪里?
- 在读取文件后重置句柄
- 从本机句柄创建std ::线程
- 提升线程 检查当前线程是否等于另一个本机句柄
- QMdiSubWindow的本机句柄
- 从线程中获取本机句柄