boost-thread

What’s the best way to delete boost::thread object right after its work is complete?

浪子不回头ぞ 提交于 2019-11-26 23:27:57
问题 I create boost::thread object with a new operator and continue without waiting this thread to finish its work: void do_work() { // perform some i/o work } boost::thread *thread = new boost::thread(&do_work); I guess, it’s necessary to delete thread when the work is done. What’s the best way to this without explicitly waiting for thread termination? 回答1: The boost::thread object's lifetime and the native thread's lifetime are unrelated. The boost::thread object can go out of scope at any time.

Can I create a software watchdog timer thread in C++ using Boost Signals2 and Threads?

余生颓废 提交于 2019-11-26 23:15:55
问题 I am running function Foo from somebody else's library in a single-threaded application currently. Most of the time, I make a call to Foo and it's really quick, some times, I make a call to Foo and it takes forever. I am not a patient man, if Foo is going to take forever, I want to stop execution of Foo and not call it with those arguments. What is the best way to call Foo in a controlled manner (my current environment is POSIX/C++) such that I can stop execution after a certain number of

How to use lock_guard when returning protected data

雨燕双飞 提交于 2019-11-26 21:29:15
问题 I have a question concerning the use of boost::lock_guard (or similar scoped locks) and using variables that should be protected by the lock in a return statement. How is the order of destroying local objects and copying the return value? How does return value optimization affect this? Example: Data Class::GetData() { boost::lock_guard<boost::mutex> lock(this->mMutex); return this->mData; } Would this be correct (if mData is the variable protected by mMutex)? Or would I have to use a local

c++ work queues with blocking

天涯浪子 提交于 2019-11-26 14:50:02
问题 This question should be a little simpler than my last few. I've implemented the following work queue in my program: Pool.h: // tpool class // It's always closed. :glasses: #ifndef __POOL_H #define __POOL_H class tpool { public: tpool( std::size_t tpool_size ); ~tpool(); template< typename Task > void run_task( Task task ){ boost::unique_lock< boost::mutex > lock( mutex_ ); if( 0 < available_ ) { --available_; io_service_.post( boost::bind( &tpool::wrap_task, this, boost::function< void() > (

C++ Thread Pool [closed]

人盡茶涼 提交于 2019-11-26 11:57:45
问题 What is a good open source implementation of a thread pool for C++ to use in production code (something like boost)? Please provide either your own example code or a link to example code usage. 回答1: I think it is still not accepted into Boost, but a good staring point: threadpool. Some example of usage, from the web site: #include "threadpool.hpp" using namespace boost::threadpool; // Some example tasks void first_task() { ... } void second_task() { ... } void third_task() { ... } void

How to create a thread pool using boost in C++?

不问归期 提交于 2019-11-26 06:45:45
How do I create a thread pool using boost in C++, and how do I assign tasks to the threadpool? Jeroen Bollen The process is pretty simple. First create an asio::io_service and a thread_group. Fill the thread_group with threads linked to the io_service. Assign tasks to the threads using the boost::bind function. To stop the threads (usually when you are exiting your program) just stop the io_service and join all threads. You should only need these headers: #include <boost/asio/io_service.hpp> #include <boost/bind.hpp> #include <boost/thread/thread.hpp> here is an example: /* * Create an asio:

C++0x has no semaphores? How to synchronize threads?

纵然是瞬间 提交于 2019-11-26 01:23:29
问题 Is it true that C++0x will come without semaphores? There are already some questions on Stack Overflow regarding the use of semaphores. I use them (posix semaphores) all the time to let a thread wait for some event in another thread: void thread0(...) { doSomething0(); event1.wait(); ... } void thread1(...) { doSomething1(); event1.post(); ... } If I would do that with a mutex: void thread0(...) { doSomething0(); event1.lock(); event1.unlock(); ... } void thread1(...) { event1.lock();

Example for boost shared_mutex (multiple reads/one write)?

…衆ロ難τιáo~ 提交于 2019-11-25 23:33:27
问题 I have a multithreaded app that has to read some data often, and occasionally that data is updated. Right now a mutex keeps access to that data safe, but it\'s expensive because I would like multiple threads to be able to read simultaneously, and only lock them out when an update is needed (the updating thread could wait for the other threads to finish). I think this is what boost::shared_mutex is supposed to do, but I\'m not clear on how to use it, and haven\'t found a clear example. Does