开发者

Unknown C expression

I ran across the following line in an example program, and don't know what it is. I imagine it's a function call, but am not sure:

(void) pthread_mutex_init(&bottleneck, &mxattr);    

If it is a function call, why is it preceded with (void)? I've never seen that before. Here's the line in more context:

attr_init(pthread_process, pthread_scope, st开发者_Python百科acksize);    
(void) pthread_mutex_init(&bottleneck, &mxattr);    
barrier_init(&setup_barrier, (2 * ntables) + 1);    

Thanks for the help. The entire program is from this Solaris whitepaper (Appendix D)


It is a normal function call. The (void) part just indicates that the function returns a value and nothing will be done with it. To remove any warnings of about unused return values.

See casting unused return values to void.


POSIX Threads

The specific call - pthread_mutex_init - returns int. The cast to void is probably done to avoid specific warning about the return value being ignored during either compile time or a static code analysis.

Update: As @Jack Kelly commented on another answer, any good static analysis tool would simply ignore this cast and continue issuing the warning. Static analysis should be controlled through separate specific annotations, not by using language constructs that can affect the compiler output.


This is a function call with explicit casting of the result to void. This is just a development style to hint both compiler and those who read this code that function call is actually returning something that is intentionally ignored. In this example, pthread_mutex_init function returns integer. Some compilers will give you a warning if you call a function marked with warn_unused_result attribute.


Yes it is a function call. The function pthread_mutex_init() initializes a mutex used to prevent multiple threads clobberring some shared resource.

The function returns an int, the (void) is flagging that we are intentionally ignoring the return value. It stops the compiler complaining about unused return values.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜