Hello c++ fellows,
I have experience in C++, but I never had to work with multi-threading before, so I thought I consult you and ask for help.
My application receives a lot of data (about 250MB/min) and needs to process this data in 2 steps. Because it is so much I need to optimize everything and would like to use multiple cores (if present).
First, I am a bit confused about multi-cores and threads. Sometimes I read that threads are automatically assigned to multiple cores and sometimes I read that even if multiple cores are presents, the threads just run on one. Can you tell me what is actually the case?
Now to my problem in particular. Here you can see all relevant code to understand my problem:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57
|
class MyChannel
{
private:
unsigned int id;
std::vector<MyRect> record;
unsigned int recordPos;
std::vector<MyRect> recordProcessBuffer;
std::vector<ProcessedRect> recordFurtherProcessingBuffer;
public:
void AddRecord(MyRect &newRect);
void ProcessData();
void FurtherProcessData();
};
void MyChannel::AddRecord(MyRect &newRect)
{
if(recordPos == maxRecords -1 )
{
std::copy(record.begin(), record.begin()+maxRecords, recordProcessBuffer.begin());
recordPos=0;
//ProcessData();
return;
}
record[recordPos]=newRect;
recordPos++;
}
void MyChannel::ProcessData()
{
//Do something with recordProcessBuffer and fill recordFurtherProcessingBuffer
}
void MyChannel::FurtherProcessData()
{
//Do something with recordFurtherProcessingBuffer
}
class MySystem
{
private:
std::vector<MyChannel> channel;
public:
static void Callback( void* myClass, MyRect* records, int NumRecords, int ChannelID );
};
void MySystem::Callback( void* myClass, MyRect* record, int NumRecords, int ChannelID )
{
MySystem *thisClass = (MySystem*) myClass;
for (int i=0; i < NumRecords; i++)
{
thisClass->channel[ChannelID].AddRecord(record[i]);
}
}
|
MySystem can generate up to 128 channels. My Callback function receives records for a particular channel and adds the records to the channel to save them in "record". If a particular number of records are received (maxrecords) I want to copy the data in a buffer to process it. Instead of polling for the number of records received so far, I thought it is easiest to call the ProcessData() function from within the AddRecord function if maxrecords is reached. But then I wouldn't receive any new records in this particular channel, until the ProcessData function returns, and that can take time. This is why I need to use multi threading. Now I was wondering, what would be the most efficient way to do multi threading in this case, because of the huge amount of data I need to optimize where I can. So I thought about 2 things: To generate a thread in AddRecord() and let the thread do ProcessData(). However, then I end up with up to 128 threads because the time maxrecords is reached is almost equal for all channels. And I could imagine, that this would procude so much overhead that I wouldn't optimize my performance but decrease it (but I don't know because as I said, I have no idea about threads). The other option I thought about is to have 3 threads: One for recieving data, one for processing data and one for further processing. And all channels would use them. However as far as I understand, a thread is deleted once the function that should be executed by the thread returns, but I want to keep them open to let them process another function a few milliseconds later...but I don't think this is possible. So I don't really know what I should do now. The questions that bother me most are:
*How do I gain the optimal performance with threads for my problem on a multi-core processor.
*What libary gives me the best performance for doing this.
*And how excatly do I implement the threading (3 threads total or 3 threads per channel).
Regarding my platform: Due to the API that sends me the records, I have to use the Visual C++ Express compiler. Though, normally I prefer to stay cross-platform and I also have no experience with the WIN API.
I would really appreciate comments and suggestions.
Have a nice day
Mantrid