boost-thread

thread destructors in C++0x vs boost

人走茶凉 提交于 2019-11-27 22:13:45
问题 These days I am reading the pdf Designing MT programs . It explains that the user MUST explicitly call detach() on an object of class std::thread in C++0x before that object gets out of scope. If you don't call it std::terminate() will be called and the application will die. I usually use boost::thread for threading in C++. Correct me if I am wrong but a boost::thread object detaches automatically when it get out of scope. Is seems to me that the boost approach follow a RAII principle and the

Can multithreading speed up memory allocation?

Deadly 提交于 2019-11-27 19:09:13
I'm working with an 8 core processor, and am using Boost threads to run a large program. Logically, the program can be split into groups, where each group is run by a thread. Inside each group, some classes invoke the 'new' operator a total of 10000 times. Rational Quantify shows that the 'new' memory allocation is taking up the maximum processing time when the program runs, and is slowing down the entire program. One way I can speed up the system could be to use threads inside each 'group', so that the 10000 memory allocations can happen in parallel. I'm unclear of how the memory allocation

Getting return value from a boost::threaded member function?

不羁的心 提交于 2019-11-27 18:21:11
问题 I have a worker class like the one below: class Worker{ public: int Do(){ int ret = 100; // do stuff return ret; } } It's intended to be executed with boost::thread and boost::bind, like: Worker worker; boost::function<int()> th_func = boost::bind(&Worker::Do, &worker); boost::thread th(th_func); th.join(); My question is, how do I get the return value of Worker::Do? Thanks in advance. 回答1: I don't think you can get the return value. Instead, you can store the value as a member of Worker:

Designing a thread-safe copyable class

主宰稳场 提交于 2019-11-27 13:16:01
问题 The straightforward way to make a class threadsafe is to add a mutex attribute and lock the mutex in the accessor methods class cMyClass { boost::mutex myMutex; cSomeClass A; public: cSomeClass getA() { boost::mutex::scoped_lock lock( myMutex ); return A; } }; The problem is that this makes the class non-copyable. I can make things work by making the mutex a static. However, this means that every instance of the class blocks when any other instance is being accessed, because they all share

c++ work queues with blocking

好久不见. 提交于 2019-11-27 09:38:09
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() > ( task ) ) ); } } private: boost::asio::io_service io_service_; boost::asio::io_service::work work_;

Creating a thread pool using boost

狂风中的少年 提交于 2019-11-27 07:08:04
Is it possible to create a thread pool using boost's thread? i was looking all over boost's libs and I couldn't find a thread pool manager (or something like that)... Is there a way to do it? tnx! There is an unofficial (yet) threadpool in boost. But it's not a problem to implement one yourself especially if great genericity is not a primary goal. Idea: your threadpool can be parametrized with TaskType type and the number of workers. The TP must be given the handler function which takes TaskType. TP contains a queue of added tasks. The real thread function just takes a task from the queue and

C++ Thread Pool [closed]

最后都变了- 提交于 2019-11-27 06:20:50
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. 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 execute_with_threadpool() { // Create a thread pool. pool tp(2); // Add some tasks to the pool. tp.schedule(

How can I achieve something similar to a semaphore using boost in c++? [duplicate]

瘦欲@ 提交于 2019-11-27 02:58:01
问题 This question already has an answer here: C++0x has no semaphores? How to synchronize threads? 10 answers I noticed that boost does not seem to support semaphores. What's the easiest way to achieve a similar effect? 回答1: You either need Boost Interprocess semaphore or Boost Thread synchronization primitives. Mutex/Lock and condition are primitives that are commonly used to synchronize access to shared resources across multiple threads of a single process. There are exclusive, readers-writer

boost::asio with boost::unique_future

醉酒当歌 提交于 2019-11-27 01:41:17
问题 According to http://www.boost.org/doc/libs/1_55_0/doc/html/boost_asio/overview/cpp2011/futures.html, we can use boost::asio with std::future . But I couldn't find any information about working with boost::unique_future , which has more functions, such as then() . How can I use? 回答1: Boost.Asio only provides first-class support for asynchronous operations to return a C++11 std::future or an actual value in stackful coroutines. Nevertheless, the requirements on asynchronous operations documents

How to pass an argument to boost::thread?

怎甘沉沦 提交于 2019-11-27 01:12:52
问题 thread_ = boost::thread( boost::function< void (void)>( boost::bind( &clientTCP::run , this ) ) ); is it possible that run has an argument like this : void clientTCP::run(boost:function<void(std::string)> func); and if yes how my boost::thread call should be written Thanks. 回答1: The following code boost::bind( &clientTCP::run , this ) defines a function callback. It calls the function run on the current instance ( this ). With boost::bind you can do the following: // Pass pMyParameter through