boost-thread

What's the difference between “mutex” and “lock”?

牧云@^-^@ 提交于 2019-12-02 18:09:24
I am very confused about the difference between a lock and mutex. In Boost docs, it says, Lock Types Class template lock_guard Class template unique_lock Class template shared_lock Class template upgrade_lock Class template upgrade_to_unique_lock Mutex-specific class scoped_try_lock Mutex Types Class mutex Typedef try_mutex Class timed_mutex Class recursive_mutex Typedef recursive_try_mutex Class recursive_timed_mutex Class shared_mutex In another article, I see functions like this, boost::shared_mutex _access; void reader() { boost::shared_lock< boost::shared_mutex > lock(_access); // do work

(simple) boost thread_group question

两盒软妹~` 提交于 2019-12-02 18:07:43
I'm trying to write a fairly simple threaded application, but am new to boost's thread library. A simple test program I'm working on is: #include <iostream> #include <boost/thread.hpp> int result = 0; boost::mutex result_mutex; boost::thread_group g; void threaded_function(int i) { for(; i < 100000; ++i) {} { boost::mutex::scoped_lock lock(result_mutex); result += i; } } int main(int argc, char* argv[]) { using namespace std; // launch three threads boost::thread t1(threaded_function, 10); boost::thread t2(threaded_function, 10); boost::thread t3(threaded_function, 10); g.add_thread(&t1); g

Pointer to boost::thread

淺唱寂寞╮ 提交于 2019-12-02 13:28:55
问题 I have a problem about the managed of thread. My problem is that I want to create a class ThreadManager that have to manage all thread created and of course also destroy this thread. class DerivedInterface { public: DerivedInterface():id("Test"){}; virtual ~DerivedInterface(){}; virtual void run() = 0; virtual std::string getId() = 0; const std::string id ; }; class Object : public DerivedInterface { public: Object():id("VirtualDae"){}; ~Object(){} void run() { std::cout<<"i'M IN RUN"<<std:

Boost 1.49 Condition Variable issue

落花浮王杯 提交于 2019-12-02 10:03:53
I am trying to use Boost Conditional variable in my application to synchronize two different threads as following: The main thread, will create a TCP server and instance of object called MIH-User and register a callback to an event_handler. Main.cpp /** * Default MIH event handler. * * @param msg Received message. * @param ec Error code. */ void event_handler(odtone::mih::message &msg, const boost::system::error_code &ec) { if (ec) { log_(0, __FUNCTION__, " error: ", ec.message()); return; } switch (msg.mid()) { // Source Server received HO Complete Message case odtone::mih::indication::n2n_ho

Read child process stdout in a separate thread with BOOST process

限于喜欢 提交于 2019-12-02 07:38:49
问题 I have a main program that uses boost process library to spawn a child process that prints Hello World ! on its stdout every 5 seconds. I would like to read/monitor the stdout of the child process in the main process when it becomes available along with performing other operations within the main program. I have tried out the examples for boost asynchronous IO (http://www.boost.org/doc/libs/1_66_0/doc/html/boost_process/tutorial.html) but all these seem to block the main program until the

Can't link against Boost.Thread 1.46.1 with MinGW 4.5.2

老子叫甜甜 提交于 2019-12-02 05:12:06
问题 I've built boost using: bjam --toolset=gcc --with-thread stage Whenever I'm trying to actually use Boost.Thread I'm getting undefined references although I link against it. It doesn't happen with other Boost libraries like Regex or System. >g++ main.cpp -I. -L. -lboost_thread-mgw45-mt-1_46_1 C:\Users\jhasse\AppData\Local\Temp\ccjYfDox.o:main.cpp:(.text+0xf): undefined reference to `_imp___ZN5boost6thread20hardware_concurrencyEv' collect2: ld returned 1 exit status Example program: #include

Read child process stdout in a separate thread with BOOST process

落花浮王杯 提交于 2019-12-02 04:55:32
I have a main program that uses boost process library to spawn a child process that prints Hello World ! on its stdout every 5 seconds. I would like to read/monitor the stdout of the child process in the main process when it becomes available along with performing other operations within the main program. I have tried out the examples for boost asynchronous IO ( http://www.boost.org/doc/libs/1_66_0/doc/html/boost_process/tutorial.html ) but all these seem to block the main program until the child process has exited. Do we need to read the childs stdout in a separate thread ? Can someone please

Boost.Thread throws bad_alloc exception in VS2010

寵の児 提交于 2019-12-02 04:04:43
Upon including <boost/thread.hpp> I get this exception: First-chance exception at 0x7c812afb in CSF.exe: Microsoft C++ exception: boost::exception_detail::clone_impl<boost::exception_detail::bad_alloc_> at memory location 0x0012fc3c.. First-chance exception at 0x7c812afb in CSF.exe: Microsoft C++ exception: [rethrow] at memory location 0x00000000.. I can't catch it, breaking at the memory location brings me to kernel32.dll and at this point I cannot say what's going on but it appears that the exception is thrown after the program ends and VS is capable of catching it. The testcase: #include

Can't link against Boost.Thread 1.46.1 with MinGW 4.5.2

╄→гoц情女王★ 提交于 2019-12-02 01:04:10
I've built boost using: bjam --toolset=gcc --with-thread stage Whenever I'm trying to actually use Boost.Thread I'm getting undefined references although I link against it. It doesn't happen with other Boost libraries like Regex or System. >g++ main.cpp -I. -L. -lboost_thread-mgw45-mt-1_46_1 C:\Users\jhasse\AppData\Local\Temp\ccjYfDox.o:main.cpp:(.text+0xf): undefined reference to `_imp___ZN5boost6thread20hardware_concurrencyEv' collect2: ld returned 1 exit status Example program: #include <boost/thread.hpp> #include <iostream> int main() { std::cout << boost::thread::hardware_concurrency() <<

Compiling C++ source file using Boost.Thread

谁都会走 提交于 2019-12-01 15:28:23
问题 I am trying to learn how to use the C++ Boost.Thread library. I have installed the Boost libraries on my Ubuntu 11.10 system. I am following the book "The Boost C++ Libraries" by Schaling - specifically example 6.1 on page 66. I am trying to compile the following code example: #include <boost/thread.hpp> #include <iostream> void wait(int seconds) { boost::this_thread::sleep(boost::posix_time::seconds(seconds)); } void thread() { for(int i = 0; i < 5; ++i) { wait(1); std::cout << i << std: