Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

commit README,httpd #8

Merged
merged 2 commits into from
Dec 12, 2016
Merged

commit README,httpd #8

merged 2 commits into from
Dec 12, 2016

Conversation

zhuzhuaicoding
Copy link
Contributor

No description provided.

@EZLippi EZLippi merged commit 7abf28e into EZLippi:master Dec 12, 2016
/* accept_request(client_sock); */
if (pthread_create(&newthread , NULL, (void *)accept_request, (void *)(intptr_t)client_sock) != 0)
/* accept_request(&client_sock); */
if (pthread_create(&newthread , NULL, (void *)accept_request, (void *)&client_sock) != 0)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这里为什么又改回去了呢?传递局部变量的地址会发生冲突吧?
修改之前以值传递方式传递,避免了线程之间可能冲突。

#5

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

不能按值传递的吧,我查了一下pthread_create()的函数原型

int pthread_create(pthread_t *thread, const pthread_attr_t *attr, void *(*start_routine) (void *), void *arg);

这里arg是指向要传给start_routine的参数的指针,像你所说的(void *)(intptr_t)client_sock)只是对client_sock进行强制类型转换,得到的并不是指向client_sock的地址,而是一个值为client_sockvoid*指针。

accept_request 中改成 int client = (intptr_t)client_sock这样应该没有问题,但是这样会不会有点意义不明确,难以理解。

所以我还是比较赞同使用从堆空间申请内存的做法。
您怎么看呢?

个人拙见,还请指正。

Copy link
Contributor

@huntinux huntinux May 4, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

你写的很明白了,用堆空间当然可以,只是个人觉得按值传递相对比较简单。^_^
要注意到把局部变量地址暴露出去的危险性就够了。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants