[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: Threads question
On Mon, 4 Dec 2000, Adam Williams wrote:
> >I'd highly recommend using the pthread_cond* functions or
> >another mutex. That usage of a timer to control >syncroinization(sp?)
> between two threads is a bad idea.
>
> Right, I use mutex(s) for synchro between threads. What I need is for a
> thread to wake up every X number of seconds and do it's thing, go back to
> sleep, and do it all over again. Traditionally I'd just sleep(X) but
> that doesn't work in a thread as is knocks out the whole process.
Understood. I don't think using a delay call is a good way of doing it
because you cannot know with absoulte certainity that the delay will be
enough time. The system may be heavily loaded, the read or write could
block, and the delay wouldn't stop a race condition from occuring.
Which is where the cond series of functions come in. I would modify the
read/write relationship where every block of data had a identifer for a
condition object, so the reader or writer would set that, and then block
itself until the other signaled that object and resuming the first thread.
And use that in a see-saw fashiong while processing was occuring.
There is a timedwait version of the function that could suite your
purposes for making a time interval. Like making a condition that is
never signaled. And have both of your threads wait on it using timedwait
and set the timeout for two seconds.
I assume you're using LinuxThreads if you're using Redhat 6.x+.
I believe its fairly close to the final draft of the POSIX standard for
threads, it may be that pthread_delay_np was a draft item that got tossed
out. I haven't been doing a lot of with it recently, so I can't say for
sure.
Jason
> >>I've been
using POSIX threads for quite some time, having taught > >>myself using
> >>"http://centaurus.cs.umass.edu/~wagner/threads_html/tutorial.html".
> >>In this tutorial they use a function called pthread_delay_np.
> >>I just tried to use it, and it appears that my RH box doesn't
> >>have it. I need to make a thread sleep for a given qunatity of
> >>time, but a call to sleep seems to cause all the threads in the
> >>process to sleep. The only reference I can find to >>pthreads_delay_np
> as it relates specifically to
> >>Linux is a VERY old Suse rpm that lists the appropriate man '
> >>page as a provided file. Anyone know the issue with >>pthread_delay_np
> or how to accomplish an equivalent?
>