我正在尝试在我的C++应用程序上使用线程.
我的代码是:
#include <iostream>
#include <thread>
class C
{
public:
void * code( void * param )
{
std::cout << "Code thread executing " << std::endl;
return NULL;
}
};
int main()
{
C c;
std::thread t ( &C::code, &c );
t.join();
}
Run Code Online (Sandbox Code Playgroud)
编译时,我得到了这些错误:
In file included from /opt/centos/devtoolset-1.0/root/usr/lib/gcc/x86_64-redhat-linux/4.7.0/../../../../include/c++/4.7.0/bits/move.h:57:0,
from /opt/centos/devtoolset-1.0/root/usr/lib/gcc/x86_64-redhat-linux/4.7.0/../../../../include/c++/4.7.0/bits/stl_pair.h:61,
from /opt/centos/devtoolset-1.0/root/usr/lib/gcc/x86_64-redhat-linux/4.7.0/../../../../include/c++/4.7.0/bits/stl_algobase.h:65,
from /opt/centos/devtoolset-1.0/root/usr/lib/gcc/x86_64-redhat-linux/4.7.0/../../../../include/c++/4.7.0/bits/char_traits.h:41,
from /opt/centos/devtoolset-1.0/root/usr/lib/gcc/x86_64-redhat-linux/4.7.0/../../../../include/c++/4.7.0/ios:41,
from /opt/centos/devtoolset-1.0/root/usr/lib/gcc/x86_64-redhat-linux/4.7.0/../../../../include/c++/4.7.0/ostream:40,
from /opt/centos/devtoolset-1.0/root/usr/lib/gcc/x86_64-redhat-linux/4.7.0/../../../../include/c++/4.7.0/iostream:40,
from C.cpp:1:
/opt/centos/devtoolset-1.0/root/usr/lib/gcc/x86_64-redhat-linux/4.7.0/../../../../include/c++/4.7.0/type_traits: In instantiation of 'struct std::_Result_of_impl<false, false, std::_Mem_fn<void* (C::*)(void*)const>, C*>':
/opt/centos/devtoolset-1.0/root/usr/lib/gcc/x86_64-redhat-linux/4.7.0/../../../../include/c++/4.7.0/type_traits:1857:12: required from 'class std::result_of<std::_Mem_fn<void* (C::*)(void*)const>(C*)>'
Run Code Online (Sandbox Code Playgroud)
还有更多...... …
我想了解为什么动态分配多次调用的数据使用的内存比在代码上直接指定的内存或通过单次调用分配的那样多malloc.
例如,我在C中制作了以下两个代码:
test1.c:int x分配有malloc
int main (void)
{
int *x;
int i, n=1048576; //n=1024*1024;
printf("size = %lu\n", n* sizeof(int));
for(i=0; i<n; i++)
{
x = malloc(sizeof(int));
*x=i;
}
printf("Look at top and then press something to finish.");fflush(stdout);
getc(stdin);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
我没有在这里免费使用它来保持简单.当程序等待交互时,我查看另一个终端中的top函数,它显示了这个:
PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND
1384 root 20 0 41300 34076 1300 S 0.0 3.3 0:00.47 test1
Run Code Online (Sandbox Code Playgroud)
test2.c:int x未动态分配
int main (void)
{ …Run Code Online (Sandbox Code Playgroud) c malloc memory-management dynamic-memory-allocation static-memory-allocation