0

I am trying to learn the std::threads, std::mutex and std::unique_lock. The idea is to create 5 threads.

Requests will be written to a queue and the 5 threads will pick up request from the queue and process it.

If the queue is empty the threads will wait and this will continue until the user keys in "done".

The code does a clean compile.

When I execute it causes a segmentation fault when the thread is created.

Not able to figure out what I am doing wrong from the debugger. Please help.

#0  0x0000003710e0e45c in _dl_fixup () from /lib64/ld-linux-x86-64.so.2
#1  0x0000003710e14c55 in _dl_runtime_resolve () from /lib64/ld-linux-x86-
64.so.2
#2  0x0000003713ab65a7 in 
std::thread::_M_start_thread(std::shared_ptr<std::thread::_Impl_base>) () 
from /usr/lib64/libstdc++.so.6
#3  0x000000000040247a in std::thread::thread<void (ThreadPool::*)(), 
ThreadPool* const> (this=0x7fffffffe3a0, __f=@0x7fffffffe3b0, 
__args#0=@0x7fffffffe3a8)
at /usr/lib/gcc/x86_64-redhat-
linux/4.4.7/../../../../include/c++/4.4.7/thread:133
#4  0x0000000000401f1c in ThreadPool::createthreads (this=0x7fffffffe3f0, 
count=5) at ThreadPool.cpp:73
#5  0x00000000004016e4 in main () at ThreadPool.cpp:99

Here is the full code

#include <queue>
#include <string>
#include <iostream>
#include <mutex>
#include <thread>
#include <vector>
#include <condition_variable>

using namespace std;

void handler(string req, int s)
{
    cout << "Process request " << req << " by thread " << 
    this_thread::get_id()<<endl;;
}

class ThreadPool
{
    vector<thread> threadlist;
    queue<pair<string, int> > requestQueue;
    mutex qLock;
    condition_variable queued;
    volatile bool breakout;

    void (*callback)(string,int);

public:

ThreadPool()
{
    breakout=false;
}

void setcallback(void (*fp)(string, int))
{
    callback=fp;    
}


void worker()
{
    while (!breakout)
    {
        pair<string, int> request;
        {
            unique_lock<mutex> lock(qLock);

            while((!breakout) && (requestQueue.empty()))
            {
                queued.wait(lock);      
            }   

            if (!requestQueue.empty())
            {
                request = requestQueue.front();
                requestQueue.pop();
            }
        }

        if (!request.first.empty())
        {
            callback(request.first,request.second);
        }
    }

}

void createthreads(int count)
{
    for (int i=0;i<count;i++)
    {
        threadlist.push_back(thread(&ThreadPool::worker,this)); 
    }
}   



};



int main()
{
    ThreadPool tp;

    tp.setcallback(handler); 
    tp.createthreads(5);

    return(0);
}
david
  • 485
  • 1
  • 4
  • 10
  • [Mcve] -- please eliminate parts of code that are not required to produce crash. – Yakk - Adam Nevraumont May 18 '18 at 11:27
  • should I add -lpthread during linking? because it work when I do g++ -ggdb ThreadPool.cpp -L. -I. -std=gnu++0x -lpthread – david May 18 '18 at 11:32
  • 4
    `-pthread`, not `-lpthread`. – YSC May 18 '18 at 11:34
  • 5
    From a quick glance your code looks ok - although using a `volatile bool breakout` like you do has undefined behaviour - use a `std::atomic` instead although that's probably not your issue. – Mike Vine May 18 '18 at 11:34
  • 1
    @david for gcc: yes, standard C++11 threads require you to link thread lib. see the answer here: https://stackoverflow.com/questions/8649828/what-are-the-correct-link-options-to-use-stdthread-in-gcc-under-linux – roalz May 18 '18 at 11:34
  • Thanks everyone for the help. Noted -pthread and std::atomic – david May 18 '18 at 11:46

0 Answers0