Find Jobs
Hire Freelancers

C/C++ Threads and Mutex Communication

$30-250 USD

In Progress
Posted about 12 years ago

$30-250 USD

Paid on delivery
Attached is project 2. Please use project 2 in order to complete project 3. The parent program will create the threads and necessary data structures but won’t do any of the work. Have the parent program create four threads, one for each of the jobs from Project 2, then wait for all the threads to quit before it exits. Three of the threads will handle the work from project 2: read the input; break the input into tokens; classify the tokens. The new job, handled by the fourth thread, will count the number of tokens in each of the four token categories and display those counts after all the tokens have been processed. It will get tokens from the thread that classifies the tokens, and it will use the same method – an “all-done” token – to know when to exit. The threads will communicate using shared-memory queues instead of pipes. Use the STL <queue> data structure, and make it a queue of records using the struct’s defined for Project 2 – one queue for the inputStruct’s and two for the tokenStruct’s. You’ll need to protect any access to each queue by any thread using a mutex. In addition, use a counting semaphore to manage each thread’s access – don’t assume that if the queue is empty, there’s nothing for the thread to do: it should block when the queue is empty using the semaphore, *not* the size of the queue. Declare the queues, mutexes, and counting semaphores as global, and have the parent program initialize them. To use the pthread library, #include <pthread.h> and link with the flag –lpthread. To use the semaphore library, #include <semaphore.h> Here’s a summary of the libraries: pthread_t An internal pthread struct, whose internals can safely be ignored, used as a thread identifier. Declare one for each thread to be created. pthread_create(pthread_t * p, NULL, void *function, void *argument) Creates a thread and dispatches it to run function with the given argument. The function must have the signature void *function(void *parameter). If you need to start that function with an actual argument, consider creating a struct variable, filling it with data, and passing it by address but cast to void *. pthread_join(pthread_t p, NULL) Waits for the indicated thread to complete. pthread_exit(void *status) Kills the thread making this call. pthread_kill(pthread p, void** status) Kills the indicated thread. Example: // in parent: pthread_t p; pthread_create(&p, NULL, fn, NULL); // Use fn( ) as the startup function for p pthread_join(p, NULL); // Wait for p to finish // in thread: void* fn(void) { // some thread code goes here // ... pthread_exit(0); } pthread_mutex_t A mutex (atomic lock) variable struct, whose contents can safely be ignored. Declare one for each mutex needed. A mutex should be statically initialized by setting it equal to PTHREAD_MUTEX_INITIALIZER. pthread_mutex_lock(&pthread_mutex_t) Locks the mutex. pthread_mutex_unlock(&pthread_mutex_t) Unlocks the mutex. Example: // in parent: pthread_mutex_t mylock = PTHREAD_MUTEX_INITIALIZER; // in thread code: pthread_mutex_lock(&mylock); // ... some critical region code goes here ... pthread_mutex_unlock(&mylock); The semaphore library is associated with pthreads, and it provides counting semaphores. sem_t An internal semaphore struct whose internals can safely be ignored. Declare one for each semaphore needed. sem_init(sem_t *, 0, unsigned int count) Initializes the indicated semaphore to count (note: ignore the second parameter for our purposes.) That is, set count to the number of shared resources, or the number of threads that can safely share one resource. sem_wait(sem_t *s) If the semaphore is 0, blocks the thread; otherwise decrements the semaphore’s value. sem_post(sem_t *s) This is what is usually called “signal”. Increments the semaphore’s value; if the old value was zero, wakes up one blocked thread (if any where actually blocked). Example: sem_t mysem; // Make this a gl
Project ID: 1534493

About the project

2 proposals
Remote project
Active 12 yrs ago

Looking to make some money?

Benefits of bidding on Freelancer

Set your budget and timeframe
Get paid for your work
Outline your proposal
It's free to sign up and bid on jobs
Awarded to:
User Avatar
Hello sir, I'm a Linux guru. Please check my PMB.
$150 USD in 1 day
5.0 (1 review)
4.6
4.6
2 freelancers are bidding on average $150 USD for this job
User Avatar
Hi. I can do this perfectly. Thanks.
$150 USD in 2 days
4.9 (19 reviews)
5.1
5.1
User Avatar
HI WE ARE HAVING EXPERIENCED TEAM OF C++ DEVELOPERS. ALSO RECENTLY WE WORK WITH RESEARCH INSTITUTE TO PROVIDE THEM OPTIMIZED SOLUTION ITS COMPLETE DEVELOPMENT IN C++ . SO U CAN THINK THIS BID SERIOUSLY. THANKS
$150 USD in 10 days
0.0 (0 reviews)
0.0
0.0

About the client

Flag of UNITED STATES
Jonesborough, United States
5.0
1
Payment method verified
Member since Mar 29, 2012

Client Verification

Thanks! We’ve emailed you a link to claim your free credit.
Something went wrong while sending your email. Please try again.
Registered Users Total Jobs Posted
Freelancer ® is a registered Trademark of Freelancer Technology Pty Limited (ACN 142 189 759) & Freelancer Online India Private Limited (CIN U93000HR2011FTC043854)
Copyright © 2024 Freelancer Technology Pty Limited (ACN 142 189 759)
Loading preview
Permission granted for Geolocation.
Your login session has expired and you have been logged out. Please log in again.