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

  免費(fèi)注冊(cè) 查看新帖 |

Chinaunix

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

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

論壇徽章:
0
跳轉(zhuǎn)到指定樓層
1 [收藏(0)] [報(bào)告]
發(fā)表于 2006-04-18 17:41 |只看該作者 |倒序?yàn)g覽
請(qǐng)看我的線程代碼:


  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ù)制代碼


這個(gè)是我的子線程,運(yùn)行起來(lái)后,我在主線程里調(diào)用pthread_cancel, 停掉子線程,其中的buff會(huì)被free嗎?

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


  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ù)制代碼


這個(gè)是我的子線程,運(yùn)行起來(lái)后,我在主線程里調(diào)用pthread_cancel, 停掉子線程,其中的buff會(huì)被free嗎?


不會(huì)。

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


即使Cancelability Type為PTHREAD_CANCEL_ASYNCHRONOUS也不會(huì),
因?yàn)閣hile(1)是死循環(huán)。

你最好在free(buff);后加個(gè)輸出信息的語(yǔ)句檢驗(yàn)一下。


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 [報(bào)告]
發(fā)表于 2006-04-18 20:41 |只看該作者
stuying...

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

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

論壇徽章:
0
5 [報(bào)告]
發(fā)表于 2006-04-19 12:15 |只看該作者
代碼中的while(1)在我們的項(xiàng)目中是一個(gè)block方式的read,因?yàn)槟承┰虮仨毷褂胋lock方式,就會(huì)出現(xiàn)上面的情況! 線程的停止要靠pthread_cancel來(lái)實(shí)現(xiàn);

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


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].
您需要登錄后才可以回帖 登錄 | 注冊(cè)

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

  

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

清除 Cookies - ChinaUnix - Archiver - WAP - TOP