I have two classes evaluated threads but I cannot send a signal from one to another, Some ideas why it doesn’t work:
class 1:
#include "timer.h"
#include <ncurses.h>
#include <stdlib.h>
#include <time.h>
#include <unistd.h>
#include "mutexy.h"
Timer::Timer() {
}
Timer::~Timer() {
}
void Timer::startThread() {
pthread_create(&thread, NULL, runInstance, reinterpret_cast<void *>(this));
}
void Timer::joinThread() {
pthread_join(thread, NULL);
}
void Timer::init() {
this->hh = 0;
this->day = 1;
this->month = 1;
this->year = 2011;
}
void Timer::run() {
while(true)
{
if(hh==24) {
hh = 0;
day++;
if(day==31) {
day=1;
month++;
if(month==13) {
year++;
month==1;
}
}
}
for (int i=0; i<10; i++) {
if(hh==7) {
hh=20;
pthread_cond_signal(&brak_biletow_treshold);
}
}
sleep(1);
hh++;
pthread_yield();
}
}
void* Timer::runInstance(void *instance) {
Timer *d = reinterpret_cast<Timer *>(instance);
d->run();
return NULL;
}
class2:
#include "flight_list.h"
#include <ncurses.h>
#include <stdlib.h>
#include <time.h>
#include <unistd.h>
#include "mutexy.h"
string cities[8] = {"Warszawa","Berlin","Paryz","Londyn","Dublin","Madryt","Wieden","Moskwa"};
FlightList::FlightList() {
srand(time(NULL));
}
FlightList::~FlightList() {
}
void FlightList::startThread() {
pthread_create(&thread, NULL, runInstance, reinterpret_cast<void *>(this));
}
void FlightList::joinThread() {
pthread_join(thread, NULL);
}
void FlightList::init() {
flightTab = new Flight *[10];
for (int i=0; i<10; i++) {
int city = rand()%7+1;
flightTab[i] = new Flight(i*2+2,i+2,1,2011,60,cities[0],cities[city]);
}
}
void FlightList::run() {
while(true)
{
pthread_mutex_lock(&bilety_mutex);
pthread_cond_wait(&brak_biletow_treshold, &bilety_mutex);
for (int i=0; i<10; i++) {
for(int i=0; i<9; i++)
flightTab[i] = flightTab[i+1];
int city = rand()%7+1;
flightTab[9] = new Flight(4,10,2,2011,60,cities[0],cities[city]);
}
pthread_mutex_unlock(&bilety_mutex);
pthread_yield();
}
}
void* FlightList::runInstance(void *instance) {
FlightList *d = reinterpret_cast<FlightList *>(instance);
d->run();
return NULL;
}
and header file with mutex and cond_t:
#include <pthread.h>
static pthread_mutex_t bilety_mutex;
static pthread_mutex_t loty_mutex;
static pthread_cond_t brak_biletow_treshold = PTHREAD_COND_INITIALIZER
;
mutex are working but signal is not recived by thread :S
Each translation unit will have it’s own
brak_biletow_tresholdinstance because you’ve declared themstatic(which effectively restricts the scope to the current translation unit).You need to change the way your “globals” are declared and accessed. I suggest that you pass the the condition to the constructor of the two classes.