-
Notifications
You must be signed in to change notification settings - Fork 4.1k
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
Comments
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. |
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. |
I don't care why and when pthread_cond_wait could be wake up, even remove pthread_cond_wait is also right. |
I guess that you want also to wait (put a thread to sleep) only if a specific condition was not present. |
The signal to wake up slept thread will be send by timer continuously without any condition. |
Would you like to wait for the predicate that "a queue" is empty or full? |
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. |
You use this programming interface in an incomplete way. Would you like to recheck any recommendations from other information sources? |
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. |
Why do you use such functions when you expect that this program should also work without them? |
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. |
How do you think about to become a bit more aware of the proper handling for the involved predicates? |
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. |
I prefer software correctness over "simplicity" for this use case. |
I think it's correct now because any error from pthread_cond_wait is 2013/9/1 Markus Elfring notifications@github.com
|
No. - I see still a need for further improvements to make the applied algorithms really correct. |
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. |
I find your view questionable.
|
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. |
Would you like to delete unneeded source code? |
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 . |
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. |
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. |
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. |
The thread can sleep or not sleep as it like, the signal is only the suggestion. so it doesn't care the predicate. |
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". |
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. |
Please distinguish the issues "completion of error handling" and "correct use of condition variables". Do you expect the semantics "wake-only-once" here? |
No. As I said before, removing "pthread_cond_wait" is no harmful either . So wake up multi-times is ok. |
Why are you not going to remove the "pthread_cond_..." stuff then? |
I explained many times, so i don't want to say more. Read the code if you want to know the reason. |
I miss still your real explanation so far. |
For which conditions would you like to wait by the discussed variable? |
The "pthread_cond_wait" here only sleeps a while and maybe wake up by "pthread_cond_signal" 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 ? |
Condition variables are not the appropriate means to wait without regarding predicates.
|
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. |
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. |
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"?
The text was updated successfully, but these errors were encountered: