Now I want to create three processes in my program and there are several threads in each process.
And each thread is infinite task, which may sleep and be waked periodically. Besides, the process has some task to do.
My questions are:
1) Do I need to set the threads as detached ? If I set the threads as detached , they seem not to run!!
But, If threads as joinable, the process has to wait the threads to exit and it can’t do its own work!!
which one should I choose?
2)What’s the scope of schedule policy ? I mean, if I set the schedule policy as FIFO, all the threads in the all processes are scheduled by FIFO policy? Or just the thread which is set with this attribute is scheduled by this policy?
3)What’s the scope of thread priority? The thread priorities are just useful in the single process, and in another process, there exist another set of thread priorities ????? And they don’t infect each other???
I would appreciate for your help! thank you!
(1) You have a coding error. A detached thread gets a time slice like everything else. If it is not running then it is something you are doing. You should post your threadfunc and the function which creates the threads in another question.
It’s impossible to say whether your threads should be joinable or detached without knowing what you are doing. The main benefit of joinable threads are you know when they finish and you can check the return data. If these aren’t important to you there is no real advantage to making them joinable – other than it is marginally easier to create them because that is the default.
If you don’t want to block in pthread_join there are strategies you can pursue. Your threads can set switches before they die, you can use condition variables, you can have a separate thread that joins the dead threads and so forth. Again, it is impossible to know what is the best strategy for your particular case.
(2 & 3) A thread inherits the schedule policy and priority of the thread that creates it and they remain that way unless you specifically change them. The policy/priority of threads in one process are not directly related to any other process.