计算创建 n 个线程所花费的时间
calculating time taken to create n threads
我正在尝试创建 1000 个线程,以便我可以知道创建它们需要多少时间。我正在使用 pthreads。但我收到分段错误:11。
谷歌搜索告诉我它可能是使用过多内存的情况,但我怀疑这是可能的原因。
关于可能是什么原因的任何指示?
代码:
int main(int argc , char *argv[])
{
int *i; // Matti's answer below: ... = (int*)malloc(sizeof(int));
*i = 0;
while( *i < 100)
{
pthread_t thread_id;
puts("Connection accepted");
if( pthread_create( &thread_id , NULL , connection_handler , (void*) &i) < 0)
{
error("could not create thread");
return 1;
}
//pthread_detach(thread_id);
*i = *i + 1;
}
return 0;
}
void *connection_handler(void *i)
{
sleep(1);
return 0;
}
您的问题是您取消引用了一个从未初始化的指针:
int *i;
*i = 0;
只是 int i;
有什么问题?
Googling it tells me that it might the case of using too much memory
在 Ubuntu 15.10 和 g++ v5.2.1 中,
default stack size per thread is 8M bytes
因此,1000 * 8M 可能多达 8G 字节。
我的旧 Dell 总共只有 4G 字节的 dram。我认为这可能意味着超过 1/2 的线程堆栈将滚动 into/out 交换分区。
不确定您是否想花时间测量它或担心它。
顺便说一句,线程上下文切换非常慢,比 function/method 调用慢大约 3 个数量级...明智地使用它们。
在我的旧戴尔上 - 使用 c++_11 线程和 std::mutex:
50 nano seconds per std::mutex lock and std::mutex::unlock
~12,000 nano seconds per context switch enforced by std::mutex
我在上面的代码片段中没有看到的是 ::pthread_exit()。您可能可以对创建和退出采取合理的措施...也许您还打算在 运行 内存不足之前退出每个线程?
更新 - 使用 posix
获取线程堆栈大小
void stackShow() // posix thread stack size
{
pthread_attr_t tattr;
int stat = pthread_attr_init (&tattr);
assert(0 == stat);
size_t size;
stat = pthread_attr_getstacksize(&tattr, &size);
assert(0 == stat);
std::cout << " ----------------------------------------------------\n"
<< " getstacksize: (" << stat << ") size is " << size
<< "\n\n";
stat = pthread_attr_destroy(&tattr);
assert(0 == stat);
}
我正在尝试创建 1000 个线程,以便我可以知道创建它们需要多少时间。我正在使用 pthreads。但我收到分段错误:11。 谷歌搜索告诉我它可能是使用过多内存的情况,但我怀疑这是可能的原因。
关于可能是什么原因的任何指示?
代码:
int main(int argc , char *argv[])
{
int *i; // Matti's answer below: ... = (int*)malloc(sizeof(int));
*i = 0;
while( *i < 100)
{
pthread_t thread_id;
puts("Connection accepted");
if( pthread_create( &thread_id , NULL , connection_handler , (void*) &i) < 0)
{
error("could not create thread");
return 1;
}
//pthread_detach(thread_id);
*i = *i + 1;
}
return 0;
}
void *connection_handler(void *i)
{
sleep(1);
return 0;
}
您的问题是您取消引用了一个从未初始化的指针:
int *i;
*i = 0;
只是 int i;
有什么问题?
Googling it tells me that it might the case of using too much memory
在 Ubuntu 15.10 和 g++ v5.2.1 中,
default stack size per thread is 8M bytes
因此,1000 * 8M 可能多达 8G 字节。
我的旧 Dell 总共只有 4G 字节的 dram。我认为这可能意味着超过 1/2 的线程堆栈将滚动 into/out 交换分区。
不确定您是否想花时间测量它或担心它。
顺便说一句,线程上下文切换非常慢,比 function/method 调用慢大约 3 个数量级...明智地使用它们。
在我的旧戴尔上 - 使用 c++_11 线程和 std::mutex:
50 nano seconds per std::mutex lock and std::mutex::unlock
~12,000 nano seconds per context switch enforced by std::mutex
我在上面的代码片段中没有看到的是 ::pthread_exit()。您可能可以对创建和退出采取合理的措施...也许您还打算在 运行 内存不足之前退出每个线程?
更新 - 使用 posix
获取线程堆栈大小void stackShow() // posix thread stack size
{
pthread_attr_t tattr;
int stat = pthread_attr_init (&tattr);
assert(0 == stat);
size_t size;
stat = pthread_attr_getstacksize(&tattr, &size);
assert(0 == stat);
std::cout << " ----------------------------------------------------\n"
<< " getstacksize: (" << stat << ") size is " << size
<< "\n\n";
stat = pthread_attr_destroy(&tattr);
assert(0 == stat);
}