std :: thread(threadfunction,this);在视觉工作室中导致错误,但不会导致视觉工作室代码

发布于 2025-02-10 20:43:03 字数 1960 浏览 1 评论 0原文

因此,我有以下C ++类,该类将std ::线程作为成员变量存储,并在该单独的线程中启动成员函数。但是,此代码仅在Visual Studio代码(使用MSYS2 12.1.0作为G ++编译器)中构建,并且在尝试在Visual Studio代码中构建它时会导致错误。

导致错误的行似乎是:foo_thread = std :: thread(threadFunction,this); 在Visual Studio代码中,我得到了一个红色的下划线警告,说“ constructor std :: thread :: thread“匹配参数列表”的实例,但是代码仍然可以编译并运行良好。

在Visual Studio中,我遇到了相同的错误,并且警告“ C3867'foo :: threadFunction':非标准语法;使用'&'要创建一个指向成员的指针”,并且代码不编译。当我尝试foo_thread = std :: thread(& threadFunction)时,this);错误会消失,但是当我尝试构建时我会在绑定的成员函数表达式上获得错误“&:非法操作。 foo_thread = std :: thread(& threadFunction,this);但是,仍然可以编译并运行Visual Studio代码。

如何使本节根据需要工作并能够跨C ++编译器/IDE进行编译?还值得一提的是,当将新生成的线程交给新卵线的功能不是成员函数时,我似乎并没有遇到这些错误,但是我需要它是此程序的成员函数。

代码:

#include <thread>
#include <windows.h>
#include <iostream>

#define DllExport   __declspec( dllexport )

typedef void (*funcPtr)(int num);

class Foo {
    public:
        // Value Constructor
        Foo(int a, int b, funcPtr f) {
            foo = a;
            bar = b;
            myFunc = f;
        }
        int getFoo() { return foo; };
        int addBar(int a) { return private_method(a); };
        void callMyFunc(int n) { myFunc(n); };
        // Start Monitor
        void StartThread();
        // Stop Monitor
        // IsMonitoring()
        ~Foo() {
            if (foo_thread.joinable()) {
                foo_thread.join();
            }
        };
    private:
        int private_method(int a) { return a + bar; };
        int foo;
        int bar;
        std::thread foo_thread;
        void threadFunction();
        funcPtr myFunc;
        std::atomic<bool> monitoring = ATOMIC_VAR_INIT(false);
};

void Foo::StartThread() {
    foo_thread = std::thread(threadFunction, this);
}

void Foo::threadFunction() {
    for (int i = 0; i < 10; i++) {
        std::cout << "Hello:" << i << std::endl;
        Sleep(500);
    }
}

So I have the following c++ class which stores a std::thread as a member variable, and starts a member function in that separate thread. However, this code only builds in visual studio code (using msys2 12.1.0 as a G++ compiler), and causes errors when I try to build it in visual studio code.

The line that causes the error seems to be: foo_thread = std::thread(threadFunction, this);
In visual studio code I get a red underline warning saying "no instance of constructor std::thread::thread" matches the argument list", however the code still compiles and runs fine.

In visual studio I get the same error, and a warning "C3867 'Foo::threadFunction': non-standard syntax; use '&' to create a pointer to member", and the code does not compile. When I try foo_thread = std::thread(&threadFunction, this); the error goes away, however when I try to build I get the error "&: illegal operation on bound member function expression. foo_thread = std::thread(&threadFunction, this); does still compile and run on visual studio code however.

How do I make this section work as desired and able to compile across c++ compilers/ides? It is also worth mentioning I don't seem to get these errors when the function that is handed to the newly spawned thread is not a member function, however I need it to be a member function for this program.

Code:

#include <thread>
#include <windows.h>
#include <iostream>

#define DllExport   __declspec( dllexport )

typedef void (*funcPtr)(int num);

class Foo {
    public:
        // Value Constructor
        Foo(int a, int b, funcPtr f) {
            foo = a;
            bar = b;
            myFunc = f;
        }
        int getFoo() { return foo; };
        int addBar(int a) { return private_method(a); };
        void callMyFunc(int n) { myFunc(n); };
        // Start Monitor
        void StartThread();
        // Stop Monitor
        // IsMonitoring()
        ~Foo() {
            if (foo_thread.joinable()) {
                foo_thread.join();
            }
        };
    private:
        int private_method(int a) { return a + bar; };
        int foo;
        int bar;
        std::thread foo_thread;
        void threadFunction();
        funcPtr myFunc;
        std::atomic<bool> monitoring = ATOMIC_VAR_INIT(false);
};

void Foo::StartThread() {
    foo_thread = std::thread(threadFunction, this);
}

void Foo::threadFunction() {
    for (int i = 0; i < 10; i++) {
        std::cout << "Hello:" << i << std::endl;
        Sleep(500);
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

陈独秀 2025-02-17 20:43:03

我弄清楚了,适当的语法是:

foo_thread = std::thread(&Foo::threadFunction, this);

I figured it out, proper syntax to make this work is:

foo_thread = std::thread(&Foo::threadFunction, this);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文