gpt4 book ai didi

c++ - 以这个程序中使用的方式使用互斥量是否正确?

转载 作者:行者123 更新时间:2023-11-28 07:11:18 24 4
gpt4 key购买 nike

我是 C++ 的新手,我已经开始在 C++ 中进行多线程处理。你能评论一下下面的节目吗?这是使用互斥锁的正确方法吗?另一个问题是在 C++ 中识别共享资源很容易——只需查看静态成员即可。 C++没有全局变量的概念,所以只能看类的静态成员?之后,决定应该序列化什么 - 互斥锁锁定/解锁?在 C 中,识别共享资源有点挑战,因为有全局变量的概念。你能纠正我的理解吗?

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

最佳答案

一些事情:

  1. 全局变量可以在 C++ 中使用,但尽量避免使用它们,尤其是在多线程代码中。
  2. 您没有在任何地方初始化或销毁互斥量。
  3. 在 C++ 中,您不应直接调用 mutex_lock/unlock,而应让一个包装类为您执行此操作,以避免在函数提前返回时锁定互斥锁(return 错误或 异常)。

例子:

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

关于c++ - 以这个程序中使用的方式使用互斥量是否正确?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20929701/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com