相关疑难解决方法(0)

为什么将对象引用参数传递给线程函数无法编译?

我使用新的c ++ 11 std::thread接口遇到了问题.
我无法弄清楚如何std::ostream将对a的引用传递给线程将执行的函数.

这是传递整数的示例(在gcc 4.6下按预期编译和工作):

void foo(int &i) {
    /** do something with i **/
    std::cout << i << std::endl;
}

int k = 10;
std::thread t(foo, k);
Run Code Online (Sandbox Code Playgroud)

但是当我尝试传递一个ostream时,它无法编译:

void foo(std::ostream &os) {
    /** do something with os **/
    os << "This should be printed to os" << std::endl;
}

std::thread t(foo, std::cout);
Run Code Online (Sandbox Code Playgroud)

有没有办法做到这一点,还是根本不可能?

注意:从编译错误看来它似乎来自一个已删除的构造函数...

c++ libstdc++ c++11

29
推荐指数
1
解决办法
2万
查看次数

std :: threads构造函数参数错误

我不是一个优秀的C++程序员,但目前使用C++的一些功能来清理我的C代码的脏部分.g ++编译器抱怨threads[i] = thread(split, i, sums[i], from, to, f, nThreads);.请帮我找到问题所在.

// mjArray只是一个瘦的类而不是std :: vector,在我的情况下太重了.

#include <cstdio>
#include <cmath>
#include <ctime>
#include <thread>
using namespace std;

template<typename T>
class mjArray {
private:
    T* _array;
    int _length;

public:
    mjArray(int length) {
        _array = new T[length];
        _length = length;
    }

    mjArray(int length, T val) {
        _array = new T[length];
        _length = length;
        for (int i = 0; i < length; ++i) {
            _array[i] = val;
        }
    }

    ~mjArray() {
        delete[] …
Run Code Online (Sandbox Code Playgroud)

c++ multithreading templates move c++11

1
推荐指数
1
解决办法
139
查看次数

错误:没有匹配函数调用std :: thread

我试图运行一个dcp在线程中调用的函数,我必须独立运行该函数三次。所以这是我的实现方式:

void dcp(cv::Mat&, int, int, cv::Mat&, double);

int main(int argc, char* argv[])
{
cv::Mat IllumTrans;
//fill IllumTrans

std::vector<cv::Mat> rgbDCP;
rgbDCP.reserve(3);
//Fill it

std::thread thread_1(dcp, rgb[0], rows, cols, IllumTrans, A[0]);
std::thread thread_2(dcp, rgb[1], rows, cols, IllumTrans, A[1]);
std::thread thread_3(dcp, rgb[2], rows, cols, IllumTrans, A[2]);

thread_1.join();
thread_2.join();
thread_3.join();
}
Run Code Online (Sandbox Code Playgroud)

但是我得到了没有匹配函数的错误调用:

In file included from 21022018WorksfineOneimageThread.cpp:6:0:
/usr/include/c++/7/thread: In instantiation of ‘struct std::thread::_Invoker<std::tuple<void (*)(cv::Mat&, int, int, cv::Mat&, double), cv::Mat, int, int, cv::Mat, int> >’:
/usr/include/c++/7/thread:127:22:   required from ‘std::thread::thread(_Callable&&, _Args&& ...) [with _Callable …
Run Code Online (Sandbox Code Playgroud)

c++ multithreading stdthread

1
推荐指数
1
解决办法
3284
查看次数

标签 统计

c++ ×3

c++11 ×2

multithreading ×2

libstdc++ ×1

move ×1

stdthread ×1

templates ×1