So habe ich ein Problem: Wenn ich versuche, den letzten Thread zu erstellen, heißt es immer, dass Kern ausgegeben wird. Es macht nichts, wenn ich schreibe, um 5 oder 2 Threads zu erstellen. Hier ist mein Code: UPD: Jetzt kann ich nicht mehr als 3 Threads und Threads tun nicht Funktionen, die ich will sie tun (konsumieren und produzieren)"Segmentierungsfehler (Core Dumped)" bei der Verwendung von Pthread_create
UPD_2: Jetzt habe ich eine Nachricht wie gehen dass: terminate nach dem werfen einer Instanz ‚genannt rekursiv aufgerufen terminate rekursiv Aborted (core dumped)
#include<cstdlib>
#include <iostream>
#include <string>
#include <mutex>
#include <pthread.h>
#include <condition_variable>
#define NUM_THREADS 4
using namespace std;
struct thread_data
{
int thread_id;
int repeat;
};
class our_monitor{
private:
int buffer[100];
mutex m;
int n = 0, lo = 0, hi = 0;
condition_variable in,out;
unique_lock<mutex> lk;
public:
our_monitor():lk(m)
{
}
void insert(int val, int repeat)
{
in.wait(lk, [&]{return n <= 100-repeat;});
for(int i=0; i<repeat; i++)
{
buffer[hi] = val;
hi = (hi + 1) % 100; //ring buffer
n = n +1; //one more item in buffer
}
lk.unlock();
out.notify_one();
}
int remove(int repeat)
{
out.wait(lk, [&]{return n >= repeat;});
int val;
for(int i=0; i<repeat; i++)
{
val = buffer[lo];
lo = (lo + 1) % 100;
n -= 1;
}
lk.unlock();
in.notify_one();
return val;
}
};
our_monitor mon;
void* produce(void *threadarg)
{
struct thread_data *my_data;
my_data = (struct thread_data *) threadarg;
cout<<"IN produce after paramiters"<< my_data->repeat<<endl;
int item;
item = rand()%100 + 1;
mon.insert(item, my_data->repeat);
cout<< "Item: "<< item << " Was prodused by thread:"<< my_data->thread_id << endl;
}
void* consume(void *threadarg)
{
struct thread_data *my_data;
my_data = (struct thread_data *) threadarg;
cout<<"IN consume after paramiters"<< my_data->repeat<<endl;
int item;
item = mon.remove(my_data->repeat);
if(item) cout<< "Item: "<< item << " Was consumed by thread:"<< my_data->thread_id << endl;
}
int main()
{
our_monitor *mon = new our_monitor();
pthread_t threads[NUM_THREADS];
thread_data td[NUM_THREADS];
int rc;
int i;
for(i = 0; i < NUM_THREADS; i++)
{
td[i].thread_id = i;
td[i].repeat = rand()%5 + 1;
if(i % 2 == 0)
{
cout << "main() : creating produce thread, " << i << endl;
rc = pthread_create(&threads[i], NULL, produce, (void*) &td[i]);
if (rc)
{
cout << "Error:unable to create thread," << rc << endl;
exit(-1);
}
} else
{
cout << "main() : creating consume thread, " << i << endl;
rc = pthread_create(&threads[i], NULL, consume, (void *)&td[i]);
if (rc)
{
cout << "Error:unable to create thread," << rc << endl;
exit(-1);
}
}
}
pthread_join(threads[0], NULL);
pthread_join(threads[1], NULL);
pthread_join(threads[2], NULL);
//pthread_exit(NULL);
}
UPD genannt terminate: Jetzt kann ich nicht mehr tun, als 3 Fäden und Fäden nicht Funktionen tun dass ich möchte, dass sie tun (konsumieren und produzieren)
UPD_2: Jetzt eine Nachricht wie die, dass ich gehen habe: enden nach dem Werfen einer Instanz ‚genannt rekursiv rekursiv Aborted (core dumped)
Woher wissen Sie, dass es in "Haupt" eher als einer der Thread-Procs fällt? –
Können Sie erklären, zu welchen Funktionen 'produzieren',' konsumieren'? –
Ich bearbeite meine Frage, es gibt vollen Code –