Chinaunix

标题: pthread退出释放资源的问题! [打印本页]

作者: nicle    时间: 2006-04-18 17:41
标题: pthread退出释放资源的问题!
请看我的线程代码:


  1. void * run(void * arg)
  2. {
  3.    char *buff = NULL;
  4.    
  5.    buff = malloc(SIZE);
  6.    
  7.    while(1){
  8.        sleep(1);
  9.    }   

  10.    free(buff);
  11. }
复制代码


这个是我的子线程,运行起来后,我在主线程里调用pthread_cancel, 停掉子线程,其中的buff会被free吗?
作者: westgarden    时间: 2006-04-18 19:04
原帖由 nicle 于 2006-4-18 17:41 发表
请看我的线程代码:


  1. void * run(void * arg)
  2. {
  3.    char *buff = NULL;
  4.    
  5.    buff = malloc(SIZE);
  6.    
  7.    while(1){
  8.        sleep(1);
  9.    }   

  10.    free(buff);
  11. }
复制代码


这个是我的子线程,运行起来后,我在主线程里调用pthread_cancel, 停掉子线程,其中的buff会被free吗?


不会。

Cancelability-Enable为PTHREAD_CANCEL_ENABLE(默认)、
Cancelability Type为PTHREAD_CANCEL_DEFERRED(默认)的线程
只能在取消点(Cancellation Points)上响应取消请求。
因此你的子线程会在sleep()上退出。


即使Cancelability Type为PTHREAD_CANCEL_ASYNCHRONOUS也不会,
因为while(1)是死循环。

你最好在free(buff);后加个输出信息的语句检验一下。


Cancellation Points
Cancellation points shall occur when a thread is executing the following functions:


accept()
aio_suspend()
clock_nanosleep()
close()
connect()
creat()
fcntl()2
fdatasync()
fsync()
getmsg()
getpmsg()
lockf()
mq_receive()
mq_send()
mq_timedreceive()


mq_timedsend()
msgrcv()
msgsnd()
msync()
nanosleep()
open()
pause()
poll()
pread()
pselect()
pthread_cond_timedwait()
pthread_cond_wait()
pthread_join()
pthread_testcancel()
putmsg()


putpmsg()
pwrite()
read()
readv()
recv()
recvfrom()
recvmsg()
select()
sem_timedwait()
sem_wait()
send()
sendmsg()
sendto()
sigpause()
sigsuspend()


sigtimedwait()
sigwait()
sigwaitinfo()
sleep()
system()
tcdrain()
usleep()
wait()
waitid()
waitpid()
write()
writev()


[ 本帖最后由 westgarden 于 2006-4-18 19:08 编辑 ]
作者: rwen2012    时间: 2006-04-18 20:41
stuying...
作者: yeehya    时间: 2006-04-19 10:00
在westgarden正确的前提下,那如何释放这个buf呢?
在这个程序结构中通过pthread_testcancel来设置Cancellation Point 是没有意义的.
我觉得,还是让线程自己主动的退出才是安全的做法.
可以通过全局变量来指引.

[ 本帖最后由 yeehya 于 2006-4-19 10:03 编辑 ]
作者: nicle    时间: 2006-04-19 12:15
代码中的while(1)在我们的项目中是一个block方式的read,因为某些原因必须使用block方式,就会出现上面的情况!! 线程的停止要靠pthread_cancel来实现;
作者: westgarden    时间: 2006-04-19 14:56
原帖由 yeehya 于 2006-4-19 10:00 发表
在westgarden正确的前提下,那如何释放这个buf呢?
在这个程序结构中通过pthread_testcancel来设置Cancellation Point 是没有意义的.
我觉得,还是让线程自己主动的退出才是安全的做法.
可以通过全局变量来指引.


NAME
pthread_cleanup_pop, pthread_cleanup_push - establish cancellation handlers
SYNOPSIS
[THR]  #include <pthread.h>

void pthread_cleanup_pop(int execute);
void pthread_cleanup_push(void (*routine)(void*), void *arg);


DESCRIPTION
The pthread_cleanup_pop() function shall remove the routine at the top of the calling thread's cancellation cleanup stack and optionally invoke it (if execute is non-zero).

The pthread_cleanup_push() function shall push the specified cancellation cleanup handler routine onto the calling thread's cancellation cleanup stack. The cancellation cleanup handler shall be popped from the cancellation cleanup stack and invoked with the argument arg when:

The thread exits (that is, calls pthread_exit()).

The thread acts upon a cancellation request.

The thread calls pthread_cleanup_pop() with a non-zero execute argument.

These functions may be implemented as macros. The application shall ensure that they appear as statements, and in pairs within the same lexical scope (that is, the pthread_cleanup_push() macro may be thought to expand to a token list whose first token is '{' with pthread_cleanup_pop() expanding to a token list whose last token is the corresponding '}' ).

The effect of calling longjmp() or siglongjmp() is undefined if there have been any calls to pthread_cleanup_push() or pthread_cleanup_pop() made without the matching call since the jump buffer was filled. The effect of calling longjmp() or siglongjmp() from inside a cancellation cleanup handler is also undefined unless the jump buffer was also filled in the cancellation cleanup handler.

The effect of the use of return, break, continue, and goto to prematurely leave a code block described by a pair of pthread_cleanup_push() and pthread_cleanup_pop() functions calls is undefined.

RETURN VALUE
The pthread_cleanup_push() and pthread_cleanup_pop() functions shall not return a value.

ERRORS
No errors are defined.

These functions shall not return an error code of [EINTR].





欢迎光临 Chinaunix (http://bbs.chinaunix.net/) Powered by Discuz! X3.2