简体   繁体   English

在int main()中使用类函数

[英]Using a class function in 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()? 如何从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? pthread_mutex_t_create? or pthread_t_create? 还是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);

EDIT: 编辑:

By the way, pthread_create and pthread_join take a pthread_t* and not a mutex! 顺便说一句, pthread_createpthread_join使用pthread_t*而不是互斥锁!

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

You can either declare those methods as static or use an object to make the calls: 您可以将这些方法声明为静态方法,也可以使用对象进行调用:

myCountingSemaphoreUsingBinarySemaphore s;
s.waitSemaphore(wrt);

You are calling class methods as just waitSemaphore without creating the object of myCountingSemaphoreUsingBinarySemaphore. 您正在将类方法仅作为waitSemaphore调用,而没有创建waitSemaphore的对象。

You should create the object first. 您应该首先创建对象。

myCountingSemaphoreUsingBinarySemaphore obj;
obj.waitSemaphore(mutex1);

The two threads you create (via reader_writer() ) do nothing. 您创建的两个线程(通过reader_writer()reader_writer() main() just goes into the first do loop with no way of getting out. main()只是进入第一个do循环而无法退出。

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. 最后,您正在调用pthread_mutex_lock()等。 on a pthread_t when those functions are supposed to be called on a pthread_mutex_t . 当应该在pthread_mutex_t上调用这些函数时,在pthread_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. 基本上,您需要在如何创建线程以及如何同步线程方面都审查多线程编程。

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

karlphillip was right, you need to pass by pointer instead of reference karlphillip是正确的,您需要通过指针而不是引用来传递

BTW, following line are mistake also, the pthread_create accept and pthread_t instead of pthread_mutex_t 顺便说一句,下一行也是错误的,pthread_create accept和pthread_t而不是pthread_mutex_t

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

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM