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

linux "SetEvent" always return 0, won't it fail? #22

Closed
romanholidaypancakes opened this issue Sep 11, 2021 · 7 comments
Closed

linux "SetEvent" always return 0, won't it fail? #22

romanholidaypancakes opened this issue Sep 11, 2021 · 7 comments

Comments

@romanholidaypancakes
Copy link

when WaitForEvent is not in a waiting state, using setevent should return failure

@mqudsi
Copy link
Member

mqudsi commented Sep 11, 2021

I’m sorry, can you clarify? Setting the event is completely independent (from the outside) from waiting on the event.

Example code that behaves differently on Linux and Windows would be helpful.

@romanholidaypancakes
Copy link
Author

romanholidaypancakes commented Sep 13, 2021

neosmart::while(WaitForEvent(event,-1))){

	// now doing
	do_something();
}
  • when the thread1 is executing do_something, it is not in a waiting state(NOT CALLING WaitForEvent), SetEvent should return an error.
neosmart::SetEvent(event);

@yinsidij
Copy link

@inaryart I tried to repo what you described for SetEvent. However, seems like I didn't see the error. SetEvent for the same event twice would just return 1
Here is my example code
https://github.com/yinsidij/pevents/blob/master/win32/HelloWorld.cpp

Here is the output running in windows (ret = 1 twice, which indicates success)

Thread 14076 waiting for write event...
Thread 18516 sets event...
ret = 1, error = 0
WaitForSingleObject returns 0
Thread 18516 sets event...
ret = 1, error = 0

The output is the same no matter the event is auto-reset or manual-reset or initialState signaled or nonsignaled.
Am I missing anything?

@romanholidaypancakes
Copy link
Author

@inaryart I tried to repo what you described for SetEvent. However, seems like I didn't see the error. SetEvent for the same event twice would just return 1
Here is my example code
https://github.com/yinsidij/pevents/blob/master/win32/HelloWorld.cpp

Here is the output running in windows (ret = 1 twice, which indicates success)

Thread 14076 waiting for write event...
Thread 18516 sets event...
ret = 1, error = 0
WaitForSingleObject returns 0
Thread 18516 sets event...
ret = 1, error = 0

The output is the same no matter the event is auto-reset or manual-reset or initialState signaled or nonsignaled.
Am I missing anything?

you are right, setEvent will be return 1, so i think linux should also return the same value(1), so that I can judge whether the wake-up(setevent) operation failed

@yinsidij
Copy link

I think neosmart::event uses return value of 0 as success, which is oppose to windows convention (return value of 1 as success). So you might want to update up-level checker.

@romanholidaypancakes
Copy link
Author

I think this is normal code, it should be part of the project.

if(setevent(event) == 1){
   // then i known 
    failedCount++;
}

just as my suggestion...

@mqudsi
Copy link
Member

mqudsi commented Oct 13, 2021

I think you are misunderstanding what an error in this context means. The calls are fully asynchronous, and will succeed regardless of whether there is another thread on the other end - a SetEvent() on an already set event will succeed, and it doesn't rendezvous with the thread calling WaitForSingleEvent() before it returns. An error is only something out of the ordinary caused by either an internal bug in the pthreads interfacing or the user providing an invalid argument (e.g. making up a value for the event parameter and passing it in).

The convention on Linux and with the pthreads library (which this library serves as some sort of extension to) is for functions to return error numbers with a return value of zero being no error. Don't check for if (SetEvent(...) == 1) but rather if (SetEvent(...) != 0) because there are no guarantees made that one is (always or the only) error returned.

@mqudsi mqudsi closed this as completed Oct 13, 2021
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

No branches or pull requests

3 participants