Linux multithreading practice (6) Use Posix condition variables to solve producer and consumer problems
In the previous article, we have already discussed how to use semaphores to solve the producer and consumer problems. Under what circumstances should we introduce conditional variables?
Assume that there is a shared resource sum, and the associated mutex is lock_s. assume that the sum operation of each th
Turn https://www.oschina.net/code/snippet_111708_25438This is a classic question, the essence of my solution is to abstract the problem into the producer consumer model, but it is a special producer consumer model, there are two requirements:1, buffer size is 1 (with a Boolean variable to represent it)2. The buffer is initially emptyMore specifically, you can think of it as a printer that can only put one s
The producer consumer model is as follows:The procedure is as follows:1#include 2#include 3 4#include 5#include 6#include 7#include string.h>8 9#include Ten One intG_count =0; A - intNnum, Nloop; - the //Defining Locks -pthread_mutex_t Mutex =Pthread_mutex_initializer; - - //define conditions and initialize +pthread_cond_t my_condition=Pthread_cond_initializer; - + #defineCustom_count 2 A #defineProduct_count 4 at - - //int Pthread_mutex_lock
1, the Python version I used2, the following to write the specific implementation processImport threadingImport timeImport Queue#首先生成一个队列Q =queue.queue ()#生产者def producer (name):L=threading. Rlock ()For I in range (40):L.acquire ()Q.put (i)L.release ()Print "This is thead name was%s, produce num is%s"% (name,i)Time.sleep (2)#消费者DEF consumer (name):Count =0While Count Resulte =q.get ()print ' The thread name is%s and the consume num is%s '% (Name,resul
1 /**2 * Requirements: Assume that there are 10 consumers to consume the production of products, products to ensure that the order by any other consumer consumption, the last consumption of the next to spend3 * 4 * @authorTrfizeng5 * 6 */7 Public classScthread {8 Public Static voidMain (string[] args) {9SYSTEM.OUT.PRINTLN ("Begin:" + (System.currenttimemillis ())/1000);Ten for(inti = 0; I //unable to move equivalent to producer One
Since the previous postI have already worked on literacy, so I should start to talk about some specific programming technical issues. However, before entering the specific technical details, we must first understand the question: how to determine the data unit? Only by clearly analyzing data units can we develop the technical design later.
★What is a data unit?What is data unit pinching? Simply put, each producer put in a buffer zone is a data unit.
Package com. jzm. thread;
Class q {Int N;Boolean valueset = false;Synchronized int get (){// The value has not been put. Wait for the put value.If (! Valueset ){Try {Wait ();} Catch (interruptedexception e ){System. Out. println ("interrupt caught ");}}
System. Out. println ("got:" + n );Valueset = false;Notify ();Return N;}Synchronized void put (int n ){// Put the value, wait for the got to take the value, and then place the valueIf (valueset ){Try {Wait ();} Catch (interruptedexception e ){Sys
This article turned from programming blog: http://program-think.blogspot.com/2009/03/producer-consumer-pattern-1-data.html
★What is a data unit?Simply put, each producer put in a buffer zone is a data unit. Each consumer fetches data from the buffer zone. For sending example, we can regard each letter as a data unit.
However, this introduction is too simple to help everyone analyze this stuff. So let's ta
This example is used to familiarize yourself with the development process of JMS.
The result is that a servlet sends a message to a message driven Bean (MDB. The server is glassfish3.1.
First, create some JMS resources, including connectionfactory and a queue. In this example, it is a ptp jms link.
Establish a connection Factory
Start glassfish-> resources-> JMS resources-> link factory-> Create
The pool name (that is, the JNDI name) is JMS/queueconnectionfactory.
Select javax. JMS. queueconne
1.Before the company has a project, there is a thread to receive a TCP connection and then put the socket into the queue, 10 threads processing socket data, but 10 threads are still not processed, the client connected and closed, the service end processing data through the socket sent, the socket has been closed.For producer-consumer issues, be sure to set the buffer size, and when the buffers are full, the producers will not join the data. The use of
Producer-Consumer modelling issues/** * Using blocking queues to implement producer-consumer models * Blocking queues only allows elements to be accessed in a FIFO * @author Bingyue * */public class Producercustomerpattern {public static V OID Main (string[] args) {//producer and consumer shared storage area blockingqueue Implement
Introduction to the basic API of the multi-thread demo program for producer consumer in linux: intpthread_create (pthread * thread, pthread_attr_t * attr, void * (* start_routine) (* void ), void * arg); the first parameter is the second parameter pointing to the thread... introduction to the basic API of the multi-thread demo program for producer consumer in linux: int pthread_create (pthread * thread, pth
import Java.util.arraylist;import java.util.List;/** * Created by the CCC on 16-4-27.*/ Public classTest { Public Static voidMain (string[] args) {gunclip clip=NewGunclip (); Producer P=NewProducer (clip); Customer C=Newcustomer (clip); P.start (); C.start (); }}/*first I'm going to have a magazine,*/classGunclip {//Mag PrivatelistNULL;//used to put bullets . PrivateBoolean bfull =false;//It is important to understand the Boolean varia
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.