从线程中获取本机句柄

Getting Native Handle from Within a Thread?

本文关键字:本机 句柄 获取 线程      更新时间:2023-10-16

>我正在使用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();
    }
};