Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <thread>
- #include <string>
- #include <mutex>
- #include <memory>
- #include <vector>
- std::mutex consoleMtx;
- void writeString(const std::string &string)
- {
- consoleMtx.lock();
- std::cout << string << std::endl;
- consoleMtx.unlock();
- }
- std::vector<std::thread> startThreads()
- {
- std::shared_ptr<std::string> sharedString(new std::string("I can't believe you've done this")); // When does this string get destroyed? Don't know, don't care.
- std::vector<std::thread> tp;
- for(unsigned char i = 0; i < 4; ++i)
- {
- tp.emplace_back([](std::shared_ptr<std::string> string){writeString(*string);}, sharedString);
- }
- return tp;
- }
- int main()
- {
- auto tp = startThreads();
- for(auto &t : tp)
- {
- t.join();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement