ifndef 发表于 2013-06-09 16:58

unix环境高级编程330页代码12-5中的问题

书中的代码 在创建键的时候 pthread_key_create(&可以,free).
使用的西沟函数 free。那么最后的 那个返回 return(envbuf)当函数返回的时候envbuf不是就会被释放吗。那返回的指针指向的内容就没了

井蛙夏虫 发表于 2013-06-09 21:20

回复 1# ifndef
pthread_key_create文档是这样说的:Anoptional destructor function may be associated with each key value.At thread exit, if a key value has a non-NULL destructor pointer,
       and the thread has a non-NULL value associated with that key, the value of the key is set to NULL, and then thefunctionpointedtois
       called with the previously associated value as its sole argument.free是在线程退出的时候调用,例子中只是一个普通的函数退出,不会调用free方法的。


   

linux_c_py_php 发表于 2013-06-11 17:58

那是getenv函数, 不是线程函数.

意思是getenv函数可以使用线程局部存储避免重复分配缓冲区, 同时避免在全局维护该缓冲区.

页: [1]
查看完整版本: unix环境高级编程330页代码12-5中的问题