Having a function that returns int how can I run it in a separate thread using boost?
I know it looks not necessary but I hope that it would help me find memory leak. So having a function inside a class that returns int,开发者_运维技巧 how can I call it from another function of that class (call it so that function that returns int would run in another thread)?
You are trying to find a memory leak in a function by having it called from another thread? That is like trying to find a needle in a haystack by adding more hay to the stack.
Thread programming 101:
- Spawn a new thread ("thread2") that invokes a new function ("foo").
- Have the original thread join against thread2 immediately after the spawn.
- Read a global variable that foo() has written its final value to.
Notice that foo() cannot return its value to the original thread; it must write the value to some shared memory (ie, global variable). Also note that this will not solve your memory leak problem, or even make it obvious where your memory leak is coming from.
Look for memory leaks with Valgrind. And read a book or tutorial about multithreading.
The operating system will not reclaim memory leaks in worker threads. That's not how it works.
Fix your bugs. The world doesn't need any more crappy software.
精彩评论