Created
September 14, 2019 17:34
-
-
Save informramiz/9d9dbb49917fe99165c6cd98994b3031 to your computer and use it in GitHub Desktop.
How to pass data to threads
This file contains hidden or 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 <iostream> | |
| #include <thread> | |
| #include <list> | |
| #include <vector> | |
| #include <string> | |
| struct ImageProcessor { | |
| void process(std::string path) { | |
| std::cout << "image processed" << std::endl; | |
| } | |
| }; | |
| class MyHelper { | |
| public: | |
| MyHelper(std::string folderName): folderName(folderName) {} | |
| void operator()() { | |
| //read folder1 | |
| std::vector<std::string> images; | |
| ImageProcessor processor; | |
| for (auto image : images) { | |
| processor.process(image); | |
| } | |
| //write back to where you want | |
| std::cout << "thread2 finished successfully" << std::endl; | |
| } | |
| private: | |
| std::string folderName; | |
| }; | |
| void someFunction() { | |
| //todo | |
| } | |
| int main() { | |
| someFunction(); //calling this function or object | |
| std::string folder1, folder2; | |
| std::thread thread1([folder1] { | |
| //read folder1 | |
| std::vector<std::string> images; | |
| ImageProcessor processor; | |
| for (auto image : images) { | |
| processor.process(image); | |
| } | |
| //write back to where you want | |
| std::cout << "Thread1 finished successfully" << std::endl; | |
| }); | |
| std::thread thread2{ MyHelper(folder1) }; | |
| thread1.join(); | |
| thread2.join(); | |
| MyHelper myHelper(folder1); | |
| myHelper(); | |
| std::thread thread3(myHelper); | |
| thread3.join(); | |
| return 0; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment