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

  • Thread starter =?iso-8859-1?B?Tm9yZGz2dw==?=
  • Start date
?

=?iso-8859-1?B?Tm9yZGz2dw==?=

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<float> q;
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<T> q; ///< 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 =============
 
V

Victor Bazarov

Nordlöw said:
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
 
B

Barry

Nordlöw said:
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<float> q;
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<T> q; ///< 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
 

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

Forum statistics

Threads
473,744
Messages
2,569,482
Members
44,901
Latest member
Noble71S45

Latest Threads

Top