I am writing a threaded program using the pthread library in linux. I have a master thread that needs to control worker threads, in particular, it has to be able to ensure that none of the worker threads will be running for a specified amount of time, even if the worker threads are active. This means that I cannot use the regular pthread_cond synchronization functions, because the other threads might be executing.
I basically need to replicate the 'suspend' method to control active worker threads. Is there any way to achieve this with pthreads? Or can you suggest other (preferably portable) libraries that give me this functionality?
Thank you very much!
P.S.
Why didn't they just add a suspend method to pthreads? I can see why the a cancel method might be unsafe, but not suspend...
So, you want to start and stop threads at will from the outside?
.
Yes, that's exactly what I want to do. But just a few minutes ago, I found a webite from INRIA's linuxthread package, which pretty much answers my questions - it's not possible. One alternate "solution" the worker thread poll a global variable (a "request") and based on the value, the worker thread suspends itself on a condition variable.
What 'suspend' method do you want to emulate? I know of none.
Solaris threads has a suspend method, and i believe kernel threads have a suspend method too. But this man page confuses me: unix.com/man-page/all/3t/pthread_resume_np/
It lists a pthread_suspend method....
Any idea why?
---------- Post updated at 10:14 PM ---------- Previous update was at 10:07 PM ----------
Since (apparently) it cannot be done with pthreads, i thought i might try to accomplish this with the clone() system call. However, I keep getting segmentation fault. I have no idea why... (I know the printf isn't thread safe, but it's a quick and dirty way to see if the process is active or not. Even if i'm just doing arithmetic operations, I still get a segmentation fault)
What I'm trying to do is to have the main process "activate" the worker for 1 second every 2 seconds. Is this doable?
The proper way to do this would be to reorganize your code so that threads don't waste time idle, rather than killing and unkilling them from the outside all the time.
Thank you all for your suggestions. I should clarify that I'm developing something for a real time embedded system. I need to implement a TDMA server, so I'm going to have N thread executing periodically, with arbitrary execution times. It could happen that a thread finishes its execution before its timeslot, and I need to insure that the CPU will remain idle in that time to save energy.
My first idea was to simply suspend all the threads whose turn it wasn't, and to activate the one correct thread. Which is why I was looking for a way to implement a 'suspend' method.
jim mcnamara, I'm not sure I understand your suggestion. How could I preempt a running task and block it? The running task does not know when it should wait for synchronization.
If the thread doesn't know when it should wait for synchronization, that's a serious design flaw. You're trying to write something more akin to an operating system, capable of imposing order from the outside.
Threads are asynchronous, anyway. Your main thread can't possibly know when a worker thread has completed anything unless the worker thread tells it so. If you try and force synchronization from the outside by guessing, you're going to catch it too soon occasionally -- and if main is given reliable information from the thread, why can't the thread use this information itself?
If these jobs are periodic with long stretches of inactivity, why not create new threads on demand instead of keeping old ones around forever? Let them quit when they're done. Threads that don't exist use no CPU.
Last edited by Corona688; 07-18-2012 at 02:50 PM..
Situation:
i have multiple pthread_create calls like this:
pthread_create(...., ThreadFunc1,.....);
pthread_create(...., ThreadFunc2,.....);
.
.
which i am using to create multiple threads.All the "ThreadFunc<i>" functions are actually calling same function "Receive" of a class using same... (3 Replies)
hi
i have a code:
I found that after exiting from child thread memory isn't freed. I commented everything which is "some actions" here, so thread's function contains only two lines. But it doesn't help.
What do I do wrong?
Thanks a lot (3 Replies)
Hi!
I'm linking my hpux code using -lpthread (gcc), yet it references libpthread_tr.1,
the debug version of the pthread lib. How do I force it to use pthreads?
Thanks, :) (3 Replies)
I have a doubt .. ( maybe I don't know ..)
Let's say , we got the folowing example ( to understand better what I mean ).
We got two POSIX threads ( pthreads ) , one receives some strings and creates a menu ( for that strings , scrollable menu ) , and another one check's to see if it has to... (2 Replies)
How to give superuser privileges while setting the attributes like pthread_attr_setschedpolicy( )??
Even with normal user mode ,it is working fine for me.But in man pages, they have specied that to set the scheduling policy as SCHED_FIFO, the process should have superuser privileges. (0 Replies)
Hi All,
I ve written a small program to get started off with pthreads. I somehow feel the program doesnt meet the purpose. Please find the code and the output below. Please find my question at the bottom.
#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
void *PrintThread1(void... (4 Replies)
I have created a thread program, it is attached. My problem is that I need to loop this program multiple times, and basically reset everything including the threads created previously. I try to loop the program, the first run is fine, as always, but the second run of the program, the initialize... (0 Replies)
howcome that pthtreads spawn 2 extra processes?
I'm kind of new with pthreads but fork() did not act like this.
Anyone who can give me a technical explanation of what happends with mother / daughter processes?
Best regards Esaia. (2 Replies)
Can anyone explain me how to use pthread_key_create() , pthread_setspecific(), pthread_getspecific() and pthread_key_delete () routines in pthreads.
Kindly state by an example. (3 Replies)