Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/161.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++ 在pthread_create中使用functor_C++_Functor - Fatal编程技术网

C++ 在pthread_create中使用functor

C++ 在pthread_create中使用functor,c++,functor,C++,Functor,此代码的作用: 执行多个命令以完成(如果(当前秒==几秒)停止)。 CommandProcessor(函子)运行此命令。我试图将这个类的指针抛出到pthread_create 问题是: 是否可以将类对象用作可调用对象 命令.h #ifndef COMMAND_H #define COMMAND_H #include <iostream> #include "commandprocessor.h" class CommandProcessor; using namespace s

此代码的作用:

执行多个命令以完成(如果(当前秒==几秒)停止)。 CommandProcessor(函子)运行此命令。我试图将这个类的指针抛出到pthread_create

问题是:

是否可以将类对象用作可调用对象

命令.h

#ifndef COMMAND_H
#define COMMAND_H

#include <iostream>
#include "commandprocessor.h"

class CommandProcessor;

using namespace std;

class Command
{
    static CommandProcessor *commandProcessor;
    time_t stopSeconds;

    public:
        Command(int _stopSeconds);
        static void setProcessor(CommandProcessor *_commandProcessor);
        void execute();
};

#endif // COMMAND_H
main.cpp

#include <iostream>
#include <pthread.h>
#include "commandprocessor.h"

#define MAX_THREADS 2

using namespace std;

int main(int argc, char **args)
{
    CommandProcessor *processor = new CommandProcessor();
    Command::setProcessor(processor);
    processor->addCommand(new Command(53));
    processor->addCommand(new Command(24));
    processor->addCommand(new Command(15));

    pthread_t threads[MAX_THREADS];
    for(int i=0; i < MAX_THREADS; i++)
    {
        pthread_create(&threads[i], NULL, processor(), NULL); // error: 'processor' cannot be used as a function
    }
    return 0;
}
#包括
#包括
#包括“commandprocessor.h”
#定义最大线程数2
使用名称空间std;
int main(int argc,char**args)
{
CommandProcessor*处理器=新的CommandProcessor();
命令::setProcessor(处理器);
处理器->添加命令(新命令(53));
处理器->添加命令(新命令(24));
处理器->添加命令(新命令(15));
pthread_t线程[最大线程];
对于(int i=0;i<代码> > P> <强>免责声明:这是C++中不应该做的事情,因为它包括围绕代码< >空*>代码>。这里需要使用它,因为它与C API交互。如果可以,请使用C++11或使用较旧的编译器。

回答您的问题:是的,一旦您为类对象实现了
operator()
,将类对象作为可调用对象(作为“函数”)就完全可以了。你已经这么做了,但是问题是需要实现C++和C线程之间的接口,需要额外的代码。 这里出现“
处理器
不能用作函数”错误的主要原因是
处理器
是一个
命令处理器*
,因此需要取消引用。但是,即使执行
(*processor)(
也不会有帮助,因为
pthread\u create
的定义如下:

int pthread_create(phread_t *thread, const pthread_attr_t *attr,void *(*start_routine) (void *), void *arg);
这意味着要执行的函数必须返回
void*
并取
void*
。文档中还指出,
start\u例程
的参数值为
arg
,最初用于调用
pthread\u create

但是,您不能将指向
CommandProcessor::operator()()
的指针传递给
pthread\u create
,原因更为重要:它是一个成员函数。所有非
静态
类成员函数都需要使用对象调用,以便它们可以在其体内引用

问题的解决方案是创建一个静态函数,该函数将匹配
pthread\u create
所需的原型,并将指向
CommandProcessor
的指针作为
pthread\u create
arg
传递。看看这个:

static void* processor_executor(void *arg)
{
    CommandProcessor *processor = static_cast<CommandProcessor*>(arg);
    (*processor)();
    return NULL;
}

int main(int argc, char **argv)
{
    /* ... */
    for(int i=0; i < MAX_THREADS; i++)
    {
        pthread_create(&threads[i], NULL, processor_executor, static_cast<void*>        (processor));
    }
    /* ... */
}
static void*处理器\执行器(void*arg)
{
CommandProcessor*processor=static_cast(arg);
(*处理器)();
返回NULL;
}
int main(int argc,字符**argv)
{
/* ... */
对于(int i=0;i
这样,
operator()
pthread\u create
创建的线程内执行,使用与
main()
中相同的
CommandProcessor
对象


请记住,从多个并行线程中访问相同的数据将导致数据争用。使用互斥、信号量或条件变量来保护数据不被并行访问。

< P> <强>免责声明:这是C++中不应该做的事情,因为它包括围绕代码< >空*> /COD>。这里需要使用它,因为它与C API交互。如果可以,请使用C++11或使用较旧的编译器。

回答您的问题:是的,一旦您为类对象实现了
operator()
,将类对象作为可调用对象(作为“函数”)就完全可以了。你已经这么做了,但是问题是需要实现C++和C线程之间的接口,需要额外的代码。 这里出现“
处理器
不能用作函数”错误的主要原因是
处理器
是一个
命令处理器*
,因此需要取消引用。但是,即使执行
(*processor)(
也不会有帮助,因为
pthread\u create
的定义如下:

int pthread_create(phread_t *thread, const pthread_attr_t *attr,void *(*start_routine) (void *), void *arg);
这意味着要执行的函数必须返回
void*
并取
void*
。文档中还指出,
start\u例程
的参数值为
arg
,最初用于调用
pthread\u create

但是,您不能将指向
CommandProcessor::operator()()
的指针传递给
pthread\u create
,原因更为重要:它是一个成员函数。所有非
静态
类成员函数都需要使用对象调用,以便它们可以在其体内引用

问题的解决方案是创建一个静态函数,该函数将匹配
pthread\u create
所需的原型,并将指向
CommandProcessor
的指针作为
pthread\u create
arg
传递。看看这个:

static void* processor_executor(void *arg)
{
    CommandProcessor *processor = static_cast<CommandProcessor*>(arg);
    (*processor)();
    return NULL;
}

int main(int argc, char **argv)
{
    /* ... */
    for(int i=0; i < MAX_THREADS; i++)
    {
        pthread_create(&threads[i], NULL, processor_executor, static_cast<void*>        (processor));
    }
    /* ... */
}
static void*处理器\执行器(void*arg)
{
CommandProcessor*processor=static_cast(arg);
(*处理器)();
返回NULL;
}
int main(int argc,字符**argv)
{
/* ... */
对于(int i=0;i
这样,
operator()
pthread\u create
创建的线程内执行,使用与
main()
中相同的
CommandProcessor
对象

请记住,从多个并行线程中访问相同的数据将导致
static void* processor_executor(void *arg)
{
    CommandProcessor *processor = static_cast<CommandProcessor*>(arg);
    (*processor)();
    return NULL;
}

int main(int argc, char **argv)
{
    /* ... */
    for(int i=0; i < MAX_THREADS; i++)
    {
        pthread_create(&threads[i], NULL, processor_executor, static_cast<void*>        (processor));
    }
    /* ... */
}