LinuxSir.cn,穿越时空的Linuxsir!

 找回密码
 注册
搜索
热搜: shell linux mysql
查看: 781|回复: 8

还是线程数量的问题!

[复制链接]
发表于 2004-6-6 01:45:08 | 显示全部楼层 |阅读模式
一个进程所能创建的线程数量是有限的,即使不是同时运行
也是不能的,是不是系统设置了什么标志,能不能更改!
发表于 2004-6-6 08:01:24 | 显示全部楼层
理论上我是没有听过线程的最大数量, 至于threads到底谁先运作, 谁后运作, 还是一起运作, 应该是OS来控制的吧
发表于 2004-6-6 10:00:48 | 显示全部楼层

回复: 还是线程数量的问题!

最初由 小锁 发表
一个进程所能创建的线程数量是有限的,即使不是同时运行
也是不能的,是不是系统设置了什么标志,能不能更改!


please read: /usr/share/doc/glibc-doc/FAQ.linuxthreads.html

FYI, you can also change the thread limit of your Linux (> 2.3.x) as root, like
echo 40000 > /proc/sys/kernel/threads-max
 楼主| 发表于 2004-6-6 10:44:40 | 显示全部楼层

  1. #include"unixnet.h"


  2. pthread_t tmp;
  3. void * thread(void* arg)
  4. {
  5.         printf("this is the child thread\n");
  6. };
  7. int main()
  8. {
  9.         int thread_count = 0;
  10.         while(1)
  11.         {
  12.                 if(pthread_create(&tmp,NULL,&thread,NULL) == 0)
  13.                 {
  14.                         thread_count ++;
  15.                         printf("the thread_count is %d\n", thread_count);
  16.                         continue;
  17.                         if(thread_count > 10000)
  18.                                 break;
  19.                 }
  20.                  sleep(1);

  21.         }
  22.         return 0;
  23. }

复制代码

例如上面的程序,每当到达254后就会停止创建线程,即使原有线程退出也不行。
发表于 2004-6-6 10:51:11 | 显示全部楼层

请在发问前,阅读我推荐的文档,谢谢

D.5: When I'm running a program that creates N threads, top or ps display N+2 processes that are running my program. What do all these processes correspond to?
Due to the general "one process per thread" model, there's one process for the initial thread and N processes for the threads it created using pthread_create. That leaves one process unaccounted for. That extra process corresponds to the "thread manager" thread, a thread created internally by LinuxThreads to handle thread creation and thread termination. This extra thread is asleep most of the time.

D.10: My application needs to create thousands of threads, or maybe even more. Can I do this with LinuxThreads?
No. You're going to run into several hard limits:

    * Each thread, from the kernel's standpoint, is one process. Stock Linux kernels are limited to at most 512 processes for the super-user, and half this number for regular users. This can be changed by changing NR_TASKS in include/linux/tasks.h and recompiling the kernel. On the x86 processors at least, architectural constraints seem to limit NR_TASKS to 4090 at most.
 楼主| 发表于 2004-6-6 10:53:50 | 显示全部楼层
呵呵,不好意思我的系统没有你说的文档,呵呵!
That extra process corresponds to the "thread manager" thread, a thread created internally by LinuxThreads to handle thread creation and thread termination. This extra thread is asleep most of the time.

是不是这个原因?
那么有没有办法更改呢,如果没有,我想用线程取代进程运行在网络服务器上是不适合的!
发表于 2004-6-6 11:20:32 | 显示全部楼层
那么您可以:
info thread_join
它会告诉你应该 join 一把,否则系统是不会让你再 create 的

至于少的那两个进程,它们就是“thread manager" threads。

btw,若是 您的机器上没有这些文档,您不妨用 google 搜索一下。我找到了:
http://200.48.142.185/doc/glibc-doc/

又及,这种情况和 zombie 进程倒有几分相似呢。
 楼主| 发表于 2004-6-6 12:40:18 | 显示全部楼层
谢谢问题解决了,不过线程有没有类似于进程的wait()函数呀!
pthread函数用起来不是很方便的呀!因为如果想同时运行多个线程,
当其中某个线程退出了,就有机会新创建线程了!但是因为不知道哪个线程会先退出,所以用pthread_join()不是很好呀!
大哥有没有解决方法,或者是其他的函数可以用呀!
 楼主| 发表于 2004-6-6 14:09:47 | 显示全部楼层
呵呵,原来还有个pthread_detach()可以用呀!
您需要登录后才可以回帖 登录 | 注册

本版积分规则

快速回复 返回顶部 返回列表