Geom
Geom

Reputation: 857

Memory issue with threads (tinythread, C++)

I debug a strange memory issue: When a multithreaded algorithm runs in a loop its memory consumption increases with every iteration although the heap checker of of GooglePerformanceTools says there is no leak. Finally I have made a separate minimal program that reproduces the bug. It seems that the threads are the problem:

#include <stdio.h>
#include <iostream>
#include <vector>
#include "tinythread.h"
using namespace std;


int a(0);
void doNothingAtAll(void*)
{
    ++a;
}

void startAndJoin100()
{   
    vector<tthread::thread*> vThreads;
    for(int i=0;i<100;++i)
    {   
        vThreads.push_back(new tthread::thread(doNothingAtAll,NULL));
    }
    while(!vThreads.empty())
    {
            tthread::thread* pThread(vThreads.back());
            pThread->join();
            delete pThread;
            vThreads.pop_back();
    }
}

int main()
{       
    for(int i=0;i<10;++i)
    {
        cout<<"calling startAndJoin100()"<<endl;
        startAndJoin100();
        cout<<"all threads joined"<<endl;
        cin.get();
    }
    return 0;
}

main() calls 10 times startAndJoin100(). It waits for a key stroke after each iteration so that one can take the memory consumption which is (under Ubuntu 17.10, 64-bit):

VIRT
2.1 GB
4 GB
5.9 GB
7.8 GB
9.6 GB
11.5 GB
13.4 GB
15.3 GB
17.2 GB
19.0 GB

Note: C++11 can't be used and the program must compile on Linux and Windows, thus tinythread is used. Minimal test code with Makefile: geom.at/_downloads/testTinyThread.zip

Upvotes: 1

Views: 234

Answers (1)

Geom
Geom

Reputation: 857

I answer my own question, this may be useful for somebody later:

Conclusion:

1) I'd really like to keep TinyThread because C++11 is unavailable (VS2008 and old Linux Systems must be supported) and no additional library shall be linked (TinyThread consists only of an *.h and *.cpp file while Boost and other solutions I know require linking a DLL).

2) Valgrind and the heap checker of the GooglePerformanceTools do not report memory leaks and I have looked into the code - it seems to be correct although the virtual memory consumption increases drastically in the minimal example posted above. It seems that the system does not re-use the previously assigned memory pages and I have not found an explanation for this behavior. Thus I do not blame TinyThread++ but it works when pthreads are used directly instead.

3) The workaround: There is a C alternative called TinyCThread: https://tinycthread.github.io/ that works also for C++ and it does not cause the problems observed with TinyThread++.

Upvotes: 2

Related Questions