LinuxQuestions.org
Review your favorite Linux distribution.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 04-17-2008, 06:10 AM   #1
mugdha
LQ Newbie
 
Registered: Apr 2008
Posts: 9

Rep: Reputation: 0
how to create Timer based threads


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...????
 
Old 04-17-2008, 07:04 AM   #2
jlinkels
LQ Guru
 
Registered: Oct 2003
Location: Bonaire, Leeuwarden
Distribution: Debian /Jessie/Stretch/Sid, Linux Mint DE
Posts: 5,195

Rep: Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043
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.

jlinkels
 
Old 04-17-2008, 07:13 AM   #3
mugdha
LQ Newbie
 
Registered: Apr 2008
Posts: 9

Original Poster
Rep: Reputation: 0
Hi jlinkels,

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?
 
Old 04-17-2008, 08:02 AM   #4
jlinkels
LQ Guru
 
Registered: Oct 2003
Location: Bonaire, Leeuwarden
Distribution: Debian /Jessie/Stretch/Sid, Linux Mint DE
Posts: 5,195

Rep: Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043
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.

jlinkels
 
Old 04-17-2008, 06:20 PM   #5
ta0kira
Senior Member
 
Registered: Sep 2004
Distribution: FreeBSD 9.1, Kubuntu 12.10
Posts: 3,078

Rep: Reputation: Disabled
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.
Code:
#include <stdio.h>
#include <pthread.h>
#include <signal.h>
#include <sys/time.h>
#include <time.h>


static int set_up = 0;
static pthread_mutex_t mutex     = PTHREAD_MUTEX_INITIALIZER;
static pthread_cond_t  condition = PTHREAD_COND_INITIALIZER;


static void *thread(void *aArgument)
{
	pthread_mutex_init(&mutex, NULL);
	pthread_cond_init(&condition, NULL);
	set_up = 1;

	int total_cycles = 10;

	while (total_cycles--)
	{
	pthread_mutex_lock(&mutex);
	pthread_cond_wait(&condition, &mutex);
	pthread_mutex_unlock(&mutex);

	printf("execute %i\n", total_cycles);
	}

	set_up = 0;
	pthread_mutex_destroy(&mutex);
	pthread_cond_destroy(&condition);

	return NULL;
}


void alarm_handler(int sSignal)
{ if (set_up) pthread_cond_broadcast(&condition); }


int main()
{
	pthread_t timed_thread = (pthread_t) NULL;

	pthread_create(&timed_thread, NULL, &thread, NULL);

	signal(SIGALRM, &alarm_handler);

	struct itimerval timer_setup = { .it_interval = { 1, 500 * 1000 },
	                                    .it_value = { 2, 0 } };

	setitimer(ITIMER_REAL, &timer_setup, NULL);

	pthread_join(timed_thread, NULL);

	return 0;
}
ta0kira

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.

Last edited by ta0kira; 04-17-2008 at 06:27 PM.
 
Old 04-18-2008, 08:16 AM   #6
mugdha
LQ Newbie
 
Registered: Apr 2008
Posts: 9

Original Poster
Rep: Reputation: 0
Thanks it helped me a lot
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Internet Based "Tea Timer" Reminder System for Ubuntu Jaffa8 Linux - Newbie 2 02-08-2008 11:03 AM
Using system calls to create/context switch threads in assembly code casmac Programming 1 07-13-2007 04:48 PM
How to create more than 2000 threads inlinux 2.4kernel Agnello Programming 5 07-11-2006 09:12 AM
Creating a milisecond timeout timer for threads cardias Programming 5 08-05-2004 07:18 AM
Problems creating a milisecond timeout timer for threads cardias Linux - Software 1 08-04-2004 03:10 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 12:15 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration