????????
?????????????C++?????????????????? ??????????????????ù??????????????? ????????????????Щ?????????????????д???ī?????ó??????ó?????
????????д??????????????????????????????????? ??????????????????á???????????????????á??????????
?????泯????
??????????????γ???????????????????????????????д????
?????: ??δ?????????????????? limonp-thread-pool-programming-example ?? ?????????????????????????????????????鱧?????????????????????????????????? GitHub ???????? ??????????й????? GitHub ??????????????????ζ??????????????????????make???Ρ?
????git clone https://github.com/yanyiwu/practice
????cd practice/cpp/limonp-v0.5.1-demo
????make
#include "limonp/ThreadPool.hpp"
#include "limonp/StdExtension.hpp"
using namespace std;
const size_t THREAD_NUM = 4;
// ???????????????壬?????????????????д??????????
class Foo {
public:
// ???????????Append??????????б???????????????? chars ???????????????д???????????????????????????
void Append(char c) {
limonp::MutexLockGuard lock(mutex_);
chars.push_back(c);
}
string chars; // ????????????
limonp::MutexLock mutex_; // ?????
};
void DemoClassFunction() {
Foo foo;
cout << foo.chars << endl;
// ?????????????
limonp::ThreadPool thread_pool(THREAD_NUM);
thread_pool.Start(); // ???????
for (size_t i = 0; i < 20; i++) {
char c = i % 10 + '0';
// ??? NewClosure ?? foo ????? Append ????????????????????????????????????У???????? NewClosure ???????
thread_pool.Add(limonp::NewClosure(&foo?? &Foo::Append?? c));
}
thread_pool.Stop(); // ??????????????????????NewClosure????????????????????????????????
cout << foo.chars << endl;
}
int main() {
DemoClassFunction();
return 0;
}
??????????????????????????·???????????????????? NewClosure ????????????? ????????????????????????????????????????????????JavaScript?????? ????????汾??????????????????????????????????
????limonp::NewClosure(&foo?? &Foo::Append?? c)
????????????????????????????????????????????????????????????????????int???????????? ????????????????????
????????????
???????????????????????????????????
????git clone https://github.com/yanyiwu/practice
????cd practice/cpp/limonp-v0.5.1-demo
????make
?????????????????????????????????????? limonp-0.5.1 ??????? ????????
????limonp-0.5.1/include/limonp/Closure.hpp
????????п??? NewClosure ????????壬NewClosure ???????庯??????????????????????????? ?????????????????
???????? NewClosure ?????????????????????????? ClosureInterface ??????????????
????????? ClosureInterface ???????????£?
????class ClosureInterface {
????public:
????virtual ~ClosureInterface() {
????}
????virtual void Run() = 0;
????};
???????????? Closure ????????????????????????????????С? ????????????????????л??Closure?????????? Run() ??????
?????????NewClosure ????????????????????????????????????????????????????? ??????????й??? ???????????????????У??????? Closure ?е? Run() ??????? ?? delete ??????????????????й???
????????????????????????????? Run() ????????? ????????????′??? Append ?????????????????????? Run() ?????? Closure ????
????limonp::NewClosure(&foo?? &Foo::Append?? c)
??????? NewClosure ????庯??????????????????????????????????????????? NewClosure ??????????£?
????template<class R?? class Obj?? class Arg1>
????ClosureInterface* NewClosure(Obj* obj?? R (Obj::* fun)(Arg1)?? Arg1 arg1) {
????return new ObjClosure1<Obj?? R (Obj::* )(Arg1)?? Arg1>(obj?? fun?? arg1);
????}