我从我的主程序调用我的function有问题。
这些function必须在我的class级。
我如何从我的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 than 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 is 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 is 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 () {}
这是我得到的错误:
他们需要什么types? pthread_mutex_t_create? 或者pthread_t_create?
什么是适当的types?
类中的函数被称为方法。 您需要实例化该类的一个对象才能使用它的方法:
myCountingSemaphoreUsingBinarySemaphore obj; // obj is an instance of the class obj.waitSemaphore(&mutex1); obj.signalSemaphore(&mutex1);
编辑:
顺便说一下, pthread_create和pthread_join需要一个pthread_t*
而不是一个互斥体!
int pthread_create(pthread_t* thread, pthread_attr_t* attr, void* (*start_routine)(void*), void* arg);
您可以将这些方法声明为静态或使用对象来进行调用:
myCountingSemaphoreUsingBinarySemaphore s; s.waitSemaphore(wrt);
您只是waitSemaphore
不创建waitSemaphore
的对象的情况下调用类方法waitSemaphore。
你应该先创建对象。
myCountingSemaphoreUsingBinarySemaphore obj; obj.waitSemaphore(mutex1);
你创建的两个线程(通过reader_writer()
)什么都不做。 main()
只是进入第一个do
循环而没有办法出去。
另外,你似乎混淆了互斥,信号量和条件变量。 函数名称使得它看起来像你正试图在你的类中实现条件变量。 但是你只是把它建造成互斥锁的封装。
最后,你正在调用pthread_mutex_lock()
等。 在pthread_t
时,应该在pthread_mutex_t
上调用这些函数。
可能还有其他的错误,但是这些是真正跳出来的错误。 基本上,您需要回顾多线程编程,无论是线程创建方式,还是同步方式。
你需要创建一个类的实例(一个对象)来调用他的成员函数。
在这个特定的代码中,成员函数没有理由是实例,可能是静态的:
class foo{ public: static void bar(int val) { //do something } }; int main() { foo::bar(10); }
karlphillip是对的,你需要通过指针而不是引用
顺便说下,线也是错的,pthread_create接受和pthread_t代替pthread_mutex_t
pthread_create(&mutex1,NULL,reader_writer,void);
pthread_create(&wrt,NULL,reader_writer,void);