繁体   English   中英

VC ++中的MultiThread程序

[英]MultiThread program in VC++

我试图做一个线程化的应用程序,将它们放入队列后无限打印一组数字。 我收到此错误:

错误1错误C3867:'Test :: ThreadFunc':函数调用缺少参数列表; 使用'&Test :: ThreadFunc'创建一个指向成员的指针。

我究竟做错了什么? 有什么错?

#include "stdafx.h"

#include <chrono>
#include <mutex>
#include <thread>
#include <list>

class Test {
    std::list<int> queue;
    std::mutex m;

public:
    void ThreadFunc()
    {
        // Loop is required, otherwise thread will exit
        for (;;)
        {
            bool read = false;
            int value;
            {
                std::lock_guard<std::mutex> lock(m);

                if (queue.size())
                {
                    value = queue.front();
                    read = true;
                    queue.pop_back();
                }
            }

            if (read)
            {
                // send(header.data(), header.dataSize());
                // send(proto.data(), proto.dataSize());
                printf("Hello %d\n", value);
            }

            std::this_thread::sleep_for(std::chrono::milliseconds(10));
        }
    }

    void TestFunc()
    {
        std::thread thread(ThreadFunc);
        thread.detach();

        int i = 0;
        // Loops only as a test example
        for (;;)
        {
            std::lock_guard<std::mutex> lock(m);
            std::this_thread::sleep_for(std::chrono::milliseconds(2000));
            queue.push_back(i++);
            // Queue Message(header, payload);
        }
    }

};

int main()
{
    Test test;
    test.TestFunc();
}

您正在尝试将指针传递给类的成员函数。 当您执行此操作时,默认情况下会在函数中添加一个参数,该参数是您要在其上调用函数的类的实例的指针。 在您的情况下,指向类的指针将是this指针。

请参阅此以获取语法参考: 使用成员函数启动线程

为了回答您的评论,为什么不隐式通过? 您不是将函数作为类的成员调用,而是通过指针传递了成员函数。 这是一种不同的,独特的情况,请参阅以下参考: 在C ++中将成员函数作为参数传递

另外,为免日后头疼,出现的下一个问题是std :: thread的构造函数按值接收其参数,因此,如果需要通过引用传递任何参数,请查看std :: ref。

解决方法是这里。 这可行。 谢谢@mock_blatt

#include "stdafx.h"

#include <chrono>
#include <mutex>
#include <thread>
#include <list>



class Test {
std::list<int> queue;
std::mutex m;

public:
    void ThreadFunc()
    {
        // Loop is required, otherwise thread will exit
        for (;;)
            {
                bool read = false;
                int value;
                {
                    std::lock_guard<std::mutex> lock(m);

                    if (queue.size())
                    {
                        value = queue.front();
                        read = true;
                        queue.pop_back();
                    }
                }

        if (read)
        {
        // send(header.data(), header.dataSize());
        // send(proto.data(), proto.dataSize());
            printf("Hello %d\n", value);
        }

        std::this_thread::sleep_for(std::chrono::milliseconds(10));
    }
}

 void TestFunc()
 {
    std::thread thread(std::bind(&Test::ThreadFunc, this));
    thread.detach();

    int i = 0;
    // Loops only as a test example
    for (;;)
    {
        std::lock_guard<std::mutex> lock(m);
        std::this_thread::sleep_for(std::chrono::milliseconds(2000));
        queue.push_back(i++);
        // Queue Message(header, payload);
    }
}

};

int main()
{
 Test test;
test.TestFunc();
}

更改std::thread thread(ThreadFunc); std::thread thread(Test::ThreadFunc, this);

暂无
暂无

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

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