#include <iostream>
#include<Windows.h>
#include<thread>
#include<functional>
using namespace std;
class _mythread
{
public:
void callback(int param)
{
for (int i = 0; i < 100; i++)
{
g_val++;
this_thread::sleep_for(chrono::seconds(5));
}
cout << "值:" << g_val << "线程id:" << this_thread::get_id() <<"自定义参数为:"<<param << endl;
}
public:
int g_val = 0;
};
int main()
{
_mythread th1;
thread st(bind(&_mythread::callback,&th1,123 ));
st.join();
system("pause");
}
你的循环里线程挂起5秒钟,那么100此就要有500秒,所以要接近10分钟后你才能看到输出
不太懂c++
但是我看你的参数好像传错了,你输出一下param试试
1,您的线程创建和绑定没问题,可以正常运行。
2,将打印语句放在for循环中最后一行试试看。
for (int i = 0; i < 100; i++)
{
g_val++;
this_thread::sleep_for(chrono::seconds(5));
cout << "值:" << g_val << "线程id:" << this_thread::get_id() <<"自定义参数为:"<<param << endl;
}
从新检查代码!测试运行