#include <iostream>
#include <thread>
#include <unistd.h>
#include <string>
using namespace std;
void show(int id, int count,const string &str);
int main(void)
{
// 传递字符串的字面值:char const *,其实是按指针进行传递
// t1接收指针,将指向字符串的指针,隐式转换成string类型
thread t1(show, 1, 10, "hello");
string s = "world";
// 按指针进行传递时不加ref
thread t2(show, 2, 10, s);
t1.join();
t2.join();
return 0;
}
void show(int id, int count, const string &str)
{
for(int i=0; i < count; ++i)
{
cout << "Thread " << id << ": " << str << endl;
sleep(1);
}
}
2.线程函数
最新推荐文章于 2024-07-25 23:49:52 发布