c++ - 在 int main() 中使用类函数

标签 c++ class unix g++ pthreads

我在从我的主程序调用我的函数时遇到问题。
这些功能必须在我的类(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 () {}  

这是我得到的一个错误:

他们需要什么类型? pthread_mutex_t_create?还是 pthread_t_create?
什么是正确的类型?

最佳答案

类中的函数称为方法。您需要实例化该类的对象才能使用它的方法:

myCountingSemaphoreUsingBinarySemaphore obj; // obj is an instance of the class

obj.waitSemaphore(&mutex1);

obj.signalSemaphore(&mutex1);

编辑:

顺便说一句,pthread_createpthread_join 采用 pthread_t* 而不是互斥量!

int pthread_create(pthread_t* thread, 
                   pthread_attr_t* attr, 
                   void* (*start_routine)(void*), 
                   void* arg);

关于c++ - 在 int main() 中使用类函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5624690/

相关文章:

c - wait() 在 Unix 上做什么?

c++ - 我应该更改函数参数的值吗?

c++ - #导入 : Cannot open file "soap12.h" for reading gSoap in VS 2010

c++ - 为什么变换矩阵在 OpenGL 中不起作用

c++ - 指向数据成员的指针如何分配/存储在内存中?

unix - 单个邮件unix中带有多个附件的mutt命令

c++ - 使用 "JSON for Modern C++"库检测整数不适合指定类型?

javascript - Mouseenter/Mouseleave 区域

java - SQLite DB android 每个表的每个类?

linux - 在目录中查找重复的文件名(不指定确切的文件名)