简体   繁体   English

VC ++中的MultiThread程序

[英]MultiThread program in VC++

I am trying to do a threaded application to infinitely print a set of numbers after enqueing them. 我试图做一个线程化的应用程序,将它们放入队列后无限打印一组数字。 I get this error: 我收到此错误:

Error 1 error C3867: 'Test::ThreadFunc': function call missing argument list; 错误1错误C3867:'Test :: ThreadFunc':函数调用缺少参数列表; use '&Test::ThreadFunc' to create a pointer to member. 使用'&Test :: ThreadFunc'创建一个指向成员的指针。

What am I doing wrong? 我究竟做错了什么? What is the mistake ? 有什么错?

#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();
}

You're attempting to pass a pointer to a member function of a class. 您正在尝试将指针传递给类的成员函数。 When you do this, there's an argument added to the function, tacitly, that is a pointer to the instance of the class that you're invoking the function on. 当您执行此操作时,默认情况下会在函数中添加一个参数,该参数是您要在其上调用函数的类的实例的指针。 In your case, the pointer to the class will be the this pointer. 在您的情况下,指向类的指针将是this指针。

See this for syntax reference: Start thread with member function 请参阅此以获取语法参考: 使用成员函数启动线程

To answer your comment, why isn't it passed implicitly? 为了回答您的评论,为什么不隐式通过? You're not calling the function as a member of a class, you're passing the member function by pointer. 您不是将函数作为类的成员调用,而是通过指针传递了成员函数。 This is a different, unique, situation, see this reference: Passing a member function as an argument in C++ 这是一种不同的,独特的情况,请参阅以下参考: 在C ++中将成员函数作为参数传递

Also, to save a little future headache, the next problem that comes up is that std::thread's constructor takes its arguments by value, so if you need to pass any arguments by reference, take a look at std::ref. 另外,为免日后头疼,出现的下一个问题是std :: thread的构造函数按值接收其参数,因此,如果需要通过引用传递任何参数,请查看std :: ref。

Here's the fix. 解决方法是这里。 This works. 这可行。 Thank you @mock_blatt 谢谢@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();
}

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

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

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