W
W
wAngel2011-11-01 10:17:12
linux
wAngel, 2011-11-01 10:17:12

Working with pthread mutex?

Greetings.
There was a question with the work of pthread_mutex_t and pthread_mutex_lock.
There is a multithreaded server class:

class Server
{
private:
  pthread_mutex_t _queryQueueMutex;

  // ... code ...c

public:
  Server();
  bool f();

  // ... code ...
};

Server::Server()
{
  if (pthread_mutex_init(&_queryQueueMutex, NULL))
    throw "err";
}

bool Server::f()
{
  pthread_mutex_lock(&_queryQueueMutex);

  // ... code ...

  pthread_mutex_unlock(&_queryQueueMutex);
  return false;
}

In which a mutex (pthread_mutex_t _queryQueueMutex) is used to synchronize some thread action.
When creating the server object as
Server* srv = new Server();
srv->f();
everything works correctly as expected.
When creating the server object as
Server srv();
srv.f();
The thread's execution stops at the pthread_mutex_lock(&_queryQueueMutex) call in the f() function.
Logic suggests that the matter is in the initialization of mutexes and memory allocation, but no errors occur, pthread_mutex_trylock works without errors. The problem is not critical, but I really want to understand the essence. Here I found a similar problem, but without a solution.
I would be very grateful for your help in this matter.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
G
Gribozavr, 2011-11-02
@gribozavr

server srv(); is a function declaration, not an object creation. After that, srv.f() should not compile at all.
If we write Server srv; then it works for me, let's complete the non-working code.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question