线程队列系统 [英] Pthread Queue System

查看:52
本文介绍了线程队列系统的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在处理有关 pthread 的作业.我是新手,以前从未接触过 pthread.你们中是否有任何示例代码或资源可以帮助我完成我的任务?

I'm working on my assignment on pthreads. I'm new and never touched on pthreads before. Is there any sample codes or resources out there that anyone of you have, that might aid me in my assignment?

这是我的任务详情.一个关于队列系统的pthread程序:

Here are my assignment details. A pthread program about queue system:

为牙科诊所的排队系统编写一个 C/C++ Pthread 程序,该程序声明一个大小为 N 的整数数组,其中 N 是当天的最大队列数.这pthread 程序使用两个线程.

Write a C/C++ Pthread program for a Dental clinic’s queuing system that declares an array of integers of size N, where N is the maximum number of queue for the day. The pthread program uses two threads.

每当有新的牙科预约时,第一个线程(创建者)将队列编号一个接一个地放入数组中.这当第二个线程(移除器)从数组中移除队列号时牙医看过病人.这是以 FIFO 方式(先进先出)完成的.创建者的算法如下:

Whenever there is a new dental appointment, the first thread (the creator) puts the queue numbers in the array, one after the other. The second thread (the remover) removes the queue numbers from the array whenever the dentist has seen the patient. This is done in a FIFO fashion (First In First Out). The algorithm of the creator is as follows:

• 如果数组未满,则在其中放入一个新数字(数字从 1并且每次都加一,因此创建者创建队列号 1、2、3 等)

• If the array is not full then put a new number in it (the numbers start at 1 and are incremented by one each time, so the creator create queue number 1, 2, 3 etc.)

• 随机休眠 1 到 10 秒

• sleep for 1 to 10 seconds, randomly

• 重复

remover的算法如下:

The algorithm of the remover is as follows:

• 如果数组不为空,则删除其最小的队列号.

• If the array is not empty then remove its smallest queue number.

• 随机休眠 1 到 10 秒

• sleep for 1 to 10 seconds, randomly

• 重复

你应该使用互斥锁来保护必须保护的东西.每个线程应该在屏幕上打印它正在做什么(例如:数字 13 已添加到队列中",从队列中删除了数字 7"等).该程序应该永远运行.

You should use mutex locks to protect things that must be protected. Each thread should print on the screen what it is doing (eg: "number 13 is added into the queue", "number 7 is removed from the queue", etc.). The program should run forever.

任何帮助将不胜感激.谢谢.

Any help will be appreciated. Thanks.

推荐答案

对于一般从 pthreads 开始,this 是一个不错的网站,其中的信息可能比您需要的要多(但我喜欢细节).它贯穿了 pthread 等的许多基础知识.如果您更喜欢死树教程,这本书非常好,为您提供了 Linux API 或核心库的大多数功能的良好基础,如果你想这样称呼它.这个stackoverflow 问题非常简洁地处理互斥体与信号量.

For generally starting out with pthreads, this is a good website with possibly more info than you need (but I like detail). It runs through a lot of the basics for pthreads and more. If you prefer a dead-tree tutorial, this book is pretty good and gives you a good grounding in most of the features of the Linux API, or the core libraries, if you want to call it that. This stackoverflow question deals with mutexes vs semaphores pretty concisely.

最后,我喜欢这个站点,因为它融合了 Linux 线程和同步.

Finally, I like this site for its converage of Linux Threading and Synchronisation.

希望这些能给你一些阅读材料.弄清楚如何处理线程,然后如何同步它们,然后解决您的问题.

Hopefully these give you some reading material. Work out how you handle threads, then how you synchronise them, then attack your problem.

这篇关于线程队列系统的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆