在int main()中使用类函数 [英] Using a class function in int main()

查看:179
本文介绍了在int main()中使用类函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我从主程序调用我的函数时遇到问题。

这些函数已在我的类中。

如何从我的int main()中访问它们?

I am having problems calling my functions from my main program.
These functions HAVE to be in my class.
How do I access them from my 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?

什么是正确的类型?

what type do they need to be? pthread_mutex_t_create? or pthread_t_create?
what is the proper type?

推荐答案

类中的函数称为方法。您需要实例化该类的一个对象,以便能够使用它的方法:

Functions inside a class are called methods. You need to instantiate an object of that class to be able to use it's methods:

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

obj.waitSemaphore(&mutex1);

obj.signalSemaphore(&mutex1);

编辑

顺便说一下, pthread_create pthread_join 取一个 pthread_t * ,而不是互斥!

By the way, pthread_create and pthread_join take a pthread_t* and not a mutex!

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

这篇关于在int main()中使用类函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆