Using a class function in int main()

2019-01-27 08:55发布

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 () {}  

Here is an error I get:

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

6条回答
看我几分像从前
2楼-- · 2019-01-27 09:18

You need to create an instance of the class (an object) in order to call his member functions.

In this particular code the member functions has no reason to be instance and could be static:

class foo{
    public:

    static void bar(int val)
    {
    //do something
    }
    };

int main()
{
   foo::bar(10);
}
查看更多
叛逆
3楼-- · 2019-01-27 09:18

karlphillip was right, you need to pass by pointer instead of reference

BTW, following line are mistake also, the pthread_create accept and pthread_t instead of pthread_mutex_t

pthread_create( &mutex1, NULL, reader_writer, void);

pthread_create( &wrt, NULL, reader_writer, void);

查看更多
来,给爷笑一个
4楼-- · 2019-01-27 09:21

You can either declare those methods as static or use an object to make the calls:

myCountingSemaphoreUsingBinarySemaphore s;
s.waitSemaphore(wrt);
查看更多
啃猪蹄的小仙女
5楼-- · 2019-01-27 09:24

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);

EDIT:

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);
查看更多
smile是对你的礼貌
6楼-- · 2019-01-27 09:34

The two threads you create (via reader_writer()) do nothing. main() just goes into the first do loop with no way of getting out.

Also, you seem to have confused mutex, semaphores, and condition variables. The function names makes it look like you're trying to implement condition variables in your class. But you're building it as just wrappers to mutex locks.

And finally, you are calling pthread_mutex_lock() et al. on a pthread_t when those functions are supposed to be called on a pthread_mutex_t.

There probably are other errors, but these are the ones that really jump out. Basically, you need to review multi-threaded programming, both in terms of how threads are created, and how they are synchronized.

查看更多
Bombasti
7楼-- · 2019-01-27 09:39

You are calling class methods as just waitSemaphore without creating the object of myCountingSemaphoreUsingBinarySemaphore.

You should create the object first.

myCountingSemaphoreUsingBinarySemaphore obj;
obj.waitSemaphore(mutex1);
查看更多
登录 后发表回答