C++ threads and classess

C++ threads and classess

本文关键字:classess and threads C++      更新时间:2023-10-16

我最近一直在开发一个线程程序,很简单,它很有效。我现在想改进它,或者用面向对象的方式重写它。

我正在使用Code::Blocks,正如我所学到的,我将类存储在单独的文件中。在那里,我有.h文件中原型化的函数。我想问的是:我应该从哪里开始线程?我应该在哪里加入他们?在"main.cpp"文件中?如果我存储类和函数,你能提供一个简单的例子来说明如何启动这样的线程吗。在不同的文件中?谢谢
我的类文件

#include "fooCheck.h"
#include <iostream>
fooCheck::fooCheck()
{
    std::cout << "blah blah" << std::endl;
    std::cout << "ctor"<< std::endl;    
}
int fooCheck::fooFS(){   <- I want this to be startred as a thread
    some code
    return 1;
}

我的头文件

    class fooCheck
{
    public:
        fooCheck();
        int fooFS();
    private:
};
#endif

和我的main()cpp文件。。。

#include stuff_stuff_stuff
#include "fooCheck.h"
int main()
{
    ICMP_check foo_acces_object;
    foo_acces_object.fooFS();
}

因此,放置的对象运行良好,并从类中执行函数。我希望类中的函数作为线程启动并执行。我尝试的是:std::thread tt1{foo_check::fooFS()};std::thread tt1{fooFS()};关于如何推进这一进程,有什么建议吗?

我提出了一种运行c++线程的模式,这些线程的生命周期与线程所属的类相同。它基本上是c++类的线程模式,避免了使用c++线程时的一些常见问题。

http://blog.chrisd.info/how-to-run-threads/