作业帮 > 英语 > 作业

英语翻译The pthread cond timedwait function behaves like the pth

来源:学生作业帮 编辑:灵鹊做题网作业帮 分类:英语作业 时间:2024/04/29 08:50:38
英语翻译
The pthread cond timedwait function behaves like the pthread cond wait function,except that it returns with an error code of ETIMEDOUT once the value of the system clock exceeds the absolute time value in abstime.Figure 11.23 shows a handy routine that a thread can use to build the abstime argument each time it calls pthread cond timedwait:
For a simple example of timeout waiting in a threaded program,suppose we want to write a beeping timebomb that waits at most 5 seconds for the user to hit the return key,printing out “BEEP” every second.If the user doesn’t hit the return key in time,then the program explodes by printing “BOOM!”.Otherwise,it prints “Whew!” and exits.Figure 11.24 shows a threaded timebomb that is based on the pthread cond timedwait function.
The main timebomb thread locks the mutex and then creates a peer thread that calls getchar,which blocks the thread until the user hits the return key.When getchar returns,the peer thread signals the main thread that the user has hit the return key,and then terminates.Notice that since the main thread locked the mutex before creating the peer thread,the peer thread cannot acquire the mutex and signal the main thread until the main thread releases the mutex by calling pthread cond timedwait.
Meanwhile,after the main thread creates the peer thread,it waits up to one second for the peer thread to terminate.If pthread cond timedwait does not time out,then the main thread knows that the peer thread has terminated,so it prints “Whew!” and exits.Otherwise,it beeps and waits for another second.This continues until it has waited a total of 5 seconds,at which point the loop terminates,the main thread explodes by printing “Boom!”,and then exits.
英语翻译The pthread cond timedwait function behaves like the pth
pthread cond timedwait 功能像 pthread 举止 cond 等候功能,除一经系统时钟的价值在 abstime 中超过绝对的时间价值它以一个 ETIMEDOUT 的错误密码返回.图 11.23 表演一个一条线能使用每次建立 abstime 争论的便利的常式它认为 pthread 是 cond timedwait:
对于一个被穿线于的计画一个暂时休息等候的简单例子,推想我们想要写最多为使用者等候 5 秒击中回返钥匙的哔哔声 timebomb,印刷出 " 哔哔声 " 每秒.如果使用者不及时击中回返钥匙,当时计画藉由印刷爆炸”急速发展!”.另外,它印刷”惊愕狼狈时所发出的声音!”而且出口.图 11.24 表演以 pthread 为基础 cond 的被穿线于的 timebomb timedwait 动作.
主要的 timebomb 线锁 mutex 然后产生一条呼叫直到使用者击中回返钥匙,妨碍线的 getchar 的同侪线.当 getchar 返回,同侪线向主要的线作信号使用者打回返钥匙,然后结束.注意,自从主要的线在创造同侪线之前锁了 mutex,同侪线直到主要的线藉由认为 pthread 是 cond 释放 mutex timedwait 才会获得 mutex 而且向主要的线作信号.
同时,在主要的线产生同侪线之后,它等候同侪线达一秒结束.如果 pthread cond timedwait 不计时出,当时主要的线知道同侪线已经结束,因此它印刷”惊愕狼狈时所发出的声音!”而且出口.另外,它哔哔声而且等候另外的秒.这继续,直到它等候总共 5 秒,在指出环结束,主要的线藉由印刷爆炸”急速发展!”, 然后出口.