How to find whether a pthread has pending cancellation request - c

I want to find whether for a thread, pthread_cancel has been called or not.
I don't want to use some tables and to maintain that. Is there any library function available for this? I don't want to cancel the thread using some cancellation point functions which cancel the thread if there is any pending cancellation request, I just want to know whether there is any pending cancellation request or not.

Even if you call pthread_cancel, it is not executed immediately. Processor takes its own time to cancel the thread. But, just to mentioned here: usage of asynchronous cancellation or immediate cancellation is not recommended. I faced an issue where my application was crashing during cancellation when using the pthread_setcanceltype() as PTHREAD_CANCEL_ASYNCHRONOUS. I really could not find a reason why.
As far as I know and even I have searched a lot for the same while debugging the crash scenario, we dont have a method to confirm at what point is the thread cancelled.

Related

thread safety in GNUTLS

The version of GNUTLS is 3.5. I want to use a child thread to handshake with remote peer.
In my child thread, I just use gnutls_handshake().
In the parent thread, can I use pthread_cancel() to cancel the child thread safety regardless of the current handshake state?
If I have registered the pull/pull_timeout/push functions with pthread_cleanup_push/pthread_cleanup_pop,
can I cancel the child thread?
i have emailed Nikos Mavrogiannopoulos(current maintainers of gnutls) ,his answer as follow:
gnutls functions were never designed as pthread cancellation points. I
have not thought that much, but I believe your main concern is memory
leaks right? It may be that if you deallocate the session in another
thread it may just work; though you may have to create a stress test
for that to verify that this is possible.

How do you prevent a user program from leaving kernel resources locked?

Let's consider a case where a user program calls a system call that has some synchronization measures. The simplest example would be
rwlock_t lock; // let's assume it's initialized properly.
write_lock(&lock);
// do something...
write_unlock(&lock);
Now, what happens when the user program terminates after locking lock but before releasing it is that the lock becomes perpetually locked, which we do not want it to happen. What we want the kernel to do is smartly detect any hanging locks and release them accordingly. But detecting such tasks can incur too much overhead, as the system needs to periodically record and check every task for every synchronizing action.
Or perhaps we can centralize the code into another kernel thread and do synchronization job there. But invoking on another thread still requires some form of synchronization, so I don't think it is possible to completely remove synchronizing code from the user program.
I have put a lot of thought into this and tried to google for some information but I couldn't see any light on this. Any help would be very much appreciated. Thank you.

Creating a pthreads thread pool to handle get requests

I find it hard to believe there isn't an answer or tutorial for this, but am struggling to find one anywhere!
I have to (and have) build a multithreaded server to handle GET requests in C.
For full marks this needs to use a thread pool. Currently my main thread accepts connections and passes them on to a new thread.
I can find a few implementations of thread pools in c online, but coming from a Java background understanding them is proving difficult. They also all seem to use a task queue.
This seems unnecessary considering you can tell the listen call to queue connections.
I saw somewhere that accept is thread safe (saying that I also hear when POSIX says safe its more of a safeish?)
Is this a sensible approach to take? Or will the overhead be higher with each thread waiting on accept instead of stopping exection until passed a connection?
If that is the case how in C would I go about doing this? I presume i would need to keep a thread safe data structure storing pointers to each thread and a value indicating if they are busy or not?
And have some method to restart the thread and pass it a connection? But I have no idea how to do this and can't find any simple tutorials on the internet.
Any advice or links to tutorials would be much appreciated!
Thanks
Accept() is thread-safe.
Actually what you describe is an elegant way to implement a socket server using a thread pool - call accept() in all of them, and the operating system will take care of waking only one thread when a connection arrives. Good job, I have never really thought about this option when I had to implement such things.
As far as I see there's no real overhead in calling accept() in multiple threads at the same time - all threads will sleep until a connection can be accepted, so they won't effectively consume any CPU time.

Is it possible to make synchronous network call on ui thread in wpf (windows phone)

Is it possible to make a synchronous network call on UI thread in WPF (Windows Phone 8).
(I know it's cons, but still i need this functionality to make it work with some ported code)
Tried using autoresetevent method. Due to deadlock, it is blocking ui thread and app hangs forever.
Even tried with webclient. still the UI thread is getting blocked and app hangs forever.
Any help...
This is a very bad idea - I've found that doing this by accident will actually lead to a complete deadlock of your UI. You need to use an asynchronous method.
Callbacks from asynchronous network operations are queued on the UI thread. If you block the thread to wait for it, the callback will never arrive because it is waiting for access to the same thread.
You do have another option though... If you have ported the code, then you will have to change it to support async operations.

How to trigger spurious wake-up within a Linux application?

Some background:
I have an application that relies on third party hardware and a closed source driver. The driver currently has a bug in it that causes the device to stop responding after a random period of time. This is caused by an apparent deadlock within the driver and interrupts proper functioning of my application, which is in an always-on 24/7 highly visible environment.
What I have found is that attaching GDB to the process, and immediately detaching GDB from the process results in the device resuming functionality. This was my first indication that there was a thread locking issue within the driver itself. There is some kind of race condition that leads to a deadlock. Attaching GDB was obviously causing some reshuffling of threads and probably pushing them out of their wait state, causing them to re-evaluate their conditions and thus breaking the deadlock.
The question:
My question is simply this: is there a clean wait for an application to trigger all threads within the program to interrupt their wait state? One thing that definitely works (at least on my implementation) is to send a SIGSTOP followed immediately by a SIGCONT from another process (i.e. from bash):
kill -19 `cat /var/run/mypidfile` ; kill -18 `cat /var/run/mypidfile`
This triggers a spurious wake-up within the process and everything comes back to life.
I'm hoping there is an intelligent method to trigger a spurious wake-up of all threads within my process. Think pthread_cond_broadcast(...) but without having access to the actual condition variable being waited on.
Is this possible, or is relying on a program like kill my only approach?
The way you're doing it right now is probably the most correct and simplest. There is no "wake all waiting futexes in a given process" operation in the kernel, which is what you would need to achieve this more directly.
Note that if the failure-to-wake "deadlock" is in pthread_cond_wait but interrupting it with a signal breaks out of the deadlock, the bug cannot be in the application; it must actually be in the implementation of pthread condition variables. glibc has known unfixed bugs in its condition variable implementation; see http://sourceware.org/bugzilla/show_bug.cgi?id=13165 and related bug reports. However, you might have found a new one, since I don't think the existing known ones can be fixed by breaking out of the futex wait with a signal. If you can report this bug to the glibc bug tracker, it would be very helpful.

Resources