#include <boost/thread/thread.hpp>
#include <iostream>
using namespace std;
class waiter {
public:
waiter();
void waiting();
void preform();
};
waiter::waiter(){
preform();
}
void waiter::waiting(){
cout << " a wait thread is created in the background" << endl;
system( "sleep 7s");
cout << " the wait is over." << endl;
}
void waiter::preform(){
boost::thread thrd( waiting );
int opt;
bool ON=true;
while( ON ){
cout << "type a number or 99 to exit" << endl;
cin >opt;
cout << "you typed " << opt << endl;
if( opt==99 ) ON=false;
}
cout << "waiting for the thread to finish" << endl;
thrd.join();
}
int main()
{
waiter jack;
}
using boost thread library
the fist code below works but the second code does not.
I was practicing in the fisrt code in-order to understand
how to use the library so that I may be able to run the
second code. but the second code failed with the errors
at the bottom of the post.
can any one help in running the second code for me please.
************************************************** **************
**************** first code ****************
************************************************** **************
#include <boost/thread/thread.hpp>
#include <iostream>
using namespace std;
void waiting(){
cout << " a wait thread is created in the background" << endl;
system( "sleep 7s");
cout << " the wait is over." << endl;
}
void preform(){
boost::thread thrd( waiting );
int opt;
bool ON=true;
while( ON ){
cout << "type a number or 99 to exit" << endl;
cin >opt;
cout << "you typed " << opt << endl;
if( opt==99 ) ON=false;
}
cout << "waiting for the thread to finish" << endl;
thrd.join();
}
int main()
{
preform();
}
************************************************** **************
**************** second code ****************
************************************************** **************
1 #include <boost/thread/thread.hpp>
2 #include <iostream>
3 using namespace std;
4
5
6 class waiter {
7 public:
8 waiter();
9 void waiting();
10 void preform();
11 };
12 waiter::waiter(){
13 preform();
14 }
15
16 void waiter::waiting(){
17 cout << " a wait thread is created in the background" << endl;
18 system( "sleep 7s");
19 cout << " the wait is over." << endl;
20 }
21
22 void waiter::preform(){
23 boost::thread thrd( waiting );
24 int opt;
25 bool ON=true;
26 while( ON ){
27 cout << "type a number or 99 to exit" << endl;
28 cin >opt;
29 cout << "you typed " << opt << endl;
30 if( opt==99 ) ON=false;
31 }
32 cout << "waiting for the thread to finish" << endl;
33 thrd.join();
34 }
35
36 int main()
37 {
38 waiter jack;
39 }
************************************************** **************
**************** error ****************
************************************************** **************
make -k
g++ -gdwarf-2 -c -o main.o main.cpp
main.cpp: In member function ‘void waiter::preform()’:
main.cpp:23: error: no matching function for call to ‘boost::thread::thread(<unresolved overloaded function type>)’
/usr/include/boost/thread/thread.hpp:38: note: candidates are: boost::thread::thread(const boost::function0<void, std::allocator<boost::function_base&)
/usr/include/boost/thread/thread.hpp:37: note: boost::thread::thread()
/usr/include/boost/thread/thread.hpp:35: note: boost::thread::thread(const boost::thread&)
make: *** [main.o] Error 1
make: Target `proj' not remade because of errors.