c++ thread的使用 调用类里面的函数和调用类外的函数的区别

 1.thread 调用类外的函数。

在使用thread之前要加上#include <thread>。

例1:

#include <iostream>
#include <thread>
using namespace std;
void Threadfunc1()
{
	cout << "Threadfunc1" << endl;
}

void Threadfunc2(int vaule)
{
	cout << "Threadfunc1:" <<vaule<< endl;
}

int main(int argc, char* argv[])
{
	thread thread1(Threadfunc1);
	thread thread2(Threadfunc2,3);
    getchar();
	return 0;
}

如果有调用的线程函数参数,那么就像这样thread thread2(Threadfunc2,3);

在形参函数后面加上它的参数。

2.thread 调用类内的函数

示例代码:

#include <iostream>
#include <thread>
using namespace std;
void Threadfunc1()
{
	cout << "Threadfunc1" << endl;
}

void Threadfunc2(int vaule)
{
	cout << "Threadfunc1:" <<vaule<< endl;
}

class Task
{
public:
	void Taskfunc(int value)
	{
		cout << "Threadfunc1:" << value << endl;
	}
};

int main(int argc, char* argv[])
{
	Task task_object;
	thread thread1(Threadfunc1);
	thread thread2(Threadfunc2,3);
	//如果调用的函数是在类里面,那么首先要在函数前面加上对应的类名,还要用取地址符,获取函数的地址传递过去
	//第二个参数是类的实例化对象,如果这段代码是在类里面的化那么这个参数就是用this来代替。
	//第三个参数是传递函数需要的参数。
	thread thread3(&Task::Taskfunc, task_object, 3);
	getchar();
	return 0;
}

//如果调用的函数是在类里面,那么首先要在函数前面加上对应的类名,还要用取地址符,获取函数的地址传递过去
 //第二个参数是类的实例化对象,如果这段代码是在类里面的化那么这个参数就是用this来代替。
 //第三个参数是传递函数需要的参数。

3.thread的函数,join()和detach()的区别

join():调用了这个函数,主程序要等线程结束了自己才会结束

detach():主线程者创建的线程就分离了,主程序做完了自己的事情就会结束。创建的线程会做完了自己的事情才会结束。各干各的互不影响。

代码示例:

#include <iostream>
#include <thread>
using namespace std;
void Threadfunc1()
{
	std::this_thread::sleep_for(std::chrono::milliseconds(5000));
	cout << "Threadfunc1" << endl;
	
}

void Threadfunc2(int vaule)
{
	std::this_thread::sleep_for(std::chrono::milliseconds(1000));
	cout << "Threadfunc2:" <<vaule<< endl;
	
}

class Task
{
public:
	void Taskfunc(int value)
	{
		cout << "Threadfunc3:" << value << endl;
	}
};

int main(int argc, char* argv[])
{
	Task task_object;
	thread thread1(Threadfunc1);
	thread thread2(Threadfunc2,3);
	//如果调用的函数是在类里面,那么首先要在函数前面加上对应的类名,还要用取地址符,获取函数的地址传递过去
	//第二个参数是类的实例化对象,如果这段代码是在类里面的化那么这个参数就是用this来代替。
	//第三个参数是传递函数需要的参数。
	thread thread3(&Task::Taskfunc, task_object, 3);
	thread1.join();
	thread2.join();
	thread3.join();

	//thread1.detach();
	//thread2.detach();
	//thread3.detach();

	return 0;
}

这段程序,主程序会卡在那里,等待所有的线程执行完成。

示例2:

#include <iostream>
#include <thread>
using namespace std;
void Threadfunc1()
{
	std::this_thread::sleep_for(std::chrono::milliseconds(5000));
	cout << "Threadfunc1" << endl;
	
}

void Threadfunc2(int vaule)
{
	std::this_thread::sleep_for(std::chrono::milliseconds(1000));
	cout << "Threadfunc2:" <<vaule<< endl;
	
}

class Task
{
public:
	void Taskfunc(int value)
	{
		cout << "Threadfunc3:" << value << endl;
	}
};

int main(int argc, char* argv[])
{
	Task task_object;
	thread thread1(Threadfunc1);
	thread thread2(Threadfunc2,3);
	//如果调用的函数是在类里面,那么首先要在函数前面加上对应的类名,还要用取地址符,获取函数的地址传递过去
	//第二个参数是类的实例化对象,如果这段代码是在类里面的化那么这个参数就是用this来代替。
	//第三个参数是传递函数需要的参数。
	thread thread3(&Task::Taskfunc, task_object, 3);
	//thread1.join();
	//thread2.join();
	//thread3.join();

	thread1.detach();
	thread2.detach();
	thread3.detach();

	return 0;
}

这段程序,主程序会直接结束。但是其他的线程还是自己还执行的。

通过打印可以看到。主程序的确是自己先结束了的。主程序和创建的线程确实是分离了,不会相互阻塞

还有一个点要注意,主进程,主程序结束了。那里面的各种线程也会自己结束的。

示例代码

#include <iostream>
#include <thread>
#include <Windows.h>
using namespace std;
void Threadfunc1()
{
	std::this_thread::sleep_for(std::chrono::milliseconds(5000));
	cout << "Threadfunc1" << endl;
	
}

void Threadfunc2(int vaule)
{
	std::this_thread::sleep_for(std::chrono::milliseconds(1000));
	cout << "Threadfunc2:" <<vaule<< endl;
	
}

class Task
{
public:
	void Taskfunc(int value)
	{
		cout << "Threadfunc3:" << value << endl;
	}
};

int main(int argc, char* argv[])
{
	Task task_object;
	thread thread1(Threadfunc1);
	thread thread2(Threadfunc2,3);
	//如果调用的函数是在类里面,那么首先要在函数前面加上对应的类名,还要用取地址符,获取函数的地址传递过去
	//第二个参数是类的实例化对象,如果这段代码是在类里面的化那么这个参数就是用this来代替。
	//第三个参数是传递函数需要的参数。
	thread thread3(&Task::Taskfunc, task_object, 3);
	//thread1.join();
	//thread2.join();
	//thread3.join();

	thread1.detach();
	thread2.detach();
	thread3.detach();
	getchar();
	return 0;
}

对应输出

通过增加getchar(); 函数让主程序一直不会结束,那里面的子线程,也会一直执行。

但是使用了detach() ,让创建的线程和主程序相关独立

  • 8
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在Android的C++Thread中,调用`run()`函数后,会执行一次`threadLoop()`函数,然后退出线程。如果需要循环执行`threadLoop()`函数,需要在自定义的线程类中重写`threadLoop()`函数,并在函数中添加循环逻辑。 以下是`Thread`类中`run()`函数的部分源码: ```cpp status_t Thread::run(const char* name, int32_t priority, size_t stack) { Mutex::Autolock _l(mLock); if (mThread != 0) { return INVALID_OPERATION; } mExited = false; mNeedToExit = false; mName = name; mPriority = priority; mStackSize = stack; mRunning = true; pthread_attr_t attr; pthread_attr_init(&attr); if (stack) { pthread_attr_setstacksize(&attr, stack); } int result = pthread_create(&mThread, &attr, _threadLoop, this); mSuccessfullyStartedException = (result == 0); pthread_attr_destroy(&attr); if (result == 0) { while (!mHasStarted) { mThreadExitedCondition.wait(mLock); } } else { mThread = 0; mRunning = false; mExited = true; mSuccessfullyStartedException = false; } return result; } ``` 可以看到,`run()`函数创建了一个新的线程,并调用`_threadLoop()`函数作为新线程的入口函数。在创建完线程后,`run()`函数会一直等待直到新线程成功启动,然后返回。 而在`_threadLoop()`函数中,会不断地调用用户自定义的`threadLoop()`函数,直到该函数返回为止。因此,如果需要循环执行`threadLoop()`函数,需要在自定义的线程类中重写`threadLoop()`函数并添加循环逻辑。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值