My task is to make a game simulation that uses threads to simulate work. If the thread's been given command to do another work it should pause it's first work and start the new work. How to implement it in C? I've tried with separate function for the input and signaling a condition variable but when it enters the giveCommand() function it just waits for input.
#include <stdio.h>
#include <pthread.h>
#include <stdlib.h>
char command;
pthread_mutex_t mutex;
pthread_cond_t condVar;
void giveCommand()
{
scanf("%c", command);
pthread_cond_signal (&condVar);
return;
}
void *thread(void *N)
{
char c;
do
{
pthread_mutex_lock(&mutex);
//do its work
pthread_cond_timedwait(&condVar, &mutex, 500000);// if command hasn't been given, continue work
pthread_mutex_unlock(&mutex);
pthread_cond_init(&condVar, NULL);
while(giveCommand());
}
}
int main()
{
int i;
int error;
pthread_t threads[NUM_THREADS];
pthread_mutex_init(&mutex, NULL);
pthread_cond_init(&condVar, NULL);
while(1)
{
for (i = 0; i < NUM_THREADS; i++)
{
error = pthread_create(&threads[i], NULL, simulateWorker, NULL);
if (error != 0) printf("Error creating thread!");
}
for (i = 0; i < NUM_THREADS; i++)
{
error = pthread_join(threads[i], NULL);
if (error != 0) printf("Error joining thread!");
}
pthread_mutexattr_destroy(&mutex);
}
return 0;
}
Related
I tried to make a parallel program that generates a random number with one thread and the other thread writes it.
Am I doing something wrong that messes with the performance/optimization? I ask it because it was very easy to write this program so I'm a little concerned that I am doing something wrong.
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <pthread.h>
#include <semaphore.h>
#include <time.h>
#include "produceConsume.h"
#define NUM_THREAD 1
pthread_mutex_t lock = PTHREAD_MUTEX_INITIALIZER;
pthread_cond_t cond = PTHREAD_COND_INITIALIZER;
int queue[1];
int queueCounter = 0;
void *producer(void *args)
{
while (1)
{
pthread_mutex_lock(&lock);
int n = rand() % 100;
queue[queueCounter] = n;
queueCounter++;
pthread_cond_wait(&cond, &lock);
pthread_mutex_unlock(&lock);
}
}
void *consumer(void *args)
{
while (1)
{
pthread_mutex_lock(&lock);
printf("%d\n", queue[queueCounter - 1]);
queueCounter--;
pthread_cond_signal(&cond);
pthread_mutex_unlock(&lock);
sleep(1);
}
}
int main()
{
system("clear");
srand(time(NULL));
pthread_t th[NUM_THREAD], th2[NUM_THREAD];
for (int i = 0; i < NUM_THREAD; i++)
{
pthread_create(&th[i], NULL, &producer, NULL);
pthread_create(&th2[i], NULL, &consumer, NULL);
}
for (int i = 0; i < NUM_THREAD; i++)
{
pthread_join(th[i], NULL);
pthread_join(th2[i], NULL);
}
}
You don't need an array if you are going to use only one thread, in any case, you create two threads but only one is joined (leaking memory), instead:
pthread_t th1[NUM_THREAD]; // or simply pthread_t th1;
pthread_t th2[NUM_THREAD]; // or simply pthread_t th2;
for (int i = 0; i < NUM_THREAD; i++)
{
pthread_create(&th1[i], NULL, &producer, NULL);
pthread_create(&th2[i], NULL, &consumer, NULL);
}
for (int i = 0; i < NUM_THREAD; i++)
{
pthread_join(th1[i], NULL);
pthread_join(th2[i], NULL);
}
I am writing various code snippets and see what happens. The code below was intended to delay all threads until all reached a certain point in the code and then make each print a distinctive number. Since the threads all do that, the numbers should occur in a random order.
My current problem is that I keep they threads busy waiting. If the number of threads gets big, the program slows down significantly.
I would like to change that by using signals, I found pthread_cond_wait() for that, however I don't see how one would use that to signal all threads that they would please wake up.
#include <pthread.h>
#include <stdio.h>
#include <unistd.h>
#define threads 10
int counter=0;
pthread_mutex_t lock;
void handler(void *v) {
pthread_mutex_lock(&lock);
counter++;
printf("%d\n", counter);
pthread_mutex_unlock(&lock);
while(counter != threads); // busy waiting
printf("I am first! %d\n", v);
}
int main() {
pthread_t t[threads];
for(int i =0; i < threads; i++) {
pthread_create(&t[i], NULL, handler, (void*) i);
}
for(int i =0; i < threads; i++) {
pthread_join(t[i], NULL);
}
return 0;
}
EDIT: I changed the code to the following, however, it still does not work :/
pthread_mutex_t lock;
pthread_cond_t cv;
void handler(void *v) {
pthread_mutex_lock(&lock);
pthread_cond_wait(&cv, &lock);
printf("I am first! %d\n", v);
pthread_mutex_unlock(&lock);
}
int main() {
pthread_t t[threads];
for(int i =0; i < threads; i++)
pthread_create(&t[i], NULL, handler, (void*) i);
sleep(2);
pthread_cond_signal(&cv);
for(int i =0; i < threads; i++)
pthread_join(t[i], NULL);
return 0;
}
use broadcast()?
http://pubs.opengroup.org/onlinepubs/009696699/functions/pthread_cond_broadcast.html
The pthread_cond_broadcast() function shall unblock all threads currently blocked on the specified condition variable cond.
The pthread_cond_signal() function shall unblock at least one of the threads that are blocked on the specified condition variable cond (if any threads are blocked on cond).
An alternative solution to pthread_cond_broadcast() might be the following.
You define a read-write mutex and lock it in write in the main thread before creating the other threads. The other threads will try to acquire a read-lock but since the main thread have a write-lock they will be blocked.
After the creation of all thread the main-thread releases the lock. All the other threads will be waken up and since many read-locks can coexist the will execute simultaneously (i.e. no one will be locked).
Code might be something like:
pthread_rwlock_t lock;
void handler(void *v) {
if ((res = pthread_rwlock_rdlock(&lock)!=0)
{
exit(1);
}
printf("I am first! %d\n", v);
pthread_rwlock_unlock(&lock);
}
int main() {
pthread_t t[threads];
//First acquire the write lock:
if ((res = pthread_rwlock_wrlock(&lock)!=0)
{
exit(1);
}
for(int i =0; i < threads; i++)
{
pthread_create(&t[i], NULL, handler, (void*) i);
//it is not clear if you want sleep inside the loop or not
// You indented it as if to be inside but not put brackets
sleep(2);
}
pthread_rwlock_unlock(&lock);
for(int i =0; i < threads; i++)
pthread_join(t[i], NULL);
pthread_rwlock_destroy(&lock);
return 0;
}
Try posting the matching remove_from_buffer code.
Better yet, Short, Self Contained, Correct Example
Make a short main() with two threads.
One thread adds to the buffer at random intervals.
The other thread removes from the buffer at random intervals.
Example
CELEBP22
/* CELEBP22 */
#define _OPEN_THREADS
#include <pthread.h>
#include <stdio.h>
#include <time.h>
#include <unistd.h>
pthread_cond_t cond;
pthread_mutex_t mutex;
int footprint = 0;
void *thread(void *arg) {
time_t T;
if (pthread_mutex_lock(&mutex) != 0) {
perror("pthread_mutex_lock() error");
exit(6);
}
time(&T);
printf("starting wait at %s", ctime(&T));
footprint++;
if (pthread_cond_wait(&cond, &mutex) != 0) {
perror("pthread_cond_timedwait() error");
exit(7);
}
time(&T);
printf("wait over at %s", ctime(&T));
}
main() {
pthread_t thid;
time_t T;
struct timespec t;
if (pthread_mutex_init(&mutex, NULL) != 0) {
perror("pthread_mutex_init() error");
exit(1);
}
if (pthread_cond_init(&cond, NULL) != 0) {
perror("pthread_cond_init() error");
exit(2);
}
if (pthread_create(&thid, NULL, thread, NULL) != 0) {
perror("pthread_create() error");
exit(3);
}
while (footprint == 0)
sleep(1);
puts("IPT is about ready to release the thread");
sleep(2);
if (pthread_cond_signal(&cond) != 0) {
perror("pthread_cond_signal() error");
exit(4);
}
if (pthread_join(thid, NULL) != 0) {
perror("pthread_join() error");
exit(5);
}
}
OUTPUT
starting wait at Fri Jun 16 10:54:06 2006 IPT is about ready to
release the thread wait over at Fri Jun 16 10:54:09 2006
I have a program which consists of multiple threads.
These threads have to synchronize at some point, continue together, do their work of different lengths and then synchronize again.
Here's the example of what I mean:
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <pthread.h>
#include <unistd.h>
#define NUM_THREADS 10
void* thread(void* idp);
int threads_running = 0;
pthread_mutex_t* mutex;
pthread_cond_t* cond;
int main(int args, char **argv)
{
pthread_mutex_t mutex_var;
pthread_cond_t cond_var;
mutex = &mutex_var;
cond = &cond_var;
pthread_mutex_init(mutex, NULL);
pthread_cond_init(cond, NULL);
pthread_t threads[NUM_THREADS];
for (int i = 0; i < NUM_THREADS; i++)
{
printf("Creating Thread %d....\n", i);
int* id = malloc(sizeof(int));
*id = i;
if(0 != pthread_create(&threads[i], NULL, thread, (void *) id))
{
perror("Error while creating the threads");
exit(1);
}
}
for (int i = 0; i < NUM_THREADS; i++)
{
pthread_join(threads[i], NULL);
}
return 0;
}
void* thread(void* idp)
{
int* id = (int*) idp;
while (1)
{
sleep(*id+2); // Thread work
pthread_mutex_lock(mutex);
threads_running++;
pthread_mutex_unlock(mutex);
pthread_cond_broadcast(cond);
printf("Thread %d WAIT\n", *id);
fflush(stdout);
// Let threads synchronize
pthread_mutex_lock(mutex);
while(threads_running != NUM_THREADS)
{
pthread_cond_wait(cond, mutex);
}
pthread_mutex_unlock(mutex);
printf("Thread %d WAKEUP\n", *id);
fflush(stdout);
// and continue
pthread_mutex_lock(mutex);
threads_running--;
pthread_mutex_unlock(mutex);
}
}
The thing that happens, is that some (usually #9 and one or two others) reach Thread %d WAKEUP but before the others can wake-up threads_running is already smaller than NUM_THREADS.
How can I synchronize multiple threads, so they continue together?
I am having some trouble wrapping my head around the parallel accesses to variables.
I found the answer seconds later:
Change while(threads_running != NUM_THREADS) to while(threads_running != 0 && threads_running != NUM_THREADS)
Change threads_running--; to threads_running = 0;
Now it works perfectly.
I recommend using pthread_cond_signal() than pthread_cond_broadcast(cond);
So the code flow where while loop is exiting looks like below.
pthread_mutex_lock(mutex);
while(threads_running != 0 && threads_running != NUM_THREADS)
{
pthread_cond_wait(cond, mutex);
}
pthread_cond_signal(cond);
So the final code looks like this:
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <pthread.h>
#include <unistd.h>
#define NUM_THREADS 10
void* thread(void* idp);
int threads_running = 0;
pthread_mutex_t* mutex;
pthread_cond_t* cond;
int main(int args, char **argv)
{
pthread_mutex_t mutex_var;
pthread_cond_t cond_var;
mutex = &mutex_var;
cond = &cond_var;
pthread_mutex_init(mutex, NULL);
pthread_cond_init(cond, NULL);
pthread_t threads[NUM_THREADS];
for (int i = 0; i < NUM_THREADS; i++)
{
printf("Creating Thread %d....\n", i);
int* id = malloc(sizeof(int));
*id = i;
if(0 != pthread_create(&threads[i], NULL, thread, (void *) id))
{
perror("Error while creating the threads");
exit(1);
}
}
for (int i = 0; i < NUM_THREADS; i++)
{
pthread_join(threads[i], NULL);
}
return 0;
}
void* thread(void* idp)
{
int* id = (int*) idp;
while (1)
{
sleep(*id+2); // Thread work
pthread_mutex_lock(mutex);
threads_running++;
pthread_mutex_unlock(mutex);
printf("Thread %d WAIT\n", *id);
fflush(stdout);
// Let threads synchronize
pthread_mutex_lock(mutex);
while(threads_running != 0 && threads_running != NUM_THREADS)
{
pthread_cond_wait(cond, mutex);
}
pthread_cond_signal(cond);
pthread_mutex_unlock(mutex);
printf("Thread %d WAKEUP\n", *id);
fflush(stdout);
// and continue
pthread_mutex_lock(mutex);
threads_running = 0;
pthread_mutex_unlock(mutex);
}
}
I have a thread which plays an mp3 using ao_lib in C. I need someway of skipping to the next mp3 halfway through playing, so after creating the thread which plays an mp3, I have tried to create another thread which would wait for a user to enter a character and if the first one joins before the second then the second is also killed.
main()
{
ret1 = pthread_create(&thread1, NULL, func_play, (void *) url);
ret2 = pthread_create(&thread2, NULL, func_char, NULL);
/* I need to somehow do something here to break func_play if a user
enters a specific char in func_char */
pthread_join(thread1, NULL);
pthread_cancel(thread2);
return 0;
}
That didn't work. Any solutions would be really welcome.
Thanks
This would be the pattern that I think would work, I don't know how the mp3 playing function works, but you need it to be non-blocking, perhaps read the .mp3 file and play it in chunks, so you can break the main loop (_the loop that plays the .mp3) using something like this
#include <stdio.h>
#include <pthread.h>
#include <time.h>
#include <unistd.h>
#include <string.h>
static pthread_mutex_t mutex;
struct SharedData
{
const char *url;
int stop;
int ready;
};
void *
mainloop(void *data)
{
struct SharedData *sharedData;
int ready;
int stop;
sharedData = data;
if (sharedData == NULL)
return NULL;
stop = 0;
ready = 0;
while (ready == 0)
{
pthread_mutex_lock(&mutex);
ready = sharedData->ready;
pthread_mutex_unlock(&mutex);
usleep(1000);
}
while (stop == 0)
{
pthread_mutex_lock(&mutex);
stop = sharedData->stop;
pthread_mutex_unlock(&mutex);
printf(".");
fflush(stdout);
sleep(1);
}
return NULL;
}
void *
controlthread(void *data)
{
int chr;
struct SharedData *sharedData;
sharedData = data;
if (sharedData == NULL)
return NULL;
pthread_mutex_lock(&mutex);
sharedData->ready = 1;
pthread_mutex_unlock(&mutex);
printf("Press return to stop the main loop...\n");
while (((chr = getchar()) != EOF) && (chr != '\n'))
usleep(1000);
pthread_mutex_lock(&mutex);
sharedData->stop = 1;
pthread_mutex_unlock(&mutex);
return NULL;
}
int main(void)
{
struct SharedData data;
pthread_t threads[2];
pthread_mutex_init(&mutex, NULL);
memset(&data, 0, sizeof(data));
pthread_create(&threads[0], NULL, mainloop, &data);
pthread_create(&threads[1], NULL, controlthread, &data);
pthread_join(threads[0], NULL);
return 0;
}
I wrote this code in order to better understand producer consumer in a program where I require threads to wait for a task and then work in parallel to complete it.
I am wondering if I really needed an array for the conditionMet integer variables?
Could I not do the same with one integer?
Was there anything I could do to simplify the code?
My goal is to allow the main thread to hand tasks to the other threads.
Since this is just the skeleton, I am wondering if there is a way to simplify it?
#define _MULTI_THREADED
#include <pthread.h>
#include <stdio.h>
#include <unistd.h>
#define NTHREADS 7
#define RUN_N_TIMES 3
pthread_cond_t new_task_available = PTHREAD_COND_INITIALIZER;
pthread_cond_t task_complete = PTHREAD_COND_INITIALIZER;
pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
volatile int producerCond = 0;
volatile int conditionMet[NTHREADS] = {};
volatile int no_more_tasks = 0;
volatile int mini_tasks_complete = - NTHREADS;
void do_work(unsigned int* current_level){
++*current_level;
printf("Thread %u has completed %u units of work.\nIt was #%d to complete it's task this round..\n", (unsigned int)pthread_self(), *current_level, mini_tasks_complete);
}
void *threadfunc(void *parm)
{
int* thread_cond;
unsigned int level = 0;
thread_cond = (int*)parm;
while(!no_more_tasks){
pthread_mutex_lock(&mutex);
++mini_tasks_complete;
if(mini_tasks_complete == NTHREADS){
producerCond = 1;
pthread_cond_signal(&task_complete);
}
threads_waiting++;
*thread_cond = 0;
while (!*thread_cond) {
pthread_cond_wait(&new_task_available, &mutex);
if(no_more_tasks)
{
pthread_mutex_unlock(&mutex);
return NULL;
}
}
pthread_mutex_unlock(&mutex);
do_work(&level);
}
return NULL;
}
void reset_cond(int val){
int i;
for (i=0; i<NTHREADS; ++i)
conditionMet[i] = val;
}
int main(int argc, char **argv)
{
int i;
pthread_t threadid[NTHREADS];
for(i=0; i < NTHREADS; ++i) {
pthread_create(&threadid[i], NULL, threadfunc, &(conditionMet[i]));
}
while(threads_waiting < NTHREADS)
sleep(1); /* Sleep is not a very robust way to serialize threads */
/* The condition has occured. Set the flag and wake up any waiting threads */
printf("Waking up all waiting threads " "#RUN_N_TIMES" " times...\n");
for(i = 0; i < RUN_N_TIMES; i++){
pthread_mutex_lock(&mutex);
mini_tasks_complete = 0;
printf("New tasks available.\n");
sleep(1);
reset_cond(1);
pthread_cond_broadcast(&new_task_available);
producerCond = 0;
while (!producerCond) {
printf("Main waiting\n");
pthread_cond_wait(&task_complete, &mutex);
}
pthread_mutex_unlock(&mutex);
}
no_more_tasks = 1;
pthread_mutex_lock(&mutex);
mini_tasks_complete = 0;
printf("Go home everybody!\n");
pthread_cond_broadcast(&new_task_available);
pthread_mutex_unlock(&mutex);
printf("Wait for threads and cleanup\n");
for (i=0; i<NTHREADS; ++i) {
pthread_join(threadid[i], NULL);
}
pthread_cond_destroy(&new_task_available);
pthread_cond_destroy(&task_complete);
pthread_mutex_destroy(&mutex);
printf("Main done\n");
return 0;
}