4 ms·
Reuzel – A tiny C++ thread pool
- to3m 9y agoThe C++11 thread library (http://en.cppreference.com/w/cpp/thread http://en.cppreference.com/w/cpp/thread) is at least not insane, and at best you might even like it. In my view it's certainly a better bet than pthreads, because it runs on Windows too.
- JDevlieghere 9y agoIndeed, it's portable and part of the standard. Personally I'm a big fan of this implementation: https://github.com/progschj/ThreadPool/ https://github.com/progschj/ThreadPool/
- gcp 9y agoThanks, this looks a lot better than the one in the subject.
- gusmd 9y agoI just finished going through this book: https://www.amazon.com/C-Concurrency-Action-Practical-Multithreading/dp/1933988770 https://www.amazon.com/C-Concurrency-Action-Practical-Multit... Talks in-depth about the thread library and also other C++11 features that made it possible. Highly recommended.
- leohotfn 9y agoThanks. Why don't I use the C++11 thread library? 1, I wrote this code on Linux and it's only used on Linux, so I don't consider portability issues. 2, actually, I dislike some classes name in the C++ thread library, so....
- usefulcat 9y agoPSA: Please don't do stuff like this in header files: using std::string; It's not cool to impose that decision on clients of the library.
- blackflame7000 9y agoNote that this is not the same as "using namespace std;" since you are only exporting the namespace std::string. This should almost always be ok since no sane programmer should ever name their class string in the namespace of std. As a developer, I wouldn't feel too bad about causing namespace conflicts in this manner because hopefully it will suggest to the maintainer that their class name is a poor choice.
- sclangdon 9y agoActually, it tells the compiler that every time it comes across a class called string that isn't fully qualified, it should use std::string. And since he's put it in the global namespace, it will apply everywhere this header is visible. He really shouldn't do that in a header, but if he at least moved it down a couple of lines so it's inside his private namespace, it at least wouldn't affect anyone else. There is no good reason to do it globally.
- deleted 9y ago[deleted]
- blackflame7000 9y agoBut that begs the question why do you have two classes named string to begin with? The ambiguous name choice of string for an alternative to the standard is a terrible idea and all the problems associated with it should be the responsibility of the person making that decision. Putting using std::string in the header would at the very least make whoever is using your class strongly consider the necessity of 2 classes named string. They would be forced to use the fully qualified name for their type instead of ambiguously referring to their alternative string class as string. This prevents future maintainers from getting confused as to why there are 2 versions of the class string.
- to3m 9y agoBut "string" in std isn't ambiguous, precisely because it's in the std namespace. As to why a program would have two classes called string... well, there's a funny thing there, because it didn't, at least not until it started using this library that does "using std::string" in its header... (Anyway, why is it the library author's concern? They're writing a library! Not only is this the tail wagging the dog, but they're actually making it harder to use, and easier to say no to. Exactly the wrong thing.)
- pacaro 9y agoAnother simpleapproach to exception handling (beyond calling abort) is to allow an exception callback to be registered. This can allow appropriate diagnostic logging, and/or compensation
- leohotfn 9y agoThanks!
- deleted 9y ago[deleted]
- blackflame7000 9y agoWhat does this library offer over std::async?
- leohotfn 9y agoWhat do you mean?
- blackflame7000 9y agothe c++11 library std::async provides a built in mechanism for spawning asynchronous tasks. Under the hood, std::async uses a thread pool. So I'm asking why use this library when the stl already provides a thread-pool mechanism?
- leohotfn 9y agoI think The C++ thread library is overdesigned so it lost some function. What's more, on Linux it's chicken ribs. So I encapsulate POSIX Threads using C++, to take advantage of RAII.
- blackflame7000 9y agoGood explanation, and frankly I agree. From looking through it, I like the control your lib allows. If its performance is superior on Linux as you say, nice work!
- leohotfn 9y agoThanks! BTW, the encapsulation is not finished yet, such as mutex and condition variable. I'll go on with the work, and welcome to contribute if you're interested!