Hello I was trying to compile a program with boost, and I have the following link errors, althought is it weird because it works on visual studio 2010 but it wont work on xcode.
Undefined symbols:
"boost::thread::hardware_concurrency()", referenced from:
_main in main.o
"boost::thread::start_thread()", referenced from:
boost::thread::thread<Producer>(Producer, boost::disable_if<boost::is_convertible<Producer&, boost::detail::thread_move_t<Producer> >, boost::thread::dummy*>::type)in main.o
boost::thread::thread<Consumer>(Consumer, boost::disable_if<boost::is_convertible<Consumer&, boost::detail::thread_move_t<Consumer> >, boost::thread::dummy*>::type)in main.o
"boost::thread::join()", referenced from:
boost::thread_group::join_all() in main.o
"typeinfo for boost::detail::thread_data_base", referenced from:
typeinfo for boost::detail::thread_data<Producer>in main.o
typeinfo for boost::detail::thread_data<Consumer>in main.o
"vtable for boost::detail::thread_data_base", referenced from:
boost::detail::thread_data_base::thread_data_base()in main.o
"boost::thread::~thread()", referenced from:
std::auto_ptr<boost::thread>::~auto_ptr()in main.o
boost::thread_group::~thread_group()in main.o
"boost::this_thread::disable_interruption::disable_interruption()", referenced from:
boost::shared_mutex::lock() in main.o
boost::shared_mutex::lock_shared() in main.o
"boost::this_thread::disable_interruption::~disable_interruption()", referenced from:
boost::shared_mutex::lock() in main.o
boost::shared_mutex::lock() in main.o
boost::shared_mutex::lock_shared() in main.o
boost::shared_mutex::lock_shared() in main.o
"boost::detail::thread_data_base::~thread_data_base()", referenced from:
boost::detail::thread_data<Producer>::~thread_data()in main.o
boost::detail::thread_data<Producer>::~thread_data()in main.o
boost::detail::thread_data<Consumer>::~thread_data()in main.o
boost::detail::thread_data<Consumer>::~thread_data()in main.o
"boost::this_thread::interruption_point()", referenced from:
Consumer::operator()()in main.o
"boost::this_thread::sleep(boost::posix_time::ptime const&)", referenced from:
void boost::this_thread::sleep<boost::posix_time::seconds>(boost::posix_time::seconds const&)in main.o
"boost::thread::interrupt()", referenced from:
boost::thread_group::interrupt_all() in main.o
"boost::detail::get_current_thread_data()", referenced from:
boost::detail::interruption_checker::interruption_checker(_opaque_pthread_cond_t*)in main.o
ld: symbol(s) not found
collect2: ld returned 1 exit status
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151
|
#include <boost/thread.hpp>
#include <boost/shared_ptr.hpp>
#include <iostream>
#include <iterator>
#include <algorithm>
#include <exception>
#include <queue>
#include <string>
#include <sstream>
#include <cstdlib>
#include <cstdio>
using namespace std;
// Cola con sincronizaciÛn de hilos
template<typename T>
class SynchronisedQueue
{
private:
std::queue<T> queue_; // cola de la stl para almacenar datos
boost::mutex mutex_; // Mutex para sincronizar los hilos (secciones crÌticas)
boost::condition_variable cond_; // CondiciÛn de espera
public:
// AÒadir datos a la cola y notificar de ello
void Enqueue(const T& data)
{
boost::unique_lock<boost::mutex> lock(mutex_);
queue_.push(data);
cond_.notify_one(); // Notify others that data is ready
} // el bloqueo es liberado por el idio RAII
// Obtiene data de la cola; espera si es que no hubiera data disponible
T Dequeue()
{
boost::unique_lock<boost::mutex> lock(mutex_);
// Cuando no exista data, esperar· hasta que alguien aÒada data en la cola
// el bloqueo es autom·ticamente liberado dentro de wait y
// obtenido nuevamente despuÈs del wait
while (queue_.size() == 0) cond_.wait(lock);
T result = queue_.front();
queue_.pop();
return result;
} // El bloqueo es autom·ticamente liberado (out of scope)
};
// Clase de producciÛn de objetos (insertados en la cola)
class Producer
{
private:
int id_; // identificador del objeto
SynchronisedQueue<string>* sync_queue_; // la cola es compartida por eso es un puntero
public:
// Constructor with id and the queue to use
Producer(int id, SynchronisedQueue<string>* queue) :
id_(id), sync_queue_(queue) {}
// FunciÛn para llenar de data la cola
void operator ()()
{
int data = 0;
while (true) {
// Producimos una cadena
stringstream stream;
stream << "Producer: " << id_ << " data: " << data++ << endl; // endl es para hacer flush autom·ticamente.
sync_queue_->Enqueue(stream.str());
cout << stream.str();
// simulaciÛn de producciÛn
boost::this_thread::sleep(boost::posix_time::seconds(1));
}
}
};
// Clase de consumo de objetos (de una cola)
class Consumer
{
private:
int id_; // identificador del consumidor
SynchronisedQueue<string>* sync_queue_; // cola a usar
public:
// la cola com˙n ser· pasad como par·metro por referencia
Consumer(int id, SynchronisedQueue<string>* queue):
id_(id), sync_queue_(queue) {};
// funciÛn de consumo
void operator ()()
{
while (true) {
// Get the data from the queue and print it
stringstream stream;
stream << "Consumer " << id_ << " consumed: (" << sync_queue_->Dequeue().c_str() << endl;
cout << stream.str();
// Forma de asegurarnos que pueda ser interrumpido el hilo de ejecuciÛn
boost::this_thread::interruption_point();
}
}
};
int main(int argc, char** argv)
{
// Mostrar el nombre de procesadores/n˙cleos
cout << boost::thread::hardware_concurrency()<< " processors/cores detected." << endl << endl;
cout << "When threads are running, press enter to stop" << endl;
// n˙mero de productores y consumidores
int nro_producers, nro_consumers;
// cola compartida
SynchronisedQueue<string> queue;
cout << "How many producers do you want? : ";
cin >> nro_producers;
cout << "How many consumers do you want? : ";
cin >> nro_consumers;
boost::thread_group producers;
for (int i = 0; i < nro_producers; i++) {
Producer p(i, &queue);
producers.create_thread(p);
}
boost::thread_group consumers;
for (int i = 0; i < nro_consumers; i++) {
Consumer c(i, &queue);
consumers.create_thread(c);
}
// esperamos
boost::this_thread::sleep(boost::posix_time::seconds(10));
// cin >> ws;
// cin.get();
// Interrupt the threads and stop them
producers.interrupt_all();
producers.join_all();
consumers.interrupt_all();
consumers.join_all();
return 0;
}
|