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_Pthreads_Boost Asio - Fatal编程技术网

C++ 如何在规则的内部调用具有多个线程的函数?

C++ 如何在规则的内部调用具有多个线程的函数?,c++,multithreading,pthreads,boost-asio,C++,Multithreading,Pthreads,Boost Asio,我使用deadline\u计时器和pthread\u t以固定的间隔调用函数。 我需要从pthread返回值,所以我使用线程可连接。 处理该函数大约需要60秒 我喜欢用六个线程每10毫秒调用一次该函数,因此该函数每10毫秒处理一次。尽管我在这里测试了两个线程,但它应该每隔10毫秒运行六个线程 我使函数是线程安全的 Main.c int main() { io_service io; deadline_timer t(io); Wheelchaircontrol w(

我使用
deadline\u计时器和pthread\u t
以固定的间隔调用函数。 我需要从
pthread返回值,所以我使用线程可连接
。
处理该函数大约需要60秒

我喜欢用六个线程每10毫秒调用一次该函数,因此该函数每10毫秒处理一次。尽管我在这里测试了两个线程,但它应该每隔10毫秒运行六个线程

我使函数是线程安全的

Main.c

int main()
{
     io_service io;
     deadline_timer t(io);
     Wheelchaircontrol w(t);
     io.run();        
     std::cout << "Exit" << endl;
}
但时间实例的间隔并不相等。 有时,它们在同一时间完成。
我喜欢在10毫秒的时间间隔内完成所有操作。

您能使用c++11吗?此外,您无法控制线程完成的时间。此外,“我将函数设置为线程安全”告诉我您可以在内部同步线程。这可能会导致某些线程需要更长的时间,因为它们必须等待。
#include <boost/asio.hpp>
#include <boost/bind.hpp>
#include <iostream>

using namespace std;
using namespace boost::asio;

static const int NUM_THREADS = 6;
struct Detectdirection {
    static void* Tracking_helper(void*) {
        return nullptr; // take 60ms in threadsafe manner
    }
};

class Wheelchaircontrol {
  public:
    Wheelchaircontrol(deadline_timer &t_) : t(t_) {
        d = new Detectdirection();
        pthread_attr_init(&attr);
        pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE);
        wait();
    }
    ~Wheelchaircontrol() { delete d; }

    void timeout(const boost::system::error_code &e) {
        for (int t = 0; t < NUM_THREADS; t++) {
            usleep(10000); // 10 msec
            switch (t) {
            case 0:
                rc = pthread_create(&thread[t], &attr, d->Tracking_helper, d);
                if (rc) {
                    printf("ERROR; return code from pthread_create() is %d\n", rc);
                    exit(-1);
                }

                break;
            case 1:
                rc = pthread_create(&thread[t], &attr, d->Tracking_helper, d);
                if (rc) {
                    printf("ERROR; return code from pthread_create() is %d\n", rc);
                    exit(-1);
                }
                break;
            } // switch (t)
        } // for
        for (int t = 0; t < NUM_THREADS; t++) {
            switch (t) {
            case 0:
                rc = pthread_join(thread[t], &ret_status);
                if (!rc) {

                    free(ret_status);
                    gettimeofday(&tp, NULL);
                    long int ms = tp.tv_sec * 1000 + tp.tv_usec / 1000;
                    cout << " time instance from1 " << ms << endl;
                }
                break;
            case 1:
                rc = pthread_join(thread[t], &ret_status);
                if (!rc) {

                    free(ret_status);
                    gettimeofday(&tp, NULL);
                    long int ms = tp.tv_sec * 1000 + tp.tv_usec / 1000;
                    cout << " time instance from2 " << ms << endl;
                }
                break;
            } // switch (t)
        } // for
        wait();
    }

    void cancel() { t.cancel(); }

    void wait() {
        t.expires_from_now(boost::posix_time::milliseconds(1)); // 100msec
        t.async_wait(boost::bind(&Wheelchaircontrol::timeout, this, boost::asio::placeholders::error));
    }

  private:
    int rc;
    deadline_timer &t;
    struct timeval tp;
    Detectdirection *d; // for direction
    // InterfaceUSB *usb;
    pthread_t thread[NUM_THREADS];
    pthread_attr_t attr;
    void *ret_status;
    int distance;
    bool imturn;
    // myMosq *mq;
    // int cnt_mqt;
    // ofstream out;
};
time instance from1 1480056797897
 time instance from2 1480056797957
 time instance from1 1480056798089
 time instance from2 1480056798089
 time instance from1 1480056798156
 time instance from2 1480056798226
 time instance from1 1480056798358
 time instance from2 1480056798358
 time instance from1 1480056798493
 time instance from2 1480056798494
 time instance from1 1480056798557