Reputation: 39
I'm new for threads and mutex and ı trying to learn them. I write some code that generally create a queue that enqueue all of the numbers from the file ( this file has nearly 20.000 lines and lots of information) since this file contains a lots of information for procees ı need to have multithreads, at the beginning user create the number of threads, then ı stuck at the part that in a while loop ı wanted see which threads enter the loop for dequing the id from queue, but apparently just the first created thread enters and dequeue all of them, ı used mutex for ensure that while a thread enters the loop make it process (dequeue a number) then unlock this mutex in order to other threads can enter but apprently ı did a mistake . Here is the code `
void printer( DynIntQueue & myQueue) // takes the Dynamic Queue
{
//int count = 0;
queMutex.lock(); // lock it before any threads come it
while(!myQueue.isEmpty()) // check this condition
{
int num;
cout << "Thread " << this_thread::get_id << " is working" << endl; // this is for printing out which threads enter this loop
myQueue.dequeue(num); // deqeueu this number from queue
queMutex.unlock(); // unlock this in order to next thread might enter
queMutex.lock(); // when this thread enters lock it in order to other threads to wait
}
queMutex.unlock(); // if myQueue is empty since it is firsly locked, unlock this
}`
My output is like this: Thread 005C1659 is working Thread 005C1659 is working Thread 005C1659 is working Thread 005C1659 is working Thread 005C1659 is working Thread 005C1659 is working Thread 005C1659 is working Thread 005C1659 is working Thread 005C1659 is working Thread 005C1659 is working
This goes on until the myQueue is empty with the same thread. What can ı do ensure that other threads might enter this loop?
Edited: Here is the main part `
int main()
{
DynIntQueue firstQueue;
ifstream input;
string line;
int numofthreads;
input.open("data.tsv");
getline(input, line); // for first empty
int id, houseAge, avgRooms, avgBedRooms, latitue, longitute, medianPrice;
cout << "Please enter the number of threads you want " << endl;
cin >> numofthreads;
vector <thread> Threads(numofthreads);
while (!input.eof())
{
getline(input, line);
istringstream divider(line);
divider >> id >> houseAge >> avgRooms >> avgBedRooms >> latitue >> longitute >> medianPrice;
firstQueue.enqueue(id);
}
for (int i = 0; i < numofthreads; i++)
{
Threads[i] = thread(&printer, ref(firstQueue));
}
for (int i = 0; i < numofthreads; i++)
{
Threads[i].join();
}
return 0;
}
Upvotes: 1
Views: 2190
Reputation: 81926
Note: std::this_thread::get_id() is a function, so you should be calling it. I assume this is just a copy/paste error.
If I add some work between opening and closing the queue, i clearly see two threads using the queue.
I don't think you have any issue with the code shown.
#include <iostream>
#include <thread>
#include <mutex>
#include <queue>
struct DynIntQueue {
bool isEmpty() const { return q_.empty(); }
void dequeue(int &elem) { elem = q_.front(); q_.pop(); }
std::queue<int> q_{{10, 20, 4, 8, 92}};
};
std::mutex queMutex;
void printer( DynIntQueue & myQueue) {
queMutex.lock();
while(!myQueue.isEmpty()) {
int num;
std::cout << "Thread " << std::this_thread::get_id() << " is working" << std::endl;
myQueue.dequeue(num);
queMutex.unlock();
std::cout << "working outside the lock" << std::endl;
std::cout << "working outside the lock" << std::endl;
std::cout << "working outside the lock" << std::endl;
std::cout << "working outside the lock" << std::endl;
std::cout << "working outside the lock" << std::endl;
queMutex.lock();
}
queMutex.unlock();
}
int main() {
std::cout << "Hello World!\n";
DynIntQueue q;
std::thread t1([&q]() { printer(q); });
std::thread t2([&q]() { printer(q); });
t1.join();
t2.join();
}
$ clang++-7 -pthread -std=c++17 -o main main.c
$ ./main
Hello World!
Thread 139686844172032 is working
working outside the lock
working outside the lock
working outside the lock
working outside the lock
working outside the lock
Thread 139686835779328 is working
working outside the lock
working outside the lock
working outside the lock
working outside the lock
working outside the lock
Thread 139686844172032 is working
working outside the lock
working outside the lock
working outside the lock
working outside the lock
working outside the lock
Thread 139686835779328 is working
working outside the lock
working outside the lock
working outside the lock
working outside the lock
working outside the lock
Thread 139686844172032 is working
working outside the lock
working outside the lock
working outside the lock
working outside the lock
working outside the lock
Upvotes: 3