티스토리 뷰
반응형
Start thread with member function(람다 이용)
출처[http://stackoverflow.com/questions/10673585/start-thread-with-member-function]
Since you are using C++11, lambda-expression is a nice&clean solution.
class blub {
void test() {}
public:
std::thread spawn() {
return std::thread( [this] { this->test(); } );
}
};
since this->
can be omitted, it could be shorten to:
std::thread( [this] { test(); } )
or just
std::thread( [=] { test(); } )
======================================================
#include <thread>
#include <iostream>
class Wrapper {
public:
void member1() {
std::cout << "i am member1" << std::endl;
}
void member2(const char *arg1, unsigned arg2) {
std::cout << "i am member2 and my first arg is (" << arg1 << ") and second arg is (" << arg2 << ")" << std::endl;
}
std::thread member1Thread() {
return std::thread([=] { member1(); });
}
std::thread member2Thread(const char *arg1, unsigned arg2) {
return std::thread([=] { member2(arg1, arg2); });
}
};
int main(int argc, char **argv) {
Wrapper *w = new Wrapper();
std::thread tw1 = w->member1Thread();
std::thread tw2 = w->member2Thread("hello", 100);
tw1.join();
tw2.join();
return 0;
}
Compiling with g++ produces the following result
g++ -Wall -std=c++11 hello.cc -o hello -pthread
i am member1
i am member2 and my first arg is (hello) and second arg is (100)
반응형
'DEV > C&C++' 카테고리의 다른 글
gdb, gcore를 통한 강제 core dump 방법 (0) | 2018.06.01 |
---|---|
[Cygwin]How to Install the Latest GCC on Windows (0) | 2017.04.14 |
terminate called without an active exception (0) | 2016.10.11 |
Makefile 옵션 (0) | 2016.09.28 |
[UNICODE]_tprintf 출력 시 문자가 깨지는 문제 (0) | 2016.08.23 |
댓글
반응형
공지사항
최근에 올라온 글
최근에 달린 댓글
- Total
- Today
- Yesterday
링크
TAG
- lvm 늘리기
- 센트6
- postgresql11
- 파티션 추가
- CMAKE_OSX_SYSROOT
- ld 옵션
- mirrorlist
- pfctl
- lvremove
- _status
- physicaldisk
- linux
- CLion
- install
- centos7
- Repository
- aix compile
- firewall-cmd service
- fielddata
- noipath
- CentOS
- yum
- Ignoring CMAKE_OSX_SYSROOT value
- update
- 물리디스크
- aix ld
- vgcreate
- virtualbox
- _stats
- pf.anchors
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | |||
5 | 6 | 7 | 8 | 9 | 10 | 11 |
12 | 13 | 14 | 15 | 16 | 17 | 18 |
19 | 20 | 21 | 22 | 23 | 24 | 25 |
26 | 27 | 28 | 29 | 30 | 31 |
글 보관함