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

Protect against spurious wakeups #34

Closed
elfring opened this issue Aug 31, 2013 · 37 comments
Closed

Protect against spurious wakeups #34

elfring opened this issue Aug 31, 2013 · 37 comments

Comments

@elfring
Copy link

elfring commented Aug 31, 2013

I have seen that no loop is used around a call of the function "pthread_cond_wait".
Would you like to reuse anything from my message on the topic "spurious wakeup"?

@cloudwu
Copy link
Owner

cloudwu commented Aug 31, 2013

The worker thread calling pthread_cond_wait do some simple work: it checks the message queue continuously , if no new message , it sleeps awhile , otherwise it dispatch a message from the queue.

Pthread_cond_wait is not important and not necessary. It's in the loop of message dispatching, and message queue is thread safe, we can do the job any times even if message queue is empty. So it don't care about why it be waked up.

@cloudwu cloudwu closed this as completed Aug 31, 2013
@elfring
Copy link
Author

elfring commented Sep 1, 2013

I would prefer that the shown programming mistake will be corrected. The predicate which should be checked together with the functiion "pthread_cond_wait" should be specified.

@cloudwu
Copy link
Owner

cloudwu commented Sep 1, 2013

I don't care why and when pthread_cond_wait could be wake up, even remove pthread_cond_wait is also right.
Why must check it in a loop ?

@elfring
Copy link
Author

elfring commented Sep 1, 2013

I guess that you want also to wait (put a thread to sleep) only if a specific condition was not present.

@cloudwu
Copy link
Owner

cloudwu commented Sep 1, 2013

The signal to wake up slept thread will be send by timer continuously without any condition.
And the worker threads seldom sleep in my system, because the message queues are always not empty.

@elfring
Copy link
Author

elfring commented Sep 1, 2013

Would you like to wait for the predicate that "a queue" is empty or full?

@cloudwu
Copy link
Owner

cloudwu commented Sep 1, 2013

No. Send signal will kill the performance and increase the complexity .

The queue seldom empty. I check the queue directly in the worker thread loop like a spin lock.

@elfring
Copy link
Author

elfring commented Sep 1, 2013

You use this programming interface in an incomplete way. Would you like to recheck any recommendations from other information sources?

@cloudwu
Copy link
Owner

cloudwu commented Sep 1, 2013

If you read the source code, you can find skynet use a 2 level message queue. The only way to know whether the queue is empty is try to pop a message.

And I don't care whether the signal send out or the thread capture the signal.

If you don't like it, you can simply remove all the pthread_wait_cond and pthread_cond_signal, the program will works fine, too.

@elfring
Copy link
Author

elfring commented Sep 1, 2013

Why do you use such functions when you expect that this program should also work without them?

@cloudwu
Copy link
Owner

cloudwu commented Sep 1, 2013

Sometimes there are not so many works to do, so some workers can sleep a while , it's optional.

There are many worker thread in skynet, and the work of threads are the same, so I don't care which one sleeps and which one wake up.

@elfring
Copy link
Author

elfring commented Sep 1, 2013

How do you think about to become a bit more aware of the proper handling for the involved predicates?

@cloudwu
Copy link
Owner

cloudwu commented Sep 1, 2013

the queue seldom empty, so the worker thread simply do again in any situation even the error raised from pthread api is ok.

Do more is harmless to this system and the code will be simple.

@elfring
Copy link
Author

elfring commented Sep 1, 2013

I prefer software correctness over "simplicity" for this use case.

@cloudwu
Copy link
Owner

cloudwu commented Sep 1, 2013

I think it's correct now because any error from pthread_cond_wait is
harmless.

2013/9/1 Markus Elfring notifications@github.com

I prefer software correctness over "simplicity" for this use case.


Reply to this email directly or view it on GitHubhttps://github.com//issues/34#issuecomment-23626740
.

http://blog.codingnow.com

@elfring
Copy link
Author

elfring commented Sep 1, 2013

No. - I see still a need for further improvements to make the applied algorithms really correct.

@cloudwu
Copy link
Owner

cloudwu commented Sep 1, 2013

If you can find how and when it is wrong, you can make a test case to show it.

In this place, pthread_cond_wait can be call once or more times or remove it (don't call it), the algorithms are always correct. So I don't care it success or failed.

@elfring
Copy link
Author

elfring commented Sep 1, 2013

I find your view questionable.

  • Such a failed function call might mean that an other thread tampered with the referenced data structures in unexpected ways.
  • I get the impression that you should reconsider the predicate handling (e. g. loops for condition checks) in this software.

@cloudwu
Copy link
Owner

cloudwu commented Sep 3, 2013

I guess you don't read other parts of code, so you don't know why I can ignore the failed function call.

pthread_cond_wait here only let the cpu/thread sleep a while , nothing else. and pthread_cond_signal only suggest (not must) it can wake up.

As I said before, sleeping is optional. Whether the sleeping really happened is not important.

@elfring
Copy link
Author

elfring commented Sep 3, 2013

Would you like to delete unneeded source code?

@cloudwu
Copy link
Owner

cloudwu commented Sep 3, 2013

It's not unneeded . Just like a cpu, if it detects few work to do , it works at lower frequency automatic. Whether it works at low or high frequency, both can do work correctly .

@elfring
Copy link
Author

elfring commented Sep 3, 2013

If you do not want to get rid of the "optional sleeping", I would prefer that your use of condition variables will become complete and correct.

@cloudwu
Copy link
Owner

cloudwu commented Sep 3, 2013

Do you mean use semaphore (sem__) instead of pthread_cond__ ? I think no difference between them to my requirement , but pthread api is more portable.

@elfring
Copy link
Author

elfring commented Sep 3, 2013

I do not suggest to change to an alternative function interface here. It seems that your desire for a thread which should sleep occasionally does not fit to a correct implementation so far. I hope that the needed predicates will become clearer for your software.

@cloudwu
Copy link
Owner

cloudwu commented Sep 3, 2013

The thread can sleep or not sleep as it like, the signal is only the suggestion. so it doesn't care the predicate.
I think the code is simple and clear enough now, and it's correct. If it is wrong, you can make a test case to show it .

@elfring
Copy link
Author

elfring commented Sep 3, 2013

I have got a different opinion. I can not show "an error demonstration" by a test case for this special situation. The real problem seems to be the detail that you do not care for the needed predicates as much as I try to recommend here. I hope that other software developers can eventually convince you to add specific loops around each call of the function "pthread_cond_wait".

@cloudwu
Copy link
Owner

cloudwu commented Sep 3, 2013

I am sure that whatever the "pthread_cond_wait" returns (even raise an error) , the worker thread can do things right. So I don't need capture the error.

@elfring
Copy link
Author

elfring commented Sep 3, 2013

Please distinguish the issues "completion of error handling" and "correct use of condition variables".

Do you expect the semantics "wake-only-once" here?

@cloudwu
Copy link
Owner

cloudwu commented Sep 3, 2013

No. As I said before, removing "pthread_cond_wait" is no harmful either . So wake up multi-times is ok.

@elfring
Copy link
Author

elfring commented Sep 3, 2013

Why are you not going to remove the "pthread_cond_..." stuff then?

@cloudwu
Copy link
Owner

cloudwu commented Sep 3, 2013

I explained many times, so i don't want to say more. Read the code if you want to know the reason.

@elfring
Copy link
Author

elfring commented Sep 3, 2013

I miss still your real explanation so far.

@elfring
Copy link
Author

elfring commented Sep 3, 2013

For which conditions would you like to wait by the discussed variable?

@cloudwu
Copy link
Owner

cloudwu commented Sep 4, 2013

The "pthread_cond_wait" here only sleeps a while and maybe wake up by "pthread_cond_signal"
I don't care about "spurious wakeup" because the main loop of worker thread check queue continuously without any condition.

Worker threads can do their jobs in any condition. they may sleep a while when they find not many jobs need to do. The number of jobs is not the condition either. In the worst case, every threads sleep a little time and signals are not send out immediately, and the messages delay a while. But the system can still works fine (every message will be dispatch later).

Maybe use eventfd or semaphore is better than pthread_cond because there is no condition need to check here. Do you have any new suggestion ?

@elfring
Copy link
Author

elfring commented Sep 4, 2013

Condition variables are not the appropriate means to wait without regarding predicates.

  • Drop the "pthread_cond_..." stuff from the affected source file to make it cleaner.
  • If you insist to use them, I recommend to add corresponding loops with dedicated checks.

@cloudwu
Copy link
Owner

cloudwu commented Sep 4, 2013

I don't need checks, and nothing need check.

I can't find another pthread api can do this.

You never told me if I don't check what's the bad thing will happen. I know my system clearly and I know what's the "spurious wakeup". I recommend you to read the source code and then give a pull request or fork the project to modify it as you like.

@elfring
Copy link
Author

elfring commented Sep 4, 2013

I do not know the intended software design good enough to offer a Git pull request. But I interpret the affected source code in the way that the predicate handling should be improved. I find it bad to overlook relevant conditions.

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

2 participants