473,287 Members | 1,978 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,287 software developers and data experts.

boost thread(ing) a member function

#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.
Nov 18 '06 #1
3 7239
* 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.

}
cout << "waiting for the thread to finish" << endl;
thrd.join();
}

int main()
{
waiter jack;
}

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Nov 18 '06 #2
"Alf P. Steinbach" <al***@start.nowrites:
* Gary Wessle:
}
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.

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);
}

Nov 18 '06 #3
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:
"Alf P. Steinbach" <al***@start.nowrites:
* Gary Wessle:
}
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.

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);
}
Nov 18 '06 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

37
by: Ben | last post by:
Hi, there. Recently I was working on a problem where we want to save generic closures in a data structure (a vector). The closure should work for any data type and any method with pre-defined...
2
by: raxitsheth | last post by:
Hello All... I am using Posix Thread. class Parent { public: virtual void* func(void *)=0;
11
by: Enquiries, Hopkins Research | last post by:
Hi all I have a conundrum that is puzzling me. I have a large codebase in C that I am converting to C++ as fast as possible (i.e. slowly because I keep learning new idioms and stumbling with...
4
by: Agoston Bejo | last post by:
Hi, I would like to do something like this: struct A { int f(float f); }; .... int g(int(*f1)(float)) { return f1(6.5); }
1
by: deluded.soul | last post by:
Hi everyone, I am trying to use the boost multithreading library. I am having a problem as the join() function for the thread never returns. I am using a boolean variable to indicate when the...
3
by: ryan.mitchley | last post by:
Hi all I have a class (cPort) that is designed to receive objects and, depending on the type, call a handler (callback) in any descendant of a cProcessBlock class. Callback functions take a...
3
by: jean.daniel.michaud | last post by:
Hi, I am using a class with operator() as threadfunc for Boost.Thread. I have inheritance used with those class but it does not work, see the example: // Snippet on #include <iostream>...
2
by: Chameleon | last post by:
Why this strange output? Why so many d'tor calls? The code: ---------------------------------------------------------- #include <cstdio> #include <boost/thread/thread.hpp> class A {
1
by: tdlr | last post by:
I'm using VC++ 2010 with Boost 1.43. I've just created a class that uses winsock to handle network communication. Now there's a method called "fillQueue" which is defined like this: void...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.