boost thread(ing) a member function

G

Gary Wessle

#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.
 
A

Alf P. Steinbach

* Gary Wessle:
#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");

Won't work on all systems. Instead, I'd recommend using the proper
sleep primitive in Boost threads. Except I can't find a usable one...

cout << " the wait is over." << endl;
}

void waiter::preform(){
boost::thread thrd( waiting );

On which object should 'waiting' be called? This simply /can't/ work.
Instead, declare 'waiting' as a 'static' function.

int opt;
bool ON=true;

Reserve all uppercase for macros.

while( ON ){
cout << "type a number or 99 to exit" << endl;
cin >> opt;
cout << "you typed " << opt << endl;
if( opt==99 ) ON=false;

Tip: look up the C++ 'break' statement.
 
G

Gary Wessle

Alf P. Steinbach said:
* Gary Wessle:

On which object should 'waiting' be called? This simply /can't/
work. Instead, declare 'waiting' as a 'static' function.

thanks

ok, the problem is that I need to the function "which you say must be
static" operate on EACH object which then must be non-static. so I
thought of making a proxy function which is a static function but
calls another function inside it and this second function operates on
the object, but from my experiment below, it shows that the knowledge
of the object is also absent from with in the static function, what is
the solution then?

here is my experiment.
****************************************************************
#include <boost/thread/thread.hpp>
#include <iostream>
using namespace std;


class waiter {
short customers_serv;
public:
waiter(short);
static void waiting_proxy();
void waiting();
void preform();
};
waiter::waiter(short cs) : customers_serv( cs ) {
preform();
}

void waiter::waiting_proxy(){
waiting();
}
void waiter::waiting(){
customers_serv--; // modify the object
cout << "serving " << customers_serv << " customers" << endl;
}
void waiter::preform(){
boost::thread thrd( waiting_proxy );
thrd.join();
}

int main()
{
waiter jack(5);
waiter sofi(2);
}
 
O

okm

This does not compile. Does it?
You cannot call a non static method from a static method. In the static
method you don't have the this pointer.

okm

Gary Wessle ha scritto:
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,067
Latest member
HunterTere

Latest Threads

Top