Ich möchte eine Variante von Beispiel 46.3 von dieser Website http://theboostcpplibraries.com/boost.lockfree ausführen. Ich bin auf einem Linux-System.boost: Sperren Sie Warteschlangen weltweit über mehrere Dateien hinweg
Ich möchte die Warteschlange q in einer Header-Datei definiert werden. Ich hätte gerne, dass die Funktionen zum Produzieren und Konsumieren in verschiedenen Dateien sind. So würde ich gerne global.h enthalten haben
static boost::lockfree::queue<int> q{100};
static std::atomic<int> sum{0};
void *produce (void*);
void *consume (void*);
Ich würde dann eine produce.cpp haben möchten enthalten:
void *produce(void*)
{
for (int i = 1; i <= 10000; ++i)
q.push(i);
}
und ich möchte eine consume.cpp haben enthalten
void *consume (void*)
{
int i;
while (q.pop(i))
sum += i;
}
dann möchte ich meine main.cpp haben enthalten
#include iosteam
#include iomanip
#include global
#include pthread
int main()
{pthread_t t1;
pthread_t t2;
pthread_t t3;
int t1_iret;
t1_iret = pthread_create(&t1, NULL, produce, NULL);
if(t1_iret)
{
fprintf(stderr,"Error - pthread_create() return code: %d\n",t1_iret);
exit(EXIT_FAILURE);
}
int t2_iret;
t2_iret = pthread_create(&t2, NULL, consume, NULL);
if(t2_iret)
{
fprintf(stderr,"Error - pthread_create() return code: %d\n",t2_iret);
exit(EXIT_FAILURE);
}
int t3_iret;
t3_iret = pthread_create(&t3, NULL, consume, NULL);
if(t3_iret)
{
fprintf(stderr,"Error - pthread_create() return code: %d\n",t3_iret);
exit(EXIT_FAILURE);
}
pthread_join(t1, NULL);
pthread_join(t2, NULL);
pthread_join(t3, NULL);
return 0; }
Außerdem fragte ich mich, ob es möglich wäre, das, was ich beschrieben habe, mit Strings statt mit ganzen Zahlen zu machen.
edit1: wenn ich versuche, und machen sein die Warteschlange Warteschlange von Strings I ::
/usr/local/include/boost/lockfree/queue.hpp erhalten: In Instantiierung ‚Klasse boost :: l ockfree :: queue> ': /home/ubuntu/Project/src/main.cpp:15:37: Von hier aus erforderlich /usr/local/include/boost/lockfree/queue.hpp:87:5: Fehler: statisch Assertion ist fehlgeschlagen: (boost :: has_trivial_destructor :: value) BOOST_STATIC_ASSERT ((boost :: has_trivial_destructor :: value)); ^ /usr/local/include/boost/lockfree/queue.hpp:91:5: Fehler: static Assertionsfehler: (boost :: :: has_trivial_assign value) BOOST_STATIC_ASSERT ((boost :: :: has_trivial_assign value)) ; ^ In der Datei enthalten von /usr/local/include/boost/lockfree/queue.hpp:21:0, von /home/ubuntu/Project/src/main.cpp:5: /usr/local/include /boost/lockfree/detail/copy_payload.hpp: In Instanziierung von 'static void boost :: lockfree :: detail :: copy_constructible_and_copyable :: copy (T &, U &) [mit T = std :: basic_string; U = int] ': /usr/local/include/boost/lockfree/detail/copy_payload.hpp:49:25: benötigt von' void boost :: lockfree :: detail :: copy_payload (T &, U &) [ mit T = std :: basic_string; U = int] ' /usr/local/include/boost/lockfree/queue.hpp:402:61: benötigt von' bool boost:: lockfree :: queue :: pop (U &) [mit U = int; T = std :: basic_string; A0 = boost :: parameter :: void_; A1 = boost :: parameter :: void_; A2 = boost :: parameter :: void_] ' /home/ubuntu/Project/src/main.cpp:21:24: erforderlich von hier /usr/local/include/boost/lockfree/detail/copy_payload.hpp: 38: 11: Fehler: ungültige Umwandlung vom Typ 'std :: basic_string' in 'int' u = U (t);
Nun, ich habe es versucht, und ich habe zwei Probleme, 1) es scheint, dass die globale Warteschlange nicht über die Header-Datei aktualisiert wird. 2) Außerdem kann ich keine Schlange bilden. –
@ArielBaron: OK, danke für die Details. Ich habe meine Antwort aktualisiert (1). Wie für (2), was meinst du mit dir "kann nicht"? Haben Sie eine Fehlermeldung erhalten? –
Für 2) Ich bekomme einen Fehler und für 1) die Summe zeigt Null, die angibt, dass die Warteschlange nicht gefüllt wird –