Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/128.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/6/multithreading/4.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++_Multithreading_Constructor_Pthreads - Fatal编程技术网

C++ 在从线程类派生的构造函数中传递字符串

C++ 在从线程类派生的构造函数中传递字符串,c++,multithreading,constructor,pthreads,C++,Multithreading,Constructor,Pthreads,我希望能够更改线程的名称,当它实际通过一个名为_thread_class的类创建时,该类是从std类thread派生的。新线程应该在调用“预期的”thread函数之前和构造函数返回之前命名 在下面的示例中,我希望以这种方式将线程名称更改为“Thread01”。 当我运行当前的实现时,即使代码没有显示错误,也会出现编译错误。 我怎样才能做到这一点?修复我的编译错误? 我的编译错误是: usr/include/c++/5/functional:1505:61:错误:类中没有名为“type”的类型

我希望能够更改线程的名称,当它实际通过一个名为_thread_class的类创建时,该类是从std类thread派生的。新线程应该在调用“预期的”thread函数之前和构造函数返回之前命名

在下面的示例中,我希望以这种方式将线程名称更改为“Thread01”。 当我运行当前的实现时,即使代码没有显示错误,也会出现编译错误。 我怎样才能做到这一点?修复我的编译错误? 我的编译错误是: usr/include/c++/5/functional:1505:61:错误:类中没有名为“type”的类型

   template<typename _Callable, typename... _Args>
   void helperFunc(const std::string &name, _Callable&& __f, _Args&&... __args){

        // setting  name here

      pthread_setname_np(pthread_self(), (char*) &name);
       // do something else
   }
   class A_thread_class : public std::thread
   {

   private :
   template<typename _Callable, typename... _Args>
   thread thread_function(const std::string &name,_Callable&& __f, _Args&&... __args) {


   thread th;
   th = std::thread([&]{helperFunc(name, std::forward<_Callable>(__f), std::forward<_Args> 
          (__args)...);});

        }

    public:

    using thread::thread;

    template<typename _Callable, typename... _Args>
    explicit
    A_thread_class (const std::string &name,_Callable&& __f, _Args&&... __args) :
        std::thread(thread_function( &name,_Callable&& __f, _Args&&... __args){}
    }


    void aThreadFunc(void){
    //blah blah implementation
    }

    int main(){


    A_thread_class aThread("Thread01", aThreadFunc);
    aThread.join();

    return 0;
     }
模板
void helperFunc(const std::string和name、_Callable&&&&uuf、_Args&&uu Args){
//在这里设置名称
pthread_setname_np(pthread_self(),(char*)&name);
//做点别的
}
A类线程类:公共标准::线程
{
私人:
模板
线程线程函数(const std::string和name、_可调用&&&&u f、_参数&&u参数){
螺纹th;
th=std::thread([&]{helperFunc(名称,std::forward(_f),std::forward
(args);});
}
公众:
使用thread::thread;
模板
明确的
线程类(const std::string&name、_Callable&&&&u f、_Args&&…_Args):
std::thread(thread_函数(&name,_可调用&&&u函数,_参数&&…_参数){}
}
void aThreadFunc(void){
//诸如此类的实施
}
int main(){
一个线程类aThread(“Thread01”,aThreadFunc);
aThread.join();
返回0;
}

如果要替换
std::thread
的构造函数,则不应使用
使用thread::thread
继承构造函数

目前,您正在使用
constchar(&)[]构造类
引用字符串文字作为第一个参数。
std::thread
的可变转发构造函数因此比您在此处声明的
a_thread\u class
的构造函数更匹配,后者将
std::string
作为第一个参数,因此需要用户定义的转换


如果使用删除
,则
std::thread
构造函数将不再可见,并且您只能定义所需的构造函数。

不太可能是当前具体问题的原因,但很重要:标识符包含双下划线,标识符以下划线开头,后跟大写字母在所有范围内都保留了C++实现,可能不被程序声明。这样做会导致未定义的行为。<代码>(char *)和名称< /C>也会导致未定义的行为。
。您切断了错误消息。请逐字包含完整的编译器输出。链中的第一个错误是:
/usr/bin/./lib64/gcc/x86\u 64-pc-linux-gnu/9.2.0/../../../../../../../include/c++/9.2.0/thread:119:2:错误:静态\u断言因要求“\u is\u invocable::value”而失败“std::thread参数在转换为右值后必须是可开票的”
因此,当您尝试调用passedn函数时,出现了一些问题。此外:1.您缺少一个结束符
A_thread\u类中的
构造函数。2.
\u可调用&
&name
在构造函数中没有意义,第二个函数甚至在语法上都无效。3.
thread\u函数
声明返回
std::thread
但没有这样做。4.
thread\u函数
尝试创建一个新线程只调用
helper\u func
.5的ad。
thread\u函数
不会在线程中运行
helper\u函数
,而只是调用它,并尝试运行其
void
返回值作为线程(这不起作用)。