传递参数给线程

#include <iostream>
#include <thread>
#include <windows.h>

using namespace std;
void switch_ab (int &a, int &b)   //参数是引用可更改

//void switch_ab (int &a, int &b)  //不可更改
{
int temp = b;
b = a;
a = temp;
cout << "a:" << a << endl;
cout << "b:" << b << endl;
}

int a = 4;
int b = 5;
thread t(switch_ab, ref(a), ref(b));  //传入引用

//thread t(switch_ab, a, b);   //不可更改

int main()
{
t.join();
cout << "a1:" << a << endl;
cout << "b1:" << b << endl;
while (1);
}

posted @   penuel  阅读(340)  评论(0编辑  收藏  举报
努力加载评论中...
点击右上角即可分享
微信分享提示