多线程后台线程不活动

时间:2019-06-11 14:20:52

标签: c++ multithreading

首先,我是多线程的初学者,所以对基本错误感到抱歉。

我通过TCP包从5个传感器流式传输数据。每个传感器都会在线程的后台进行初始化并发送数据。

以这种方式对供应商的SDK进行编程:它将软件包放在缓冲区中排队,然后通过套接字将其卸载。如果CPU繁忙并且缓冲区过大(上限为100个数据包),它将丢弃它们并等待CPU卸载。

查看传感器4,这是在同时操作多个线程/传感器时观察到的行为:

Sensor 0: THIS THREAD IS ACTIVE: 0 ID: 120600400447232 on CPU: 0 
Sensor 0: buff Queue size: 1 
Sensor 0: time for unloading buffer: 0ms 
Sensor 2: THIS THREAD IS ACTIVE: 2 ID: 120600400654848 on CPU: 2 
Sensor 2: buff Queue size: 1 
Sensor 2: time for unloading buffer: 0ms 
Sensor 4: buff Queue size: 101 
Sensor 4: Warning: Client dropped packet due to full buffer... 
Sensor 0: THIS THREAD IS ACTIVE: 0 ID: 120600400447232 on CPU: 0 
Sensor 0: buff Queue size: 1 
Sensor 0: time for unloading buffer: 0ms 
Sensor 2: THIS THREAD IS ACTIVE: 2 ID: 120600400654848 on CPU: 2 
Sensor 2: buff Queue size: 1 
Sensor 2: time for unloading buffer: 0ms

此输出一遍又一遍地重复,传感器4的线程在几秒钟内处于非活动状态(我在某些时候也实现了时间戳)。

当然,输出不是线程安全的,但应简要概述正在发生的事情。我将传感器0分配给cpu 0,将传感器1分配给cpu 1,依此类推...程序在线程0和2之间切换,而线程4在完整缓冲区中。短时间激活2-5个传感器后,将出现此现象。只有一个能正常工作。

此时让线程0和2进入睡眠状态不能解决问题。这也是我的lscpu:

x86_64
CPU:12
每个核心的线程数:2
每个插槽的核心数:6
插槽:1

为什么只有两个线程同时处于活动状态?有没有办法告诉特定线程“激活”?还是关于线程结构的一般性问题?

谢谢你!

亚历克斯

// main:
struct *DataArrayGlobal0 {...}; 
struct *DataArrayGlobal1 {...}; 
// ... 5 times

sensor0 = new sensor("192.168.10.200"); 
sensor1 = new sensor("192.168.10.201"); 
// ... 5 times

sensor0->DataArrayGlobal = DataArrayGlobal0;
sensor1->DataArrayGlobal = DataArrayGlobal1;
// ... 5 times

sensor0->init(); 
sensor1->init(); 
// ... 5 times

// Do some other run methods
// To process they copy the data into their own array
// The threads keep running and are not joined yet

sensor0->deInit();
// .... 5 times
// join threads and clean up


class sensor {
private:
    std::thread start_thread;
    struct myDataArray {...};
    std::mutex mutex;
    // some methods
public:
    struct *DataArrayGlobal;
    void init();
    void start_sensor();
    // some methods
}

void sensor::init() 
{ 
    // some init stuff
    start_thread = thread(&start_sensor, this); // this is an infinite loop until invokeShutdown
}

void sensor::start_sensor() 
{ 
    // do some stuff to create data array myDataArray 
    std::lock_guard guard(mutex); 
    std::copy(myDataArray->begin(), myDataArray->end(), DataArrayGlobal); 
}

void sensor::invokeShutdown()
{
    start_thread.join();
}

void sensor::deInit()
{
    invokeShutdown();
}

0 个答案:

没有答案
相关问题