C++11 提供头文件thread,使用std的thread实例化一个线程对象创建。
#include#include #include using namespace std; mutex m; int cnt = 10; void thread1() { while (cnt > 5){ lock_guard lockGuard(m); if (cnt > 0) { --cnt; cout << cnt << endl; } } } void thread2() { while (cnt > 0) { lock_guard lockGuard(m); if (cnt > 0) { cnt -= 10; cout << cnt << endl; } } } int main(int argc, char* argv[]) { thread th1(thread1); //实例化一个线程对象th1,该线程开始执行 thread th2(thread2); th1.join(); th2.join(); cout << "main..." << endl; return 0; }
#include#include using namespace std; void myThread(int first, int second) { cout << "in my thread! " << first << " "<< second < 欢迎分享,转载请注明来源:内存溢出
评论列表(0条)