Boost thread c2064 error when trying to compile - c++

I'm quite new to the stack overflow, in fact this is my first post, So hello everyone. So let's get to the point.
Using boost library thread ver. 1.54.0
Using VS2010 32 Bit - Professional
I have built the libraries for the boost thread,
not using precompiled headers in vs C++ settings,
linked the library to the project,
here is the code
#include <boost\thread\thread_only.hpp>
#include <iostream>
#include <conio.h>
#pragma comment(lib, "libboost_thread-vc100-mt-gd-1_54.lib")
#define BOOST_LIB_NAME libboost_thread-vc100-mt-gd-1_54.lib
struct callable
{
void blah();
};
void callable::blah()
{
std::cout << "Threading test !\n";
}
boost::thread createThread()
{
callable x;
return boost::thread(x);
}
int main()
{
createThread();
_getch();
return 0;
}
after all this I get this error
Error 1 error C2064: term does not evaluate to a function taking 0 arguments ..\..\boost_1_54_0\boost\thread\detail\thread.hpp 117 1 BoostTrial
Could you help me to get this example to work. Reason why I am using this example is because I have another app which has been set up exactly the same way and it's not working because of this error :-( my goal is to get the multithreading to work and then I can take it from there.
Thanks for your time.

You need to implement operator() in callable.
Don't forget to either join() or detach() thread to prevent abnormal program termination.
See boost::thread tutorial for more examples.
#include <boost\thread\thread_only.hpp>
#include <iostream>
#pragma comment(lib, "libboost_thread-vc100-mt-gd-1_54.lib")
using namespace boost;
struct callable
{
void operator()()
{
std::cout << "Threading test !\n";
}
};
boost::thread createThread()
{
callable x;
return boost::thread(x);
}
int main()
{
boost::thread th = createThread();
th.join();
}
Example with std::thread;

Related

gcc giving error when initiating thread class

I've been having an error declaring a thread class object. I tried this code and it gave me the error "'thread' was not declared in this scope". If it helps, I'm compiling to Windows using MinGW GCC.
#include <iostream>
#include <thread>
using namespace std;
void func(){
cout << "Hello from thread 2\n";
}
int main(){
cout << "Hello from thread 1\n";
thread t2(func);
t2.join();
return 0;
}
Compiling gives me "error: 'thread' was not declared in this scope"
Threads are missing in mingw, but you can use some library like this one mingw-std-threads.

Implementing Boost.thread

I am learning boost c++ and thus I was trying this piece of code which deals with boost threads
#include <boost/thread.hpp>
#include <boost/chrono.hpp>
#include <iostream>
void wait(int seconds)
{
boost::this_thread::sleep_for(boost::chrono::seconds(seconds));
}
void thread()
{
for(int i = 0; i < 5; i++)
{
wait(2);
std::cout << i << std::endl;
}
}
int main()
{
boost::thread t(thread);
t.join();
return 0;
}
But when I compile this code on VS2008 I get the following errors:
LINK : fatal error LNK1104: cannot open file 'boost_thread-vc90-mt-1_58.lib'
What I understood is this error is due to configuration issues but I don't know how to get rid of it. Can somebody help me in solving this error?
First of all you need to compile boost (if you didn't).
Secondly you need to properly add this library to project, look at this answer.

boehm-gc with C++11's thread library

As we know, using boehm-gc in multi-thread requires calling GC_register_my_thread with stack base from GC_get_stack_base. but It seems not to work well with C++11's thread library, such as std::thread... How can I use boehm-gc with C++11's thread library?
(I use VS2013)
edit: This is tested code. std::thread is good, but std::future doesn't work (stop on _CrtIsValidHeapPointer
#include <iostream>
#include <thread>
#include <future>
#define GC_THREADS
#include <gc.h>
#include <gc_cpp.h>
#pragma comment(lib, "gcmt-lib")
void foo()
{
GC_stack_base sb;
GC_get_stack_base(&sb);
GC_register_my_thread(&sb);
int *ptr;
for (int i = 0; i < 10; i++)
{
ptr = new (GC) int;
*ptr = 1;
}
GC_unregister_my_thread();
}
int main()
{
GC_INIT();
GC_allow_register_threads();
std::cout << "test for std::thread";
std::thread thrd(foo);
thrd.join();
std::cout << " [sucs]\n";
std::cout << "test for std::future";
std::future<void> fu = std::async(std::launch::async, foo);
fu.get();
std::cout << " [sucs]\n";
std::cin.get();
}
edit: here is a capture of stack trace (Sorry that it isn't English, but I think it doesn't matter, anyway)
and here is a debug message
HEAP[TestGC.exe]: Invalid address specified to RtlValidateHeap( 00E80000, 00C92F80 )
While debugging, I found The error occurs after fu.get().
edit: The error doesn't occur with /MD(or /MDd)...
(I think GC might touch library's pointers (namespcae Concurrency), but it is just guess;;)
Before you start using the collector and before you create the threads make sure that you issue both
GC_INIT, and
GC_allow_register_threads
Then in every thread follow it up with,
GC_get_stack_base/GC_register_my_thread, and eventually
GC_unregister_my_thread.
You didn't say what you are compiling with but it works for gcc 4.8 (with -std=c++11).
EDIT: The OP was able to resolve the issue by addressing the instruction above and compiling the code with the /MD[d] flags for the multi-threaded dynamic MSVCR100 runtime. The issue remained unresolved for the multithreaded statically compiled runtime.

Cross platform way to prevent high cpu usage in while loop (Without boost)?

I have a server that I want to run, and it uses a cross platform library that only gives me a tick() to call:
int main()
{
Inst inst;
while(true)
{
inst.tick();
}
}
I need to try to lower the cpu usage so that it doesnt constantly take up 1 core.
Is there a simple way to do this without boost?
Thanks
#include <iostream>
#include <thread>
#include <chrono>
using namespace std;
int main()
{
//5 seconds
auto duration = chrono::duration<float>(5);
this_thread::sleep_for(duration);
return 0;
}
However, even if this code is completely fine, I can't seem to compile it with the provided MinGW compiler from Code::Blocks.

Freezing feelings towards Visual Studio

This code freezes VS2010 sp1:
// STC_14_1.cpp : Defines the entry point for the console application.
//
//#include "stdafx.h"
#include <exception>
#include <iostream>
#include <cstdlib>
using std::cerr;
using std::cout;
using std::cin;
void my_new_handler()
{
cerr << "Mem. alloc failed.";
std::exit(-1);
}
//std::unexpected_handler std::set_unexpected(std::unexpected_handler);
class STC
{
std::new_handler old;
public:
STC(std::new_handler n_h):old(std::set_new_handler(n_h))
{ }
~STC()
{
std::set_unexpected(old);
}
};
int main(int argc, char* argv[])
{
STC stc(&my_new_handler);
while (true)
{
auto tmp = new int[50000];
}
return 0;
}
Is it that I'm doing something wrong or it's VS's problem?
Your loop is endless:
while (true)
{
auto tmp = new int[50000];
}
You have to define a condition to exit outside of the loop. In counterpart, VS will be frozen iterating through the loop and draining memory from the heap (since you allocate a new block of memory in every iteration).
EDIT: Your handler is not called because it has to be defined as void __cdecl:
void __cdecl no_memory () {
cout << "Failed to allocate memory!\n";
exit (1);
}
Since handler is not called, the problem is in endless loop.
It works on my VS 2010.
When you say 'freezes', are you sure that it's not just that the code is still actually running and has not hit the new handler code yet. I tried running the example set_new_handler code from the MSDN here, and it still took a minute or so and the example is allocating 5000000 at a time rather than 50000.