What happens in the background of a blocking function? - multithreading

I wonder what happens in the background when calling a blocking function. Let's take recv() for example.
When that function gets called, is there another process outside my program that uses some mutex-like function to block my thread / process? (And there, the other process sits in an endless loop waiting for an update)
Can someone please give me better insight into how it works. It feels like somewhere there must be a loop that spins endlessly

Related

Concurrently run socket and other computation in C++

I'm sorry if this question is too easy to solve.
I would like to implement the following scenarios in C++.
There exists a collection of functions to be evaluated like f_1, f_2, etc.
While evaluating f_i, the program is sending and receiving something to or from another host.
When f_i finishes, there is some return value.
So the program should immediately move to socket part to send the value or receive something from another machine.
But at the same time, computation of f_j which is not evaluated now should start.
I know multi-threading may solve this problem.
But, how a one thread knows if a computation in some specific thread finishes?
If the socket is replaced by File I/O, I think we can do same thing.
It would be really appreciate if you suggest me a way to solve this or some reference to do that.
You should probably have at least one I/O thread with an event loop to handle your sockets.
This I/O thread can dispatch computations to a thread pool. Once the computation finishes you should let the I/O thread know that it should send the computation result. There a few methods to do that, one simple method is for the compute thread to allocate the computation result on the heap and write a pointer to it into a pipe. The I/O thread event loop notices that there is data in the read end of the pipe available, reads the pointer to the result and starts sending it in non-blocking fashion.

set a deadline for each callback in an event-driven/ event-loop based program

In a typical ASIO or event-based programming library like libevent, is there a way to set a deadline for each callback?
I am worried about possible infinite loops within the callbacks. Is there a way to gracefully detect them, remove the misbehaving callback from task queue and continue processing other tasks in the queue?
I can think of a way to detect it through an external thread and kill the event-loop thread and create a different thread but I am trying to see if there are any other commonly used methods. I believe this is a problem which someone has faced at some point of time and thought through a solution
There is no general way to unstick a thread without its cooperation, whether it's running a callback or not. The thread may hold critical locks or may have acquired resources that would never get released if the thread was somehow coerced to stop from the outside.
If you really do need this functionality, then all code that could potentially be interrupted must be designed to support some specific method of interruption. You can start a deadline timer when you enter the callback and cancel it when you're finished. The deadline timer would have to trigger the thread's interruption mechanism. You'd need at least one other thread running the I/O service in order for some thread to run the timer handler while the callback was running in another thread.
You can also isolate the code in its own process with some kind of wrapper. Then if the code fails to terminate, you can kill the process from the outside.

QThread hungs up when calling isRunning on it

I have a QThread that fetches data from the web. Sometimes the user asks for something else, and the data needs to be fetched changes as well.
In my current configuration, I call terminate() upon the thread, change the input data, and call start() on the thread again. Now, that works fine, but sometimes I get the main eventloop stuck when calling isRunning() or isFinished() upon a terminated thread. It gets stuck forever, and does not recover until I kill the process.
Why would isRunning() or isFinished() hung in the first place? They don't suppose to block.
Is this workflow acceptable? If not, how can I stop a thread's process when I don't need it no more (or how can I abandon it)?
Don't use terminate(), read the warning in the documentation.
Whil it is possible to restart a QThread, restarting threads usually is not a good idea, there should be a better solution to do what you're trying to do.
It seems that in some cases, the thread becomes unusable after termination, and isRunning() and isFinished() may hang the calling thread, even if called only after the TERMINATED signal.
My workaround was to terminate a thread, forget about it and start a new one.

Does TTimer.OnTimer causes a worker thread to synchronize with a main thread?

I have to work with legacy code. This code has a TTimer created in a main thread.
In OnTimer event the timer is checking periodically a state of some data in the worker thread.
pseudocode:
procedure MainForm.OnTimer(Sender: TObject);
begin
if WorkerThread.Data.State = full then
begin
WorkerThread.Free; //This freezes GUI.
end else
//Do something else.
end;
The problem is that I want to do some background operation when the WorkerThread is terminating. To avoid synchronization I've overriden DoTerminate method. However in this particular case, this is not helping and my GUI becomes frozen until the DoTerminate finishes.
Can I somehow avoid the freeze?
Thanks.
There's not enough code here to say anything with any certainty. However, calling Free on a thread results in a call to Terminate followed by a WaitFor. It's quite plausible that the wait is not returning which would be consistent with the frozen UI.
This is truly backwards. In any decent threading scheme, your thread will be notifying your gui-thread about a condition like .Data.State = full. You gui-thread or main-thread will then take appropriate action. One thing i am certain about is that WorkerThread.Free must be wrong. Trying to free a thread that's apparently blocked for whatever reason, is guaranteed to fail. Thread.Terminate will also fail if the thread is blocked, so no help there either.
Having a Timer monitor the status of a thread is never right. I never use the words always and never, but... I'll repeat: Having a Timer monitor the status of a thread is never right. Never ever. Don't even think about it.
turin

What's the best way to signal threads that sleep or block to stop?

I've got a service that I need to shut down and update. I'm having difficulties with this in two different cases:
I have some threads that sleep for large amounts of time. Obviously I can't wait for them to wake up to finish shutting down the service. I had a thought to use an AutoResetEvent that gets set by some controller thread when the sleep interval is up (by just checking every two seconds or something), and triggering it immediately at OnClose time. Is there a better way to facilitate that?
I have one thread that makes a call to a blocking method call (one which I cannot modify). How do you signal such a thread to stop?
I'm not sure if I understood your first question correctly, but have you looked at using WaitForSingleObject as an alternative to Sleep? You can specify a timeout as well as an object to wait on, so if you want it to wake up earlier, just signal the object.
What exactly do you mean by "call to a blocking thread"? Or did you just mean a blocking call? In general, there isn't a way to interrupt a thread without forcefully terminating it. However, if the call is a system call, there might be ways to return control by making the call fail, eg. cancelling I/O or closing an associated handle.
For 1. you can get your threads into an interruptable Sleep by using SleepEx rather than Sleep. Once they get this shutdown kick (initiated from your termination logic using QueueUserApc), you can detect it happened using the return code from SleepEx and terminate those threads accordingly. This is similar to the suggestion to use WaitForSingleObject, but you don't need another per-thread handle that's just used to terminate the associated thread.
The return value is zero if the
specified time interval expired.
The return value is WAIT_IO_COMPLETION
if the function returned due to one or
more I/O completion callback
functions. This can happen only if
bAlertable is TRUE, and if the thread
that called the SleepEx function is
the same thread that called the
extended I/O function.
For 2., that's a tough one unless you have access to some resource used in that thread that can cause the blocking call to abort in such a way that the calling thread can handle it cleanly. You may just have to implement code to kill that thread with extreme prejudice using TerminateThread (probably this should be the last thing you do before exiting the process) and see what happens under test.
An easy and reliable solution is to kill the service process. A process is the memory-safe abstraction of the OS, after all, so you can safely terminate one without regard for process-internal state - of course, if your process is communicating or fiddling with external state, all bets are off...
Additionally, you could implement the solution which OS's themselves commonly do: one warning signal asking the process to clean up as best possible (which sets a flag and gracefully exits what can be gracefully stopped), and then forceful termination if the process doesn't exit by itself (which ends pesky things like blocking I/O).
All services should be built such that forceful termination isn't harmful, since these processes are system managed and may be terminated by things such as a reboot - i.e., your service ideally should permit this without corrupting storage anyhow.
Oh, and one final warning; windows services may share a process (I presume for efficiency, though it strikes me as an avoidable optimization), so if you go this route, you want to make sure your service is not sharing a process with other services. You can ensure this by passing the option SERVICE_WIN32_OWN_PROCESS to ChangeServiceConfig.

Resources