#Need help with threads and Message Queues

49 messages · Page 1 of 1 (latest)

keen burrowBOT
#

When your question is answered use !solved to mark the question as resolved.

Remember to ask specific questions, provide necessary details, and reduce your question to its simplest form. For tips on how to ask a good question use !howto ask.

floral palm
#

there are no guarantees that your notify_one() call will run after the wait() runs, obviously

#

is there a specific reason you are not using std::thread?

#

cause you do use all the other threading tools in C++11, but not std::thread, why pthread_t ?

#

you need to upload the full files here, I can't debug with these snippets, like, where is the header file with the queue

#

this message queue, are you trying to implement a producer-consumer algorithm?

#

cause a message queue is a special synchronization primitive that is not related to what you wrote

#

you want one thread to add work on a queue, and a second thread to pop items off the queue?

#

did you solve exercise 1 and 2? did you get a grade?

#

for starters, have you tried running thread sanitizer

#

to see if there are races as you believe

#

g++ -fsanitize=thread -O0 -g *.cpp

#

have you tried it?

#

this compiles your program with address sanitizer enabled
now run it

#

why did you kill it with ctrl + C ?

#

is it infinite?

#

kay so, change that to, a fixed ammount for(int i = 0; i < 10; ++i) and track down which thread does exactly what

#

add loads of print statements in the global functions from main.cpp

#

and see how each thread is using your class MsgQueue

#

and force the function rand() to return the exact same random numbers by setting the seed srand(123); at the start of main

#

to have reliable tests due to your sleep calls

floral palm
#

entryGuard is trying to acquire the exitQueue.mtx variable, in the function Message* MsgQueue::receive(unsigned long& id) , what do you mean not handled immideately? when loads of threads are trying to acquire the same mutex, based on randomness the threads will all eventually acquire the mutex

#

I don't understand why 2 queues exist at all

#

why not just one queue?

#

a car entering the queue means, .send
a car leaving the queue means .receive
why do you have 2 queues???

#

kay so refactor the main.cpp file

#

not, whatever this exercise is

floral palm
#

...and they are lying to you, yes

#

and what is the behavior you want to see?

#

I will explain later, focus on the code

#

so you wanted
car 1 enters
car 1 exits
car 2 enters
car 2 exits
?

floral palm
#

and what about multiple cars?

#

if car 3 goes through the entry gate before car 2 does, can car3 pass the exit gate after car2 ?

#

well, your queue is enforcing first-in-first-out behavior,
so maybe get rid of the queue and use unordered_map<car_id, Message*>

#

the entry gate will be represented as one condition_variable
the exit gate will be represented as a another condition_variable

#

instead of just one, like you have in class MsgQueue

#

but your function receive doesn't say "receive car N", instead you receive whatever car is on the queue, and you return that id

#

so I don't understand you

#

explain to me how you expect 2 cars to move through the entry gate and exit gate

#

?

#

yes? is this what you want?

#

then how do you want to force the cars to go through the 2 gates?

#

Explain to me, 5 real life physical cars, pasisng through 2 physical gates, what should happen?

#

so the storage of the parking is a hashmap, and not a queue, definitely

floral palm
#

and your function receive, how does it choose which exact car should leave?

#

if there's car 2, car 4 and car 1 that passed the entry gate, how do you choose which car will be the first car to pass the exit gate?

#

if we need FIFO, meaning that car 2 must be the first car to pass the exit gate, then 4, then 1,
then why did you tell me they dont need to leave in the same order they entered earlier???

#

to solve this task you must clarify exactly what the expected behavior is, otherwise I can't help you