I am working on a program consisting of two parallel threads. One (here, “Clock”) performs some calculations on a regular basis (10 Hz) and uses memory quite intensively. Another (here is a "huge list") uses even more RAM, but is not as critical in time as the first. So I decided to lower the priority to THREAD_PRIORITY_LOWEST. However, when a thread frees up most of the memory it used, the critical cannot save its time.
I managed to condense the problem with this bit of code (make sure optimization is turned off!): While Clock is trying to save a 10-GHz time, a huge stream line allocates and frees more and more memory that is not organized in any pieces .
#include "stdafx.h"
#include <stdio.h>
#include <forward_list>
#include <time.h>
#include <windows.h>
#include <vector>
void wait_ms(double _ms)
{
clock_t endwait;
endwait = clock () + _ms * CLOCKS_PER_SEC/1000;
while (clock () < endwait) {}
}
void hugeList(void)
{
SetThreadPriority(GetCurrentThread(), THREAD_PRIORITY_LOWEST);
unsigned int loglimit = 3;
unsigned int limit = 1000;
while(true)
{
for(signed int cnt=loglimit; cnt>0; cnt--)
{
printf(" Countdown %d...\n", cnt);
wait_ms(1000.0);
}
printf(" Filling list...\n");
std::forward_list<double> list;
for(unsigned int cnt=0; cnt<limit; cnt++)
list.push_front(42.0);
loglimit++;
limit *= 10;
printf(" Clearing list...\n");
while(!list.empty())
list.pop_front();
}
}
void Clock()
{
clock_t start = clock()-CLOCKS_PER_SEC*100/1000;
while(true)
{
std::vector<double> dummyData(100000, 42.0);
printf("delta: %d ms\n", (clock()-start)*1000/CLOCKS_PER_SEC);
start = clock();
wait_ms(100.0);
}
}
int main()
{
DWORD dwThreadId;
if (CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)&Clock, (LPVOID) NULL, 0, &dwThreadId) == NULL)
printf("Thread could not be created");
if (CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)&hugeList, (LPVOID) NULL, 0, &dwThreadId) == NULL)
printf("Thread could not be created");
while(true) {;}
return 0;
}
First of all, I noticed that allocating memory for a linked list is faster than freeing it up. On my machine (Windows7), at approximately the 4th iteration of the "huge list", the Clock-Thread cathode is significantly violated (up to 200 ms). The effect disappears if dummyData-vector "requests" some memory in Clock-Thread.
So,
- Is there a way to increase the memory allocation priority for Clock-Thread in Win7?
- Or do I need to divide both operations into two contexts (processes)?
, , - IPC, .
, 1 , " " boost:: unordered_map ntdll.dll! RtIInitializeCriticalSection.
( systinernals process explorer)
, , 1,4 16 (64- win7).
, . , , . , .
, , . ( ).
, (, ) - threadprivate, .
:
http://bmagic.sourceforge.net/memalloc.html
?
/ ?
http://software.intel.com/en-us/articles/avoiding-heap-contention-among-threads
http://www.boost.org/doc/libs/1_55_0/libs/pool/doc/html/boost_pool/pool/introduction.html