简体   繁体   English

将 void 指针指向的 std::thread 转换为 object std::thread 而不是指向 object 的指针

[英]Convert std::thread pointed by void pointer to object std::thread instead of pointer to object

I was looking to an exercise I did, specifically a class "threads manager" which manage threads (omg really? lol) by storing them inside an array of "typedef void *HANDLE;"我正在寻找我所做的练习,特别是 class“线程管理器”,它通过将线程存储在“typedef void *HANDLE;”数组中来管理线程(真的吗?哈哈) declared inside a struct GROUP.在结构 GROUP 中声明。

So I already made it working, but I saw that I was converting the "HANDLE", so "void*", by "reinterpret_cast" conversion to "std::thread*".所以我已经让它工作了,但我看到我正在通过“reinterpret_cast”转换为“std::thread*”来转换“HANDLE”,所以“void*”。

After I saw that I worried: how and can I convert it directly to an std::thread object?看到后我担心:如何以及可以将其直接转换为 std::thread object?

I did this code for example:例如,我做了这段代码:

#include <iostream>
#include <thread>

typedef void *HANDLE;

class thread_obj {
    public:
        void operator()(int x){
            for(int i = 0x0; i < x; i++){
                std::cout << "i = " << i << std::endl;
            }
        }
};

int main(){

    std::thread thr(thread_obj(), 0x3);
    HANDLE test = &thr;

    std::thread *p_thr = reinterpret_cast<std::thread*>(test);

    p_thr->join();

    return 0x0;
}

This is the original code.这是原始代码。

If instead I do:相反,如果我这样做:

std::thread p_thr = reinterpret_cast<std::thread&>(test);

or:或者:

std::thread p_thr = *reinterpret_cast<std::thread*>(test);

or:或者:

std::thread *temp = reinterpret_cast<std::thread*>(test);
std::thread p_thr = *temp;

I always get:我总是得到:

error: use of deleted function 'std::thread::thread(std::thread&)'错误:使用已删除的 function 'std::thread::thread(std::thread&)'

On "reinterpret_cast" line for the first and second case, and on the following assignment line for the third.在第一种和第二种情况下的“reinterpret_cast”行上,在第三种情况下的下一个赋值行上。

Now I suppose the problem is with the copy constructor called.现在我想问题出在调用的复制构造函数上。 I searched a little and I opened the thread class and I found it:我搜索了一下,打开了线程 class 并找到了它:

thread(thread&) = delete;

So:/ searching a little I only found the solution on which you override the copy constructor.所以:/搜索了一下我只找到了覆盖复制构造函数的解决方案。 In this case I think a solution could be make a superclass an re-declare this deleted constructor, right?在这种情况下,我认为一个解决方案可以是让一个超类重新声明这个已删除的构造函数,对吧? But this is just a waste of time:/但这只是浪费时间:/

So is there a way to convert this "void*" to "std::thread" object, again to a "std::thread" object?那么有没有办法将此“void *”转换为“std::thread”object,再次转换为“std::thread”object? If yes or no can you explain me in detail please?如果是或否,你能详细解释一下吗?

Thank you so much have a nice day and coding:D非常感谢您有愉快的一天和编码:D

What you really need is你真正需要的是

std::thread& ref_thr = *reinterpret_cast<std::thread*>(test);
//         ^ declare a reference to a std::thread

and then you can use ref_thr just like you would use a normal object.然后您可以像使用普通ref_thr一样使用 ref_thr。

As you've found, the copy constructor for std::thread is deleted, so if you actually want to create a thread object from test , then you need to move the casted object into new_thr like如您所见, std::thread的复制构造函数已被删除,因此,如果您确实想从test创建线程 object ,则需要将已转换的 object 移动到new_thr

std::thread new_thr = std::move(*reinterpret_cast<std::thread*>(test));

but this means that the object pointer to by test is no longer usable as it was moved into new_thr .但这意味着test指向的 object 指针不再可用,因为它已移至new_thr This is why you want to use a reference, unless you actually want to move it.这就是为什么要使用引用的原因,除非您真的想移动它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM