首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在这个程序中使用互斥是正确的吗?

在这个程序中使用互斥是正确的吗?
EN

Stack Overflow用户
提问于 2014-01-05 03:29:18
回答 1查看 126关注 0票数 0

我是C++的新手,我已经开始在C++中做多线程了。请您对以下节目发表意见?这是使用互斥的正确方式吗?另一个问题是识别C++中的共享资源很容易--只需查看静态成员即可。C++没有全局变量的概念,因此我们只需要查看类的静态成员?然后,决定什么应该序列化-互斥锁/解锁?在C中,确定共享资源有点困难,因为存在全局变量的概念。你能根据我的理解纠正我吗?

代码语言:javascript
复制
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#include <iostream>
/** get pid **/
#include <sys/types.h>
#include <unistd.h>

using namespace std;


class helium_thread
{
  private:
  pthread_t *thread_id;

  public:
  static pthread_mutex_t mutex_thread;
  void set_thread_id(pthread_t tid);
  pthread_t *get_thread_id();
  int create_thread(pthread_t *thread_ptr, const pthread_attr_t *attr, void * (*start_routine)(void *), void *arg );
  helium_thread();  
  ~helium_thread();

};

void helium_thread::set_thread_id( pthread_t tid)
{
   *(this->thread_id) = tid;    
}

pthread_t * helium_thread::get_thread_id( )
{
   return (this->thread_id);
}

int helium_thread::create_thread(pthread_t *thread_ptr, const pthread_attr_t *attr, void * (*start_routine)(void *), void *arg )
{
   int ret;
   ret = pthread_create(thread_ptr,attr,start_routine,(void *)arg)  ;
   cout<<"Thread created "<<std::hex<<thread_ptr<<endl;
   return ret;

}

helium_thread::helium_thread()
{

    thread_id = new pthread_t;
    cout<<"Constructor called "<<std::hex<<thread_id<<endl;
}

helium_thread::~helium_thread()
{
    cout<<"Destructor called"<<std::hex<<thread_id<<endl;
    delete thread_id;
}

/** While defining the methods of the class, Keywords static and virtual should not be repeated in the definition. **/
/** They should only be used in the class declaration. **/

void *Thread_Function(void *thread_arg)
{
  pthread_mutex_lock(&(helium_thread::mutex_thread));   
  cout<<"Inside Thread_Function"<<endl; 
  pid_t *thread_pid_val  = (pid_t *) thread_arg;
  /**  std::hex will print the value isn hexadecimal **/
  cout<<"The process pid is "<< std::hex << (*thread_pid_val) <<endl;
  pthread_t ptid = pthread_self();
  cout<<" The thread id is " << std::hex<< ptid << endl;
  pthread_mutex_unlock(&(helium_thread::mutex_thread)); 

}

/** The definition of the static member can't be inside a function, You need to put it outside **/
/** When I tried using inside a function, I got the error - error: invalid use of qualified-name ‘helium_thread::mutex_thread **/

pthread_mutex_t helium_thread::mutex_thread = PTHREAD_MUTEX_INITIALIZER;

int main(int argc, char *argv[])
{
   helium_thread thread_1, thread_2;
   pid_t thread_pid_val = getpid();
   pthread_t thread_1_id;

   thread_1.create_thread((thread_1.get_thread_id()),NULL,Thread_Function,&thread_pid_val);
   thread_2.create_thread((thread_2.get_thread_id()),NULL,Thread_Function,&thread_pid_val);
   pthread_join( *(thread_1.get_thread_id()), NULL);
   pthread_join( *(thread_2.get_thread_id()), NULL);

   return  0;   
}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2014-01-05 10:01:41

有几件事:

  1. Globals可以在C++中使用,但要尽量避免它们,特别是在多线程代码中。
  2. 你没有在任何地方初始化或销毁互斥体。
  3. 在C++中,您不应该直接调用mutex_lock/unlock,而应该让包装类为您执行此操作,以避免在函数(return on error或exception)返回时锁定互斥锁。

示例:

代码语言:javascript
复制
class auto_lock {
    pthread_mutex_t* mutex;
public:
    auto_lock(pthread_mutex_t* _mutex) : mutex(_mutex) {
        if (mutex) pthread_mutex_lock(mutex);
    }
    ~auto_lock(){
        if (mutex) pthread_mutex_unlock(mutex);
    }
};
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/20929701

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档