OS: Win7
IDE: Visual Studio 2010
Boost Version: 1.47
I’m new to Boost and what I’m trying is very simple. I’ve created a single thread in a header file and tried putting it to sleep. But I can’t get it working. Here is the code and compilation errors
main.h –
#pragma once
#include <conio.h>
#include <boost\thread.hpp>
boost::posix_time::seconds workTime ( 120 );
boost::this_thread::sleep ( workTime );
main.cpp
#include "main.h"
void main ( void ) {
_getch();
};
Output –
error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
error C2365: 'boost::this_thread::sleep' : redefinition; previous definition was 'function'
error C2491: 'boost::this_thread::sleep' : definition of dllimport data not allowed
error C2482: 'boost::this_thread::sleep' : dynamic initialization of 'thread' data not allowed
Using the following code now, all in main.cpp:
#include <boost\thread.hpp>
#include <conio.h>
void thread_func()
{
boost::posix_time::seconds workTime ( 120 );
boost::this_thread::sleep ( workTime );
}
int main(int argc, char* argv[])
{
boost::thread t(thread_func);
_getch();
}
Receiving the following errors:
1>LIBCMTD.lib(dbgheap.obj) : error LNK2005: __heap_alloc already defined in LIBCMT.lib(malloc.obj)
1>LIBCMTD.lib(dbgheap.obj) : error LNK2005: __recalloc already defined in LIBCMT.lib(recalloc.obj)
1>LIBCMTD.lib(dbgheap.obj) : error LNK2005: __msize already defined in LIBCMT.lib(msize.obj)
1>LIBCMTD.lib(dbghook.obj) : error LNK2005: __crt_debugger_hook already defined in LIBCMT.lib(dbghook.obj)
1>LIBCMTD.lib(isctype.obj) : error LNK2005: __isctype_l already defined in LIBCMT.lib(isctype.obj)
1>LIBCMTD.lib(isctype.obj) : error LNK2005: __isctype already defined in LIBCMT.lib(isctype.obj)
1>LINK : warning LNK4098: defaultlib ‘LIBCMTD’ conflicts with use of other libs; use /NODEFAULTLIB:library
1>fatal error LNK1169: one or more multiply defined symbols found
You are calling
boost::this_thread::sleep ( workTime )outside of any control flow. You should do something like: