Find the answer to your Linux question:
Results 1 to 2 of 2
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 ...
  1. #1
    Just Joined!
    Join Date
    Mar 2008
    Posts
    13

    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...????

  2. #2
    Linux Engineer wje_lf's Avatar
    Join Date
    Sep 2007
    Location
    Mariposa
    Posts
    1,192
    You're using POSIX threads, so you don't need RTLINUX. Any Linux will do.

    But I would highly discourage you from using POSIX threads unless absolutely necessary. The reason is that all threads share the same heap address space, and if there is a bug in one thread that stomps on memory it shouldn't, the bug might show itself by making a different thread misbehave. Very difficult to debug. Use fork() to produce a child process. Either the parent can be the writer and the child can be the reader, or the other way around. Your choice.

    Use mmap() and /dev/zero to create shared memory. Do this in the parent process before fork()ing.

    Use file locking to synchronize between the threads. That will involve calling fcntl() with the commands F_RDLCK, F_WRLCK, and F_UNLCK.

    For doing things periodically, use sleep().

    Hope this helps.
    --
    Bill

    Old age and treachery will overcome youth and skill.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
...