I have a boost thread program from a tutorial, i can compile it with no problems or warnings but when i run it i don't get any output form eclipse. The program termiates a once. I'm using MiNGW with my eclipse could this be the problem?
Does anybody have a idea? All sugestions are welcome!
Below is the tutorial code that i used, its from this site:
#include <iostream>
#include <boost/thread.hpp>
#include <boost/date_time.hpp>
void workerFunc()
{
boost::posix_time::seconds workTime(10000);
std::cout << "Worker: running" << std::endl;
// Pretend to do something useful...
boost::this_thread::sleep(workTime);
std::cout << "Worker: finished" << std::endl;
}
int main(int argc, char* argv[])
{
std::cout << "main: startup" << std::endl;
boost::thread workerThread(workerFunc);
std::cout << "main: waiting for thread" << std::endl;
workerThread.join();
std::cout << "main: done" << std::endl;
return 0;
}
Update
I have linked pthreads under mingw linker and the program compiles but the result is the same. When i put i breakpoiiny just below main i get this:
The target endianness is set automatically (currently little endian)
[New Thread 4168.0xbf8]
And the program terminates does anybody have idea?
You need to make sure you're linking in both the boost_thread and pthread libraries.
Related
I am running Visual Studio 2012 and attempting to learn how std::async works. I have created a very simple C++ console application:
#include "stdafx.h"
#include <future>
#include <iostream>
void foo() {
std::cout << "foo() thread sleep" << std::endl;
std::this_thread::sleep_for(std::chrono::seconds(5));
std::cout << "foo() thread awake" << std::endl;
}
int main()
{
std::future<void> res = std::async(std::launch::async, foo);
res.get();
std::cout << "MAIN THREAD" << std::endl;
system("pause");
return 0;
}
My initial expectation was to see "MAIN THREAD" printout appearing before "foo() thread awake" since the two threads are running asynchronously, with the foo() trailing behind due to its sleeping behavior. However, that is not what is actually happening. The call to res.get() blocks until foo() wakes up, and only then does it get to the "MAIN THREAD" printout. This is indicative of a synchronous behavior, so I am wondering what if perhaps I am either missing something, or not fully grasping the implementation. I have looked through numerous posts on this matter, but still cannot make any sense of it. Any help would be appreciated!
res.get();
blocks until the async is done.
http://en.cppreference.com/w/cpp/thread/future/get
Regardless of how you tell it to run, get can't give you the results until it's done.
Well, this is how std::future::get works - it blocks until future has some result or exception to provide.
that doesn't mean that async works synchronously, it is working asynchronously, it's only because you block the thread which waits on the result.
the idea was to to launch some task asynchronously, do something meanwhile and only call get when you need the result, as you might figured out, it is not the most scale-able thing..
if you use Visual Studio 2015, you can access the await keyword both for std::future and concurrency::task (Microsoft PPL library) , and for your own compatible defined types. this achieves non-blocking behavior.
#include "stdafx.h"
#include <future>
#include <iostream>
void foo() {
std::cout << "foo() thread sleep" << std::endl;
std::this_thread::sleep_for(std::chrono::seconds(5));
std::cout << "foo() thread awake" << std::endl;
}
std::future<void> entry(){
await std::async(std::launch::async, foo);
std::cout << "foo has finished, back in entry()\n";
}
int main()
{
std::cout << "MAIN THREAD" << std::endl;
entry();
std::cout << "BACK INMAIN THREAD" << std::endl;
system("pause");
return 0;
} //make sure to compile with /await flag
The problem is that res.get() has to wait for its thread to finish before getting its result (if any). To see the concurrency in motion you need to move the get() to after the other code that you want to run at the same time.
This example may make it a little clearer:
#include <ctime>
#include <cstdlib>
#include <future>
#include <iostream>
void foo(int id) {
std::cout << "foo(" << id << ") thread sleep" << std::endl;
// random sleep
std::this_thread::sleep_for(std::chrono::seconds(std::rand() % 10));
std::cout << "foo(" << id << ") thread awake" << std::endl;
}
int main()
{
std::srand(std::time(0));
std::future<void> res1 = std::async(std::launch::async, foo, 1);
std::future<void> res2 = std::async(std::launch::async, foo, 2);
std::future<void> res3 = std::async(std::launch::async, foo, 3);
std::cout << "MAIN THREAD SLEEPING" << std::endl;
std::this_thread::sleep_for(std::chrono::seconds(20));
std::cout << "MAIN THREAD AWAKE" << std::endl;
// now wait for all the threads to end
res1.get();
res2.get();
res3.get();
system("pause");
return 0;
}
I'm using boost 1.54.0 and Visual Studio 2010. For the code:
#include <iostream>
#include "boost/thread/thread.hpp"
#include "boost/thread/mutex.hpp"
boost::mutex mx1;
void func1()
{
{
boost::mutex::scoped_lock(mx1);
std::cout << "Thread " << boost::this_thread::get_id() << " starting work." << std::endl;
}
int x = 0;
for (int i=0; i<100; i++)
x++;
{
boost::mutex::scoped_lock(mx1);
std::cout << "Thread " << boost::this_thread::get_id() << " finished." << std::endl;
}
}
int main(void)
{
boost::thread thread1(&func1);
boost::thread thread2(&func1);
thread1.join();
thread2.join();
return 0;
}
About half the time I get the following (with varying thread ids and execution order, obviously):
Thread Thread 15b0 starting work.
1a18 starting work.
Thread 15b0 finished.
Thread 1a18 finished.
...instead of this (which is what I'd expect):
Thread 15b0 starting work.
Thread 1a18 starting work.
Thread 15b0 finished.
Thread 1a18 finished.
However, using
mx1.lock();
std::cout << "Thread " << boost::this_thread::get_id() << " starting work." << std::endl;
mx1.unlock();
...seems to work with no problems.
The output always seems to follow the same pattern. Am I using the mutex incorrectly, or is it something to do with std::cout?
Replace
boost::mutex::scoped_lock(mx1);
with
boost::mutex::scoped_lock lock(mx1);
you fell a victim of the most frequently occurring typo with the scoped lock:-)
I'm working with a very simply boost sample on Windows, and I'm running into several strange issues.
Here's the program:
// BoostThreadTest.cpp : Defines the entry point for the console application.
//
#define BOOST_ALL_NO_LIB
#include "stdafx.h"
#include <iostream>
#include <boost/thread.hpp>
#include <boost/date_time.hpp>
void workerFunc()
{
boost::posix_time::seconds workTime(3);
std::cout << "Worker: running" << std::endl;
boost::this_thread::sleep(workTime);
std::cout << "Worker: finished" << std::endl;
}
int _tmain(int argc, _TCHAR* argv[])
{
std::cout << "main: startup" << std::endl;
boost::thread workerThread(workerFunc);
std::cout << "main: waiting for thread" << std::endl;
boost::posix_time::seconds workTime(10);
boost::this_thread::sleep(workTime);
//workerThread.join();
std::cout << "main: done" << std::endl;
return 0;
}
The main issue is that the boost::this_thread::sleep call in the workerFunc is not actually sleeping, it is returning immediately. I also get a generic exception in the debugger when I attempt to join with the thread. The really strange thing is that a call to boost::this_thread::sleep in the main method works fine!
Does anyone have any clue what the issue might be?
I am on Windows 7, using boost 1_53_0. I built the boost thread library as a static library and am linking it in with my application built using Visual Studio 2008.
I was following this beginner tutorial on boost threads:
http://www.codeproject.com/Articles/279053/How-to-get-started-using-Boost-threads
Everything was going fine with this sample they provided:
#define BOOST_THREAD_USE_LIB
#include <iostream>
#include <boost/thread.hpp>
#include <boost/date_time.hpp>
void workerFunc()
{
boost::posix_time::seconds workTime(3);
std::cout << "Worker: running" << std::endl;
// Pretend to do something useful...
boost::this_thread::sleep(workTime);
std::cout << "Worker: finished" << std::endl;
}
int main(int argc, char* argv[])
{
std::cout << "main: startup" << std::endl;
boost::thread workerThread(workerFunc);
std::cout << "main: waiting for thread" << std::endl;
workerThread.join();
std::cout << "main: done" << std::endl;
return 0;
}
But then I decided to try it without the sleep function. So I commented out those lines.
#define BOOST_THREAD_USE_LIB
#include <iostream>
#include <boost/thread.hpp>
#include <boost/date_time.hpp>
void workerFunc()
{
//boost::posix_time::seconds workTime(3);
std::cout << "Worker: running" << std::endl;
// Pretend to do something useful...
//boost::this_thread::sleep(workTime);
std::cout << "Worker: finished" << std::endl;
}
int main(int argc, char* argv[])
{
std::cout << "main: startup" << std::endl;
boost::thread workerThread(workerFunc);
std::cout << "main: waiting for thread" << std::endl;
workerThread.join();
std::cout << "main: done" << std::endl;
return 0;
}
I started to get the following compile error:
)]+0x40)||undefined reference to `_gmtime32'|
I've done quite a bit of snooping around trying to figure out what this means and why it is only happening when I remove those two lines. As of right now, I am leaning towards it being something to do with some kind of header, like time.h, that I have to include (although I tried that already obviously).
I am using a rather strange setup. Code::Blocks with mingw/gcc. I compiled the boost library myself with command line arguments following the codeblocks tutorial:
http://wiki.codeblocks.org/index.php?title=BoostWindowsQuickRef
Everything seemed to work fine with this, but I suppose I could have built the libraries incorrectly.
I would dig deeper but the file name of the error is not standard and is listed as ")]+0x40)". I am not sure what this means - is it maybe some kind of file location address?
MORE INFO:
Windows XP 32 bit
CodeBlocks 10.05
MingW GCC 4.4.3
Boost 1_53_0
BUILD LOG:
Linking console executable: bin\Debug\Bjarne_Strousup_Samples.exe
....\CodeBlocks\lib\libboost_thread-mgw44-mt-1_53.a(thread.o):thread.cpp:(.text$_ZN5boost9date_time6c_time6gmtimeEPKlP2tm[boost::date_time::c_time::gmtime(long
const*, tm*)]+0x40): undefined reference to `_gmtime32' collect2: ld
returned 1 exit status Process terminated with status 1 (0 minutes, 6
seconds) 1 errors, 0 warnings
COMMAND LINE ATTEMPT:
C:\CodeBlocks Tests\BoostExamples>g++ main.cpp -lboost_thread -lboost_system -lb
oost_chrono
main.cpp:5:28: error: boost/thread.hpp: No such file or directory
main.cpp:6:31: error: boost/date_time.hpp: No such file or directory
main.cpp: In function 'int main()':
main.cpp:21: error: 'boost' has not been declared
main.cpp:21: error: expected ';' before 'workerThread'
main.cpp:24: error: 'workerThread' was not declared in this scope
C:\CodeBlocks Tests\BoostExamples>
LINKER SETTINGS:
IDM Link:
http://pic.dhe.ibm.com/infocenter/aix/v6r1/index.jsp?topic=%2Fcom.ibm.aix.basetechref%2Fdoc%2Fbasetrf1%2Fctime.htm
I think the error is on this line of c_time.hpp:
static std::tm* gmtime(const std::time_t* t, std::tm* result)
Attempting to build a standard Boost::thread example I found on the internet, I get multiple errors thrown by the Boost header file thread_data.hpp, which I don't link to directly but which I presume gets linked by Boost. (I also get the same errors in my actual program, but I am using the example code to ensure it's not a problem with my code.)
Here is the example code I found in a boost::thread tutorial:
#include <iostream>
#include <boost/thread.hpp>
#include <boost/date_time.hpp>
void workerFunc()
{
boost::posix_time::seconds workTime(3);
std::cout << "Worker: running" << std::endl;
// Pretend to do something useful...
boost::this_thread::sleep(workTime);
std::cout << "Worker: finished" << std::endl;
}
int main(int argc, char* argv[])
{
std::cout << "main: startup" << std::endl;
boost::thread workerThread(workerFunc);
std::cout << "main: waiting for thread" << std::endl;
workerThread.join();
std::cout << "main: done" << std::endl;
return 0;
}
Here is my build command:
mingw32-g++.exe -LC:\projects\boost\lib -o bin\Debug\Guardian.exe obj\Debug\Scratch.o -lboost_filesystem-mgw47-mt-1_53 -lboost_date_time-mgw47-mt-1_53 -lboost_system-mgw47-mt-1_53 -lboost_thread-mgw47-mt-1_53
(Notice that I am linking the boost.thread library.)
Here is the first error thrown:
C:\projects\boost\include\boost-1_53\boost\thread\win32\thread_data.hpp|123|undefined reference to `_imp___ZTVN5boost6detail16thread_data_baseE'|
In CodeBlocks I get pointed to line 123 of the header file thread_data.hpp as the source of the error:
//#if defined BOOST_THREAD_PROVIDES_INTERRUPTIONS
, interruption_handle(create_anonymous_event(detail::win32::manual_reset_event,detail::win32::event_initially_reset))
, interruption_enabled(true)
Have I forgotten to link a library? I've been using Boost without problems, until now I'm trying to use the thread library. I'm new to Boost and don't know what could be causing the error.
Try add -lpthread to the linker.