Reputation: 1540
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
Upvotes: 0
Views: 473
Reputation: 881113
It's because every C source file that includes your header file gets its own copy of the static variables. They're not shared amongst translation units.
The mutexes only seem to work since their action in that case is to not affect each other. For a mutex, that looks like it's working right up to the point where your data gets corrupted.
For a condition variable, the separation means that it doesn't look like it's working, and indeed it isn't.
One solution is to mark them extern
in a header file declaration and define them in a C file somewhere, something like:
my_thread_stuff.h:
extern pthread_mutex_t bilety_mutex;
extern pthread_mutex_t loty_mutex;
extern pthread_cond_t brak_biletow_treshold;
global_thread_stuff.c:
#include "my_thread_stuff.h"
pthread_mutex_t bilety_mutex;
pthread_mutex_t loty_mutex;
pthread_cond_t brak_biletow_treshold = PTHREAD_COND_INITIALIZER;
Upvotes: 1
Reputation: 33655
Each translation unit will have it's own brak_biletow_treshold
instance because you've declared them static
(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.
Upvotes: 2