常见c/C++
#include <windows.h> #include <iostream> DWORD WINAPI ThreadProc(LPVOID lpParam) { std::cout << "线程执行中,参数是: " << (int)lpParam << std::endl; return 0; } int main() { HANDLE hThread = CreateThread( NULL, // 默认安全属性 0, // 默认栈大小 ThreadProc, // 线程函数指针 (LPVOID)123, // 传递给线程函数的参数 0, // 创建标志 NULL // 不需要线程ID ); if (hThread == NULL) { std::cerr << "CreateThread failed ( " << GetLastError() << " ).\n"; return 1; } // 等待线程结束,以便主程序可以清理资源 WaitForSingleObject(hThread, INFINITE); CloseHandle(hThread); return 0; }
C++:
void threadFunction() { std::cout << "Thread is running." << std::endl; } int main() { // 创建线程 auto thread = std::make_unique<std::thread>(threadFunction); // 等待线程完成 thread->join(); return 0; }
标签:std,cout,threadFunction,C++,线程,include,写法 From: https://www.cnblogs.com/8335IT/p/18325745