Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/134.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/xcode/7.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
以本程序中使用的方式使用互斥是正确的吗? 我是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; } #包括 #包括 #包括 #包括 /**获得pid**/ #包括 #包括 使用名称空间std; 类氦螺纹 { 私人: pthread_t*thread_id; 公众: 静态pthread_mutex_t mutex_线程; 无效设置线程id(pthread\t tid); pthread_t*get_thread_id(); int create_thread(pthread_t*thread_ptr,const pthread_attr_t*attr,void*(*start_例程)(void*),void*arg); 氦_螺纹(); ~he_螺纹(); }; 无效线程::设置线程id(pthread\t tid) { *(此->线程id)=tid; } pthread\u t*氦线程::获取线程id() { 返回(此->线程id); } int HEMA_thread::创建_线程(pthread_t*thread_ptr,const pthread_attr_t*attr,void*(*启动_例程)(void*),void*参数) { int ret; ret=pthread\u create(thread\u ptr、attr、start\u例程,(void*)arg); 有几件事: 全局变量可以用于C++,但尽量避免它们,尤其是在多线程代码中。 您没有在任何地方初始化或销毁互斥锁 在C++中,不应该直接调用MutExxLoop/UnCub,而是有一个包装类为您做,以避免互斥锁被锁定在函数的早期返回(返回错误或异常< /代码>)。_C++_Multithreading - Fatal编程技术网 线程id)=tid; } pthread\u t*氦线程::获取线程id() { 返回(此->线程id); } int HEMA_thread::创建_线程(pthread_t*thread_ptr,const pthread_attr_t*attr,void*(*启动_例程)(void*),void*参数) { int ret; ret=pthread\u create(thread\u ptr、attr、start\u例程,(void*)arg); 有几件事: 全局变量可以用于C++,但尽量避免它们,尤其是在多线程代码中。 您没有在任何地方初始化或销毁互斥锁 在C++中,不应该直接调用MutExxLoop/UnCub,而是有一个包装类为您做,以避免互斥锁被锁定在函数的早期返回(返回错误或异常< /代码>)。,c++,multithreading,C++,Multithreading" /> 线程id)=tid; } pthread\u t*氦线程::获取线程id() { 返回(此->线程id); } int HEMA_thread::创建_线程(pthread_t*thread_ptr,const pthread_attr_t*attr,void*(*启动_例程)(void*),void*参数) { int ret; ret=pthread\u create(thread\u ptr、attr、start\u例程,(void*)arg); 有几件事: 全局变量可以用于C++,但尽量避免它们,尤其是在多线程代码中。 您没有在任何地方初始化或销毁互斥锁 在C++中,不应该直接调用MutExxLoop/UnCub,而是有一个包装类为您做,以避免互斥锁被锁定在函数的早期返回(返回错误或异常< /代码>)。,c++,multithreading,C++,Multithreading" />

以本程序中使用的方式使用互斥是正确的吗? 我是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; } #包括 #包括 #包括 #包括 /**获得pid**/ #包括 #包括 使用名称空间std; 类氦螺纹 { 私人: pthread_t*thread_id; 公众: 静态pthread_mutex_t mutex_线程; 无效设置线程id(pthread\t tid); pthread_t*get_thread_id(); int create_thread(pthread_t*thread_ptr,const pthread_attr_t*attr,void*(*start_例程)(void*),void*arg); 氦_螺纹(); ~he_螺纹(); }; 无效线程::设置线程id(pthread\t tid) { *(此->线程id)=tid; } pthread\u t*氦线程::获取线程id() { 返回(此->线程id); } int HEMA_thread::创建_线程(pthread_t*thread_ptr,const pthread_attr_t*attr,void*(*启动_例程)(void*),void*参数) { int ret; ret=pthread\u create(thread\u ptr、attr、start\u例程,(void*)arg); 有几件事: 全局变量可以用于C++,但尽量避免它们,尤其是在多线程代码中。 您没有在任何地方初始化或销毁互斥锁 在C++中,不应该直接调用MutExxLoop/UnCub,而是有一个包装类为您做,以避免互斥锁被锁定在函数的早期返回(返回错误或异常< /代码>)。

以本程序中使用的方式使用互斥是正确的吗? 我是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; } #包括 #包括 #包括 #包括 /**获得pid**/ #包括 #包括 使用名称空间std; 类氦螺纹 { 私人: pthread_t*thread_id; 公众: 静态pthread_mutex_t mutex_线程; 无效设置线程id(pthread\t tid); pthread_t*get_thread_id(); int create_thread(pthread_t*thread_ptr,const pthread_attr_t*attr,void*(*start_例程)(void*),void*arg); 氦_螺纹(); ~he_螺纹(); }; 无效线程::设置线程id(pthread\t tid) { *(此->线程id)=tid; } pthread\u t*氦线程::获取线程id() { 返回(此->线程id); } int HEMA_thread::创建_线程(pthread_t*thread_ptr,const pthread_attr_t*attr,void*(*启动_例程)(void*),void*参数) { int ret; ret=pthread\u create(thread\u ptr、attr、start\u例程,(void*)arg); 有几件事: 全局变量可以用于C++,但尽量避免它们,尤其是在多线程代码中。 您没有在任何地方初始化或销毁互斥锁 在C++中,不应该直接调用MutExxLoop/UnCub,而是有一个包装类为您做,以避免互斥锁被锁定在函数的早期返回(返回错误或异常< /代码>)。,c++,multithreading,C++,Multithreading,例如: 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+

例如:

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++ 11的代码> STD::线程< /Cord>?应该在CODReVIEW.STACKExchange(com)中。但是,我看到的第一个错误是“代码> *(THE-THEXRID)= TID;。在处理更复杂的多线程编程之前,你应该先从单线程C++编程开始(即学习指针)。这里有什么问题?(this->thread_id)=tid;?您的问题中似乎有几个错误的假设:“C++没有全局变量的概念”和(隐式地)“只有全局/静态变量需要由互斥锁保护”。好的,我在构造函数中看到了
新的
。但是你的
创建线程
有一个问题,因为用户可以传入他们想要的任何线程指针。但是互斥锁的使用在我看来很好。