传递参数给线程

#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 @ 2019-09-04 19:22  penuel  阅读(337)  评论(0编辑  收藏  举报