【问题标题】:Using a class function in int main()在 int main() 中使用类函数
【发布时间】:2011-04-11 16:49:26
【问题描述】:

我在从主程序调用函数时遇到问题。
这些功能必须在我的课堂上。
如何从我的 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?
什么是正确的类型?

【问题讨论】:

  • 能否请您减少您的示例或至少添加您遇到的错误
  • 那么您到底遇到了哪一行?或者你想在哪里调用哪个函数?
  • 我希望你的编译器不接受这个。 pthread_t 是线程类型,而不是互斥类型。您正在寻找pthread_mutex_tman pthreads.
  • @ohlegend,你没有。 pthread_t 代表一个线程。 pthread_mutex_t 代表一个互斥体。 pthread_cond_t 表示条件变量。您似乎对这些概念感到困惑。我建议您阅读有关这些概念的更多内容,然后特别关注一些有关 pthread 的教程。
  • 您不使用 pthreads_mutex_t 创建线程,而是使用 pthread_create。在开始编写项目之前,请最好阅读一些 pthread 教程并创建一些简单的示例。这个教程看起来不错computing.llnl.gov/tutorials/pthreads

标签: c++ class unix g++ pthreads


【解决方案1】:

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

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

【讨论】:

  • 当我这样做时,我得到:Source1.cpp:77: error: no matching function for call to 'myCountingSemaphoreUsingBinarySemaphore::deleteSemaphore (pthread_mutex_t&)'
  • 当您在 main() 中调用方法时,您不是传递指向互斥锁的指针,而是传递互斥锁本身。你必须像 obj.deleteSemaphore(&amp;mutex1); 而不是 obj.deleteSemaphore(mutex1); 那样做
  • 你的函数需要一个pthread_mutex_t*,一个指针。您尝试传递引用,传递指针使用(&amp;mutex1)
  • 在继续之前,您真的需要阅读 pthread 教程amparo.net/ce155/thread-ex.html
  • @ohlegend 顺便说一句,您的所有问题都已经得到解答。考虑接受答案!如果您还有其他问题,我们很乐意在另一个帖子中回答。
【解决方案2】:

您可以将这些方法声明为静态或使用对象进行调用:

myCountingSemaphoreUsingBinarySemaphore s;
s.waitSemaphore(wrt);

【讨论】:

    【解决方案3】:

    您只是在调用类方法 waitSemaphore 而没有创建 myCountingSemaphoreUsingBinarySemaphore 的对象。

    你应该先创建对象。

    myCountingSemaphoreUsingBinarySemaphore obj;
    obj.waitSemaphore(mutex1);
    

    【讨论】:

      【解决方案4】:

      您创建的两个线程(通过reader_writer())什么都不做。 main() 只是进入第一个 do 循环,无法退出。

      此外,您似乎混淆了互斥锁、信号量和条件变量。函数名称使您看起来像是在尝试在类中实现条件变量。但是您将其构建为互斥锁的包装器。

      最后,你打电话给pthread_mutex_lock() et al。当这些函数应该在 pthread_mutex_t 上调用时,在 pthread_t 上。

      可能还有其他错误,但这些才是真正跳出来的。基本上,您需要回顾多线程编程,包括线程的创建方式和同步方式。

      【讨论】:

      • @ohlegend, pthread_mutex_t 是互斥体,而不是线程。您可能应该阅读 Zuljin 引用的教程。
      • @ohlegend pthread_mutex_t 用于 mutex线程pthread_t 表示。您确实需要阅读有关线程的教程。
      【解决方案5】:

      你需要创建一个类的实例(一个对象)才能调用他的成员函数。

      在这个特定的代码中,成员函数没有理由成为实例,并且可以是静态的:

      class foo{
          public:
      
          static void bar(int val)
          {
          //do something
          }
          };
      
      int main()
      {
         foo::bar(10);
      }
      

      【讨论】:

      • 请编写编译器输出。
      • 对了,你真的有理由把这些函数集中到一个类下吗?
      【解决方案6】:

      karlphillip 是对的,你需要通过指针而不是引用来传递

      顺便说一句,以下行也是错误的,pthread_create 接受和 pthread_t 而不是 pthread_mutex_t

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

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

      【讨论】:

      • 他们需要是什么类型的? pthread_mutex_t_create?还是 pthread_t_create?
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-06-07
      • 2020-06-27
      • 2015-05-15
      • 1970-01-01
      • 2018-03-16
      • 2016-01-28
      相关资源
      最近更新 更多