亚洲av成人无遮挡网站在线观看,少妇性bbb搡bbb爽爽爽,亚洲av日韩精品久久久久久,兔费看少妇性l交大片免费,无码少妇一区二区三区

  免費注冊 查看新帖 |

Chinaunix

  平臺 論壇 博客 文庫
最近訪問板塊 發(fā)新帖
查看: 5215 | 回復(fù): 5
打印 上一主題 下一主題

pthread退出釋放資源的問題! [復(fù)制鏈接]

論壇徽章:
0
跳轉(zhuǎn)到指定樓層
1 [收藏(0)] [報告]
發(fā)表于 2006-04-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. }
復(fù)制代碼


這個是我的子線程,運行起來后,我在主線程里調(diào)用pthread_cancel, 停掉子線程,其中的buff會被free嗎?

論壇徽章:
0
2 [報告]
發(fā)表于 2006-04-18 19:04 |只看該作者
原帖由 nicle 于 2006-4-18 17:41 發(fā)表
請看我的線程代碼:


  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. }
復(fù)制代碼


這個是我的子線程,運行起來后,我在主線程里調(diào)用pthread_cancel, 停掉子線程,其中的buff會被free嗎?


不會。

Cancelability-Enable為PTHREAD_CANCEL_ENABLE(默認)、
Cancelability Type為PTHREAD_CANCEL_DEFERRED(默認)的線程
只能在取消點(Cancellation Points)上響應(yīng)取消請求。
因此你的子線程會在sleep()上退出。


即使Cancelability Type為PTHREAD_CANCEL_ASYNCHRONOUS也不會,
因為while(1)是死循環(huán)。

你最好在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 編輯 ]

論壇徽章:
0
3 [報告]
發(fā)表于 2006-04-18 20:41 |只看該作者
stuying...

論壇徽章:
0
4 [報告]
發(fā)表于 2006-04-19 10:00 |只看該作者
在westgarden正確的前提下,那如何釋放這個buf呢?
在這個程序結(jié)構(gòu)中通過pthread_testcancel來設(shè)置Cancellation Point 是沒有意義的.
我覺得,還是讓線程自己主動的退出才是安全的做法.
可以通過全局變量來指引.

[ 本帖最后由 yeehya 于 2006-4-19 10:03 編輯 ]

論壇徽章:
0
5 [報告]
發(fā)表于 2006-04-19 12:15 |只看該作者
代碼中的while(1)在我們的項目中是一個block方式的read,因為某些原因必須使用block方式,就會出現(xiàn)上面的情況!! 線程的停止要靠pthread_cancel來實現(xiàn);

論壇徽章:
0
6 [報告]
發(fā)表于 2006-04-19 14:56 |只看該作者
原帖由 yeehya 于 2006-4-19 10:00 發(fā)表
在westgarden正確的前提下,那如何釋放這個buf呢?
在這個程序結(jié)構(gòu)中通過pthread_testcancel來設(shè)置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].
您需要登錄后才可以回帖 登錄 | 注冊

本版積分規(guī)則 發(fā)表回復(fù)

  

北京盛拓優(yōu)訊信息技術(shù)有限公司. 版權(quán)所有 京ICP備16024965號-6 北京市公安局海淀分局網(wǎng)監(jiān)中心備案編號:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年舉報專區(qū)
中國互聯(lián)網(wǎng)協(xié)會會員  聯(lián)系我們:huangweiwei@itpub.net
感謝所有關(guān)心和支持過ChinaUnix的朋友們 轉(zhuǎn)載本站內(nèi)容請注明原作者名及出處

清除 Cookies - ChinaUnix - Archiver - WAP - TOP