Xcode 7:C ++线程错误:尝试使用已删除的功能 [英] Xcode 7: C++ threads ERROR: Attempting to use a deleted function

查看:111
本文介绍了Xcode 7:C ++线程错误:尝试使用已删除的功能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在Xcode 7上用c ++编写数独求解器.我设法使用回溯算法编写了成功的求解器.

现在,我正在尝试并行化求解器中的所有函数,以便加快求解算法的速度.我有3个函数,负责检查试图插入到网格中的当前数字是否存在于行,列或框中. (数独标准规则).由于这些是互斥的操作,因此我想将它们并行化.

我知道对这个程序进行多线程处理是多余的,但是目标是更多地学习多线程处理,而不是加快求解器算法的速度.

这是我到目前为止所获得的. 我已经包含了标准的c ++ 11线程库. 使用默认的Xcode 7构建设置.

我得到的错误是我正在尝试使用删除的功能,当我在Xcode上单击"Build and Run"按钮时,该功能会弹出. Xcode的intellisense不会抱怨我的代码.我不明白请帮忙.

#include <thread>

....

typedef uint8_t byte;
typedef uint16_t dbyte;

....

bool sudokuGame::check(byte num, byte row, byte col)
{
    setBoxFlag(true);
    setColFlag(true);
    setRowFlag(true);

    std::thread t1{&sudokuGame::checkRow, num, row};
    std::thread t2{&sudokuGame::checkColumn,num,col};
    std::thread t3{&sudokuGame::checkBox,num,row,col};

    t1.join();
    t2.join();
    t3.join();

    return (getBoxFlag() && getRowFlag() && getColFlag()); 
}

弹出试图使用已删除的功能"错误的线程"中的某处.

...

__thread_execute(tuple<_Fp, _Args...>& __t, __tuple_indices<_Indices...>)
{
    __invoke(_VSTD::move(_VSTD::get<0>(__t)), _VSTD::move(_VSTD::get<_Indices>(__t))...);
}

...

我的构建设置如下

解决方案

要使用非静态成员函数创建线程,您需要提供成员函数应使用的实例,即线程函数内的this变量.这是通过将实例作为第一个参数传递给线程函数来完成的:

std::thread t1{&sudokuGame::checkRow, this, num, row};
//                                    ^^^^
//             Note the use of `this` here

请注意,您不需要更改线程功能,它们全部由标准库和编译器处理.

然后遇到另一个问题:像这样的线程分配

t1=std::thread(&sudokuGame::checkRow, num, row);

他们没有任何意义.您已经创建了线程对象,对其进行了初始化,并使线程运行.而且由于线程已经在运行,您不能 分配给它们.参见有关重载线程分配运算符的参考. >

您遇到的编译器错误是由于第一个问题,即在创建线程时不传递实例.

I have been writing a sudoku solver in c++ on Xcode 7. I managed to write a successful solver using a backtracking algorithm.

Now i'm trying to parallelize whatever functions inside my solver so that I can to speed up the solving algorithm. I have 3 functions that are in charge of checking if the current number trying to be inserted into the grid exists in the row, column or box. (standard sudoku rules). Since these are mutually exclusive operations I want to parallelize them.

I know it's overkill to multithread this program, but the goal is more to learn multithreading rather than speed up my solver algorithm.

This is what I've got so far. I've included the standard c++11 thread library. Using default Xcode 7 build settings.

The error I get says that I'm attempting to use a deleted function which pops up when I hit the "Build and Run" button on Xcode. Xcode's intellisense does not complain bout my code. I don't understand this. Please help.

#include <thread>

....

typedef uint8_t byte;
typedef uint16_t dbyte;

....

bool sudokuGame::check(byte num, byte row, byte col)
{
    setBoxFlag(true);
    setColFlag(true);
    setRowFlag(true);

    std::thread t1{&sudokuGame::checkRow, num, row};
    std::thread t2{&sudokuGame::checkColumn,num,col};
    std::thread t3{&sudokuGame::checkBox,num,row,col};

    t1.join();
    t2.join();
    t3.join();

    return (getBoxFlag() && getRowFlag() && getColFlag()); 
}

Somewhere inside "thread" where the "attempting to use a deleted function" ERROR pops up.

...

__thread_execute(tuple<_Fp, _Args...>& __t, __tuple_indices<_Indices...>)
{
    __invoke(_VSTD::move(_VSTD::get<0>(__t)), _VSTD::move(_VSTD::get<_Indices>(__t))...);
}

...

My build settings looks like this

解决方案

To create a thread using non-static member functions, you need to provide the instance the member function should use, the this variable inside the thread function. This is done by passing the instance as the first argument to the thread function:

std::thread t1{&sudokuGame::checkRow, this, num, row};
//                                    ^^^^
//             Note the use of `this` here

Note that you don't need to change the thread function, it's all handled by the standard library and the compiler.

Then for another problem: The thread assignments like

t1=std::thread(&sudokuGame::checkRow, num, row);

They don't make any sense. You have already created the thread objects, initialized them, and got the thread running. And because the threads are already running you can't assign to them. See e.g this reference for the overloaded thread assignment operator.

The compiler error you get is because of the first problem, that you don't pass an instance when creating the threads.

这篇关于Xcode 7:C ++线程错误:尝试使用已删除的功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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