ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
I have created two threads ( thread1 and thread2 ). These two threads use a common buffer.
I want to implement the following logic:
After EVERY 100 sec thread1 starts , if available aquires lock for writing data into the buffer ,writes data into the buffer, release the lock.
After every 500 sec thread2 starts ,if available aquires lock , reads data from the buffer .
In short i want to make my threads execute periodically .
I m NOT USING RTLINUX .....
how do I create timer based threads......where can I set the time attribute for a thread.
Or is it essential to send signals to thread after the required time interval ?
How to set the run state for a thread?
Is it necessary to have RTLINUX as it provides system calls like pthread_make_periodic_np() etc...????
Distribution: Debian /Jessie/Stretch/Sid, Linux Mint DE
Posts: 5,195
Rep:
I assume you don't want to start the threads every 100 or 500 ms by the main program.
Once a thread has been running, you can put it to sleep for a specified number of milliseconds. After that it wakes up, performs its task and goes to sleep again.
If you need a relation with the real time and you don't know how long your thread has been waiting to get access to the shared memory, calculate your time to sleep from substracting the current time from the time your thread should run again.
Yes I need a relation with the real time and want to make my threads execute periodically .
Instead of any calulations is there any attribute of the thread where i can set the time interval....so that the thread will be called periodically after that specified time.
Here I have a feel that i need to use RTLINUX for that ,is it right ?
Can i schedule timmigs for my threads in normal linux OS?
Distribution: Debian /Jessie/Stretch/Sid, Linux Mint DE
Posts: 5,195
Rep:
Depends on your requirements. I once made such a periodic execution as explained in a thread with a 20 ms time slice. Although I was not running RTLinux, the interval was never off more than 2 ms, usually variation was 0.1 ms. There is some variation because other tasks have to run as well and your thread not always the highest priority.
This was on a P166 and my program was executing 2-3 ms of that time slice of 20 ms to give you an idea about processor load.
RTLinux is not nice to use as it is a far way from standard Linux. I like to be able to develop and test on the same machine, with RTL that is more difficult.
AFAIK there is no way to tell a thread to execute periodically. As I said, you can of course build a simple scheduler which puts threads to sleep and wake them up again using semaphores. A thread goes to sleep while waiting for a semaphore.
You could register a timer with setitimer, set a signal handler for SIGALRM that calls pthread_cond_broadcast, then have your thread(s) block at the top of a while loop (etc.) using pthread_cond_wait.
PS Although the timed signal will get the job done, that signal will interrupt blocked system calls and nanosleeps in all threads, so they will need to check errno for EINTR and restart if necessary. That should be a part of threaded programs anyway, though.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.