编程随想录
CSDN拆迁户 @2014-04-07

导航

 

下午调试程序的“segmentation fault”,先上代码:

 

 

左看右看找不到错在那,尝试去掉pthread_join, 竟然正常了。

 

注意这句:

sock_listen_tid = pthread_create(&sock_listen_tid,NULL,listen_thread,NULL);

 

pthread_create的返回值赋给了sock_listen_tid,这行代码看起来的确的确的确有些怪~

 

看函数说明 man 3 pthread_create

 

函数返回值描述如下:

 

RETURN VALUE

       If successful, the pthread_create() function shall return  zero;  otherwise,

       an error number shall be returned to indicate the error.


ERRORS

       The pthread_create() function shall fail if:


       EAGAIN The  system  lacked the necessary resources to create another thread,

              or the system-imposed limit on the  total  number  of  threads  in  a

              process {PTHREAD_THREADS_MAX} would be exceeded.


       EINVAL The value specified by attr is invalid.


       EPERM  The  caller  does not have appropriate permission to set the required

              scheduling parameters or scheduling policy.


       The pthread_create() function shall not return an error code of [EINTR].


       The following sections are informative.

 

 

如果创建成功, pthread_create将返回0. 赋值给sock_listen_tid等于0。然后执行pthread_join(0, NULL),相当于等待线程号为0的线程,当然segmentation fault

 

 

posted on 2011-03-04 16:13  dos5gw  阅读(769)  评论(0)    收藏  举报