英文:
How do I make a seperate thread inside a class?
问题
我有一个名为foo
的类,并在其中的一个成员函数中放置了一个线程对象。我尝试像这样初始化它:std::thread mythread(&foo::myprint, this);
,在另一个函数中。我的问题是,我得到了相同的thread::get_id
,而我需要对一些东西进行计数的不同函数foo::mycount
。myprint
和mycount
都使用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 <iostream>
#include <future>
#include <thread>
#include <chrono>
// dont do "using namespace std"
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 < 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(); // start thread
return 0;
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论