c++ - Using a class function in int main() -
i having problems calling functions main program.
these functions have in class.
how access them int main()?
#include <iostream> #include <stdio.h> #include <pthread.h> #include <stdlib.h> #include <math.h> #include <sys/types.h> #include <semaphore.h> #include <synch.h> using namespace std; class mycountingsemaphoreusingbinarysemaphore { public: void waitsemaphore(pthread_mutex_t *thread) { pthread_mutex_lock(*thread);// makes value 1 (not available) } void signalsemaphore(pthread_mutex_t *thread) { pthread_mutex_unlock(*thread); // makes value 0 (available) } void deletesemaphore(pthread_mutex_t *thread) { pthread_mutex_destroy(*thread);// deletes } }; int readercount; int database = (rand() / 100); // number less 1000 void reader_writer(void); int main(int argc, char *argv[]) { mycountingsemaphoreusingbinarysemaphore obj; pthread_mutex_t mutex1; pthread_mutex_t wrt; pthread_create( &mutex1, null, reader_writer, void); pthread_create( &wrt, null, reader_writer, void); //----------------------reader------------------------// do{ cout << "database before read = " << database << endl; obj.waitsemaphore(mutex1);//lock readercount++; if (readercount == 1) { obj.waitsemaphore(wrt);//lock obj.signalsemaphore(mutex1);//unlock //reading preformed obj.waitsemaphore(mutex1); // lock readercount--; } if(readercount == 0) { obj.signalsemaphore(wrt);//unlock obj.signalsemaphore(mutex1); // unlock } cout << "database after read = " << database << endl; }while (true); //-----------------------writer---------------------// do{ cout << "database before write = " << database << endl; obj.waitsemaphore(wrt);//lock //writing preformed database = database + 10; obj.signalsemaphore(mutex1);//unlock cout << "database after write = " << database << endl; }while(true); pthread_join( mutex1, null); pthread_join( wrt, null); obj.deletesemaphore(* mutex1); obj.deletesemaphore(* wrt); return 0; } void reader_writer () {} here error get:
what type need be? pthread_mutex_t_create? or pthread_t_create?
proper type?
functions inside class called methods. need instantiate object of class able use it's methods:
mycountingsemaphoreusingbinarysemaphore obj; // obj instance of class obj.waitsemaphore(&mutex1); obj.signalsemaphore(&mutex1); edit:
by way, pthread_create , pthread_join take pthread_t* , not mutex!
int pthread_create(pthread_t* thread, pthread_attr_t* attr, void* (*start_routine)(void*), void* arg);
Comments
Post a Comment