如何在类内部创建一个单独的线程?

huangapple go评论63阅读模式
英文:

How do I make a seperate thread inside a class?

问题

我有一个名为foo的类,并在其中的一个成员函数中放置了一个线程对象。我尝试像这样初始化它:std::thread mythread(&foo::myprint, this);,在另一个函数中。我的问题是,我得到了相同的thread::get_id,而我需要对一些东西进行计数的不同函数foo::mycountmyprintmycount都使用this_thread::sleep_for,但它们不会分开休眠(这是我想要发生的事情)。我使用一些代码示例跟进:

class foo
{
  void func()
  {
    std::thread mythread(&foo::myprint, this);
    mythread.join();
  }
  void myprint()
  {
    sleep_for(1s);
    cout << count << endl;
  }
  void mycount()
  {
    sleep_for(1ms);
    count++;
    cout << count << endl;
  }
};
void main()
{
  foo obj;
  while(1)
 {
   obj.func();
   obj.mycount();
 }
}

我还尝试将mycount放在另一个带有线程对象的函数中,我不知道std::call_once是否影响了任何事情,因为我在mycount函数中使用了它。我期望不同的函数有不同的get_id

英文:

I have a class foo and i put inside a member function a thread object. And i tried to initialize it like this std::thread mythread(&foo::myprint, this); inside another function. My problem is that I get the same thread::get_id with a different function foo::mycount that i need to count something. Both myprint and mycount uses this_thread::sleep_for but they don't sleep separately (something that i want to happen). I follow you up with some code example

class foo
{
  void func()
  {
    std::thread mythread(&foo::myprint, this);
    mythread.join();
  }
  void myprint()
  {
    sleep_for(1s);
    cout << count << endl;
  }
  void mycount()
  {
    sleep_for(1ms);
    count++;
    cout << count << endl;
  }
};
void main()
{
  foo obj;
  while(1)
 {
   obj.func();
   obj.mycount();
 }
}

I also tried putting mycount in another function with a thread object, and I don't if std::call_once affected anything, cause i used it inside the mycount function. I expected a different get_id for different functions.

答案1

得分: 1

以下是您要翻译的代码部分:

这里是一个使用 lambda 函数启动异步进程的示例。
并使用 std::future 来同步您的类的析构函数与后台线程(在此示例中计数数字)。

#include <iostream>
#include <future>
#include <thread>
#include <chrono>

// 不要使用 "using namespace std"

using namespace std::chrono_literals;

class foo
{
public:

    foo() = default;

    ~foo()
    {
        // m_future 的析构函数将与线程的执行同步(等待其完成)
    }

    void func()
    {
        m_future = std::async(std::launch::async, [=] { myprint(); });
    }


    void myprint()
    {
        for (std::size_t n = 0; n < 5; ++n)
        {
            std::this_thread::sleep_for(1s);
            std::cout << n << " ";
        }
        std::cout << "\n";
    }

private:
    std::future<void> m_future;
};

int main()
{
    foo obj;

    obj.func(); // 启动线程

    return 0;
}
英文:

Here is an example with a lambda function to start an asynchronous process.
And using std::future for synchronizing the destructor of your class with the background thread (which is counting numbers in this example).

#include &lt;iostream&gt;
#include &lt;future&gt;
#include &lt;thread&gt;
#include &lt;chrono&gt;

// dont do &quot;using namespace std&quot;

using namespace std::chrono_literals;

class foo
{
public:

    foo() = default;

    ~foo()
    {
        // destructor of m_future will synchronize destruction with execution of the thread (waits for it to finish)
    }

    void func()
    {
        m_future = std::async(std::launch::async, [=] { myprint(); });
    }


    void myprint()
    {
        for (std::size_t n = 0; n &lt; 5; ++n)
        {
            std::this_thread::sleep_for(1s);
            std::cout &lt;&lt; n &lt;&lt; &quot; &quot;;
        }
        std::cout &lt;&lt; &quot;\n&quot;;
    }

private:
    std::future&lt;void&gt; m_future;
};

int main()
{
    foo obj;

    obj.func(); // start thread

    return 0;
}

huangapple
  • 本文由 发表于 2023年1月9日 01:39:15
  • 转载请务必保留本文链接:https://go.coder-hub.com/75049997.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定