By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
448,691 Members | 1,143 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 448,691 IT Pros & Developers. It's quick & easy.

Problem using boost::bind() with template functions having arguments

P: n/a
Hey there, C++ Coders!

I am learning multi-threading with boost and have come up with the
following code example shown below.
This example implements a test of the producer-consumer design
pattern.
gcc-4.1.2 however complains with errors about my use of boost::bind().
Doesn't bind support this way of specifying a function and its
arguments or should I solve it in another way?

Many thanks in advance,
Per Nordl÷w

=========== test_threadsafety.cpp begins =============

#include "threadsafe_queue.hpp"
#include <iostream>

using std::cout;
using std::endl;

template <typename T>
void produce(threadsafe_queue<T& q, uint n)
{
for (uint i = 0; i < n; i++) {
T x = i;
q.push(x);
cout << "i:" << i << " produced: " << x << endl;
}
}

template <typename T>
void consume(threadsafe_queue<T& q, uint n)
{
for (uint i = 0; i < n; i++) {
T x = q.wait_pop();
cout << "i:" << i << " consumed: " << x << endl;
}
}

int main()
{
threadsafe_queue<floatq;
boost::thread pt(boost::bind(produce<float>, q, 10));
boost::thread ct(boost::bind(consume<float>, q, 10));
pt.join();
ct.join();
return 0;
}

=========== test_threadsafety.cpp ends =============

=========== threadsafe_queue.hpp begins =============

#ifndef PNW__THREADSAFE_QUEUE_HPP
#define PNW__THREADSAFE_QUEUE_HPP

/***
* @file threadsafe_queue.hpp
* @brief Thread Safe wrapper on std:queue using Boost.
*/

#include <queue>
#include <iostream>

#include <boost/bind.hpp>
#include <boost/thread/thread.hpp>
#include <boost/thread/mutex.hpp>
#include <boost/thread/condition.hpp>

template <typename T>
class threadsafe_queue
{
std::queue<Tq; ///< Queue.
boost::mutex m; ///< Mutex.
boost::condition c; ///< Condition.
public:

/***
* Push @p value.
*/
void push(const T & value) {
boost::mutex::scoped_lock sl(m); // NOTE: lock mutex
q.push(value);
}

/***
* Try and pop into @p value, returning directly in any case.
* @return true if pop was success, false otherwise.
*/
bool try_pop(T & value) {
boost::mutex::scoped_lock sl(m); // NOTE: lock mutex
if (q.size()) {
value = q.front();
q.pop();
return true;
}
return false;
}

/// Pop into @p value, while possibly waiting forever.
T wait_pop() {
boost::mutex::scoped_lock sl(m); // NOTE: lock mutex
// wait until queue has at least on element()
c.wait(sl, boost::bind(&std::queue<T>::size, q));
T value = q.front();
q.pop();
return value;
}
};

#endif

=========== threadsafe_queue.hpp ends =============

Aug 16 '07 #1
Share this Question
Share on Google+
3 Replies


P: n/a
Nordl÷w wrote:
I am learning multi-threading with boost and have come up with the
following code example shown below.
This example implements a test of the producer-consumer design
pattern.
gcc-4.1.2 however complains with errors about my use of boost::bind().
Doesn't bind support this way of specifying a function and its
arguments or should I solve it in another way?
[..]
Do you know that Boost.org has its own online forums? Just checking...

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Aug 16 '07 #2

P: n/a
On Aug 16, 4:57 pm, Nordl÷w <per.nord...@gmail.comwrote:
Hey there, C++ Coders!
Hey there, you!

Can you copy paste your g++ output?
And if nobody can save you ass here, there you'll find the boost-
user mailing list: http://www.boost.org/more/mailing_lists.htm#users

Cheers,
LS

Aug 16 '07 #3

P: n/a
Nordl÷w wrote:
Hey there, C++ Coders!

I am learning multi-threading with boost and have come up with the
following code example shown below.
This example implements a test of the producer-consumer design
pattern.
gcc-4.1.2 however complains with errors about my use of boost::bind().
Doesn't bind support this way of specifying a function and its
arguments or should I solve it in another way?

Many thanks in advance,
int main()
{
threadsafe_queue<floatq;
boost::thread pt(boost::bind(produce<float>, q, 10));
boost::thread ct(boost::bind(consume<float>, q, 10));
pt.join();
ct.join();
return 0;
}

=========== test_threadsafety.cpp ends =============

=========== threadsafe_queue.hpp begins =============

template <typename T>
class threadsafe_queue
{
std::queue<Tq; ///< Queue.
boost::mutex m; ///< Mutex.
^ noncopyable
boost::condition c; ///< Condition.
>
=========== threadsafe_queue.hpp ends =============
threadsafe_queue has mutex as its member, which is noncopyable, when you
do boost::bind, it triggers error
Aug 21 '07 #4

This discussion thread is closed

Replies have been disabled for this discussion.