Last active
April 21, 2020 11:38
-
-
Save yasukei/49b607f3d975a2961c36b0cb9b61d8ee to your computer and use it in GitHub Desktop.
std::thread
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <cstdio> | |
#include <string> | |
#include <thread> | |
class SomeClass | |
{ | |
public: | |
SomeClass(std::string threadName, int loopNum) : | |
_threadName(threadName), | |
_loopNum(loopNum) | |
{ | |
} | |
static void run(void* arg) | |
{ | |
SomeClass* self = (SomeClass*)arg; | |
for (int i = 0; i < self->_loopNum; i++) | |
{ | |
printf("threadName: [%s], i: [%d]\n", self->_threadName.c_str(), i); | |
} | |
} | |
private: | |
std::string _threadName; | |
int _loopNum; | |
}; | |
int main() | |
{ | |
SomeClass instance1("thread1", 5); | |
SomeClass instance2("thread2", 10); | |
std::thread thread1(SomeClass::run, &instance1); | |
std::thread thread2(SomeClass::run, &instance2); | |
printf("thread1.joinable(): [%d]\n", thread1.joinable()); | |
printf("thread2.joinable(): [%d]\n", thread2.joinable()); | |
printf("before join\n"); | |
thread1.join(); | |
thread2.join(); | |
printf("after join\n"); | |
return 0; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
thread1.joinable(): [1] | |
thread2.joinable(): [1] | |
before join | |
threadName: [thread1], i: [0] | |
threadName: [thread1], i: [1] | |
threadName: [thread1], i: [2] | |
threadName: [thread1], i: [3] | |
threadName: [thread1], i: [4] | |
threadName: [thread2], i: [0] | |
threadName: [thread2], i: [1] | |
threadName: [thread2], i: [2] | |
threadName: [thread2], i: [3] | |
threadName: [thread2], i: [4] | |
threadName: [thread2], i: [5] | |
threadName: [thread2], i: [6] | |
threadName: [thread2], i: [7] | |
threadName: [thread2], i: [8] | |
threadName: [thread2], i: [9] | |
after join | |
続行するには何かキーを押してください . . . |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment