这是在c++中启动线程的正确方式吗?
Is this the right way to start a thread in C++
这是我用来启动线程的方法,它工作,但我想知道这种方式是否有任何缺点。
void myFunc()
{
//code here
}
unsigned int _stdcall ThreadFunction(void* data)
{
myFunc();
return 0;
}
我的主函数我使用:
HANDLE A = (HANDLE)_beginthredex(0,0,&ThreadFunction,0,0,0);
我以CloseHandle(A);
结尾。
如果你可以访问c++ 11,使用<thread>
库,你就不需要担心跨平台兼容性:
#include <thread>
std::thread t(&ThreadFunction, nullptr);
等待线程执行完成,使用join()
:
t.join();
阻塞,直到线程应该运行的函数返回。
否则,使用CreateThread(因为它看起来像你在Windows上)或beginthreadx。
对于POSIX,使用pthread_create()
相关文章:
- 从不同线程使用int64的不同字节安全吗
- Qt C++静态thread_local QNetworkAccessManager是线程应用程序的好选择吗
- 在std::thread中,joinable()然后join()线程安全吗
- 以线程安全的方式调用"QQuickPaintedItem::updateImage(const QImage&image)"(no QThread)
- 用C++中的std::condition_variable将线程置于死锁中会有风险吗
- 并发/多线程:是否可以以这种方式生成相同的输出?
- 我可以将shared_ptr作为临时传递给线程吗?
- 以线程安全的方式转换 C/C++ 中时区名称字符串的时区偏移量
- C++线程安全:如果只有一个线程可以写入非原子变量,但多个线程从中读取. 会遇到问题吗?
- 提升精神 V2 Qi 语法线程安全吗?
- asio 链对象线程安全吗?
- 我应该在C++中打印线程 ID 吗?
- 多个线程可以安全地同时将相同的值写入同一变量吗?
- 如何以线程安全的方式更改目录?
- 给定使用 C++ 或 C,我如何测量在 linux 下进行线程切换需要多长时间?可能吗?
- Eclipse CDT 多线程调试不是最佳的 - 如何以独占方式运行线程
- 如何使用pthreads以智能的方式向线程共享变量
- 这是在c++中启动线程的正确方式吗?
- 以最优雅的方式同步线程
- 如何以可导出的方式命名线程