Tag Archives: thread
C++ || Snippet – Multi-Process Synchronization Producer Consumer Problem Using Threads
The following is sample code which demonstrates the use of POSIX threads (pthreads), aswell as pthread mutex and condition variables for use on Unix based systems.
The use of mutual exclusion (mutex) variables refers to the requirement of ensuring that no two processes or threads are in their critical section at the same time. Here, a critical section refers to the period of time in which a process accesses a shared resource, such as shared memory. This procedure highlights a classic example of a multi-process synchronization problem known as the producer–consumer problem.
The producer–consumer problem describes a scenario in which two processes (the producer and the consumer) share a common resource (i.e: a string buffer). In this scenario, the producer’s job is to generate a piece of data, update that data with the shared resource (the buffer), and repeat. At the same time, the consumer is consuming that shared resource (i.e: removing something from that same buffer) one piece at a time. The problem is to make sure that the producer wont try to manipulate that shared resource (i.e: add data into the buffer) while the consumer is accessing it; and that the consumer wont try to remove data from that shared resource (the buffer) while the producer is updating it.
In this instance, a solution for the producer can be to go to sleep if a synchronization conflict occurs, and the next time the consumer removes an item from the buffer, it can notify the producer to wake up and start to fill the buffer again. In the same way, the consumer can go to sleep when the producer is accessing it. The next time the producer puts data into the buffer, it wakes up the sleeping consumer and the process continues.
The example on this page demonstrates the use of the above solution using the “pthread_mutex_lock()” function call to limit access to the critical section, and the “pthread_cond_wait()” function call to simulate the sleeping process.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 |
// ============================================================================ // Author: Kenneth Perkins // Date: Oct 3, 2013 // Taken From: http://programmingnotes.org/ // File: Condvar.cpp // Description: Demonstrate the use of the producer consumer problem // using mutex and condition variables // ============================================================================ #include <iostream> #include <cstdlib> #include <pthread.h> using namespace std; // Compile & Run // g++ Condvar.cpp -lpthread -o Condvar // ./Condvar // function prototypes void* Produce(void* arg); void* Consume(void* arg); // global variables // the mutex and the condition variable pthread_mutex_t mutex; pthread_cond_t cond; // the condition flag bool condition = false; // the item produced int count = 0; // how much to produce const int NUM_TO_PRODUCE = 6; int main() { // declare variables pthread_t producerThread; pthread_t consumerThread; // initialize the mutex and the condition variable pthread_mutex_init(&mutex, NULL); pthread_cond_init(&cond, NULL); cout <<"\nThe Parent is creating the producer and consumer threads..\n" << endl; // create a producer thread if(pthread_create(&producerThread, NULL, Produce, NULL) < 0) { perror("pthread_create"); exit(1); } // create a consumer thread if(pthread_create(&consumerThread, NULL, Consume, NULL) < 0) { perror("pthread_create"); exit(1); } // wait for the threads to complete // similar to the wait() system call for fork() processes if(pthread_join(producerThread, NULL) < 0) { perror("pthread_join"); exit(1); } if(pthread_join(consumerThread, NULL) < 0) { perror("pthread_join"); exit(1); } cout <<"\nBoth threads have completed and have terminated!\n" <<"\nThe Parent is now exiting...\n"; return 0; }// end of main /** * The producer thread function * @param arg - pointer to the thread local data - unused */ void* Produce(void* arg) { // produce things until the loop condition is met while(count < NUM_TO_PRODUCE) { // lock the mutex to protect the condition variable if(pthread_mutex_lock(&mutex) < 0) { perror("pthread_mutex_lock"); exit(1); } // we have produced something that has not been // consumed yet, so we sleep (wait) until the consumer // wakes us up. while(condition) { // sleep (wait) on a condition variable until the // the consumer wakes the producer up. if(pthread_cond_wait(&cond, &mutex) < 0) { perror("pthread_cond_wait"); exit(1); } } // produce an item cout <<"Produced: "<<++count<<endl; // we have produced something condition = true; // wake up the sleeping consumer if(pthread_cond_signal(&cond) < 0) { perror("pthread_cond_signal"); exit(1); } // release the mutex lock if(pthread_mutex_unlock(&mutex) < 0) { perror("pthread_mutex_unlock"); exit(1); } } return 0; }// end of Produce /** * The consumer function * @param arg - pointer to the thread local data - unused */ void* Consume(void* arg) { // consume things until the loop condition is met while(count < NUM_TO_PRODUCE) { // lock the mutex to protect the condition variable if(pthread_mutex_lock(&mutex) < 0) { perror("pthread_mutex_lock"); exit(1); } // if there is nothing to consume, then sleep while(!condition) { // sleep (wait) on a condition variable until the // producer wakes the consumer up. if(pthread_cond_wait(&cond, &mutex) < 0) { perror("pthread_cond_wait"); exit(1); } } cout <<"Consumed: "<<count<<endl<<endl; // consume the item condition = false; // wake up the sleeping producer if(pthread_cond_signal(&cond) < 0) { perror("pthread_cond_signal"); exit(1); } // unlock the mutex if(pthread_mutex_unlock(&mutex) < 0) { perror("pthread_mutex_unlock"); exit(1); } } return 0; }// http://programmingnotes.org/ |
QUICK NOTES:
The highlighted lines are sections of interest to look out for.
To use pthreads, the compiler flag “-lpthread” must be set.
The code is heavily commented, so no further insight is necessary. If you have any questions, feel free to leave a comment below.
The following is sample output:
The Parent is creating the producer and consumer threads..
Produced: 1
Consumed: 1Produced: 2
Consumed: 2Produced: 3
Consumed: 3Produced: 4
Consumed: 4Produced: 5
Consumed: 5Produced: 6
Consumed: 6Both threads have completed and have terminated!
The Parent is now exiting...
C++ || Snippet – How To Create And Use Threads For Interprocess Communication
The following is sample code which demonstrates the use of POSIX threads (pthreads), aswell as the pthread_create, and pthread_join function calls on Unix based systems.
Much like the fork() function call which is used to create new processes, threads are similar in that they too are used for interprocess communication. Threads allow for multi-threading, which is a widespread programming and execution model that allows for multiple threads to exist within the same context of a single program.
Threads share the calling parent process’ resources. Each process has it’s own address space, but the threads within the same process share that address space. Threads also share any other resources within that process. This means that it’s very easy to share data amongst threads, but it’s also easy for the threads to step on each other, which can lead to bad things.
The example on this page demonstrates the use of multiple pthreads to display shared data (an integer variable) to the screen.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 |
// ============================================================================ // Author: Kenneth Perkins // Date: Oct 3, 2013 // Taken From: http://programmingnotes.org/ // File: Pthread.cpp // Description: Demonstrate the use of using multiple threads using the // pthread_create() function // ============================================================================ #include <iostream> #include <cstdlib> #include <pthread.h> using namespace std; // Compile & Run // g++ Pthread.cpp -lpthread -o Pthread // ./Pthread <NUMBER OF THREADS TO CREATE> // function prototype void* ThreadFunc(void* args); // global variable int threadNum = 0; int main(int argc, char* argv[]) { // declare variables int numberOfThreads = 0; pthread_t* threads; // make sure theres enough commandline arguments // and EXIT if theres not if(argc < 2) { cout <<"** ERROR - NOT ENOUGH ARGS!\n" <<"\nUSAGE:"<<argv[0]<<" <NUMBER OF THREADS TO CREATE>\n"; exit(1); } // convert the number of threads from the command line // from string to int value numberOfThreads = atoi(argv[1]); // declare the array of thread variables threads = new pthread_t[numberOfThreads]; cout <<"\nThe Parent is creating "<<numberOfThreads<<" threads!" << endl; // use a loop to create the threads for(int x = 0; x < numberOfThreads; ++x) { // create the thread and call the "ThreadFunc" if(pthread_create(&threads[x], NULL, ThreadFunc, NULL) < 0) { perror("pthread_create error"); exit(1); } } cout <<"The Parent is now waiting for the thread(s) to complete...\n\n"; // wait for all threads to finish for(int x = 0; x < numberOfThreads; ++x) { pthread_join(threads[x], NULL); } cout <<"\nAll thread(s) are complete and have terminated!\n" <<"\nThe Parent is now exiting...\n"; return 0; }// end of main /** * The function called by the thread * @param args - pointer to the thread local data */ void* ThreadFunc(void* args) { // display the thread info cout <<"Hi, Im thread #"<<++threadNum <<" and this is my id number: "<<(unsigned)pthread_self()<<endl; return 0; }// http://programmingnotes.org/ |
QUICK NOTES:
The highlighted lines are sections of interest to look out for.
So, what is the difference between fork() processes and thread processes? Threads differ from traditional multitasking operating system processes in that:
• processes are typically independent, while threads exist as subsets of a process
• processes carry considerable state information, whereas multiple threads within a process share state as well as memory and other resources
• processes have separate address spaces, whereas threads share their address space
• processes interact only through system-provided inter-process communication mechanisms.
• Context switching between threads in the same process is typically faster than context switching between processes.
To use pthreads, the compiler flag “-lpthread” must be set.
The code is heavily commented, so no further insight is necessary. If you have any questions, feel free to leave a comment below.
The following is sample output:
./Pthread 5
The Parent is creating 5 threads!
The Parent is now waiting for the thread(s) to complete...Hi, Im thread #1 and this is my id number: 1664468736
Hi, Im thread #2 and this is my id number: 1647683328
Hi, Im thread #3 and this is my id number: 1656076032
Hi, Im thread #4 and this is my id number: 1672861440
Hi, Im thread #5 and this is my id number: 1681254144All thread(s) are complete and have terminated!
The Parent is now exiting...