在另一个类中创建线程
Creating a thread in another class
我试图在ThreadMe类中为名为thefunction()的函数在主函数中创建一个线程。棘手的部分是,我需要启动一个线程在另一个类TYIA -Roland
#include <iostream>
#include <process.h>
#include <windows.h>
int main() {
char cincatcher[24];
std::cout << "I want to run a thread using a function on another classn";
// Here is a good place to start the thread
while( true ) {
std::cin >> cincatcher
}
}
class ThreadMe {
void thefunction();
};
void ThreadMe::thefunction() {
while( true ) {
std::cout << "working!n"
Sleep(800);
}
}
不能用类方法直接启动线程。您必须将类方法包装到一个普通函数中,然后使用该函数启动线程。例如:
void threadBody(void *p) {
ThreadME tm;
tm.thefunction();
}
相关文章:
- C++使用params创建线程函数会导致转换错误
- 为什么我不能在 while 循环中创建线程?
- 如何创建线程序列以按照启动顺序执行任务?
- 使用带有 ref 参数的成员函数创建线程时出现编译错误
- std::使用类成员函数创建线程 - 最佳实践
- CPP:如何使用需要指针的方法创建线程
- 使用 lambda 表达式创建线程时,如何为每个线程提供自己的 lambda 表达式副本
- C++,如何创建线程限制/受保护的变量和函数
- BOOST_LOG_SEV在创建线程后损坏async_read_some
- 为什么当我在构造函数中创建线程时,实例化对象和对象的指针的行为不同
- 如何通过调用类中的函数来创建线程
- 如何在Silverlight本机C 中创建线程
- 通过类构造函数创建线程
- C++:创建线程时出现问题;错误 C2672:"std::invoke":找不到匹配的重载函数
- 尝试在dll main中创建线程
- 如何在Linux上为C和C 创建线程安全LIB
- 如何在 dll 中创建线程 (c++)?
- 从调度队列块 [ swift / c++ ] 创建线程
- 从成员类功能创建线程
- 创建线程,但不要立即在 Linux 中运行它