Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

pthred_con_timewait problem

14 views
Skip to first unread message

ephlodur

unread,
Jan 29, 2013, 9:53:28 AM1/29/13
to
my problem is with pthread... I have a class that handle access to a msg
queue with the below code..
this the part that is causing me problem...the init for the
pthread_con_timewait is below..
protected:
pthread_cond_t _event_id;
pthread_condattr_t cattr;

// Prevent copying or assignment
CEvent(const CEvent& _arg);
CEvent& operator=(const CEvent& _rhs);
public:
CEvent(){
//pthread_attr_init(&cattr);
pthread_condattr_setclock(&cattr,
CLOCK_MONOTONIC);
pthread_cond_init(&_event_id,&cattr);


the call to the pthread_con_timewait is below

struct timespec ts;
clock_gettime(CLOCK_MONOTONIC,&ts);

ts.tv_sec += delay; //time that I want to wait


lock();//unlock in this case is define as a base
class CEvent...
_ret = pthread_cond_timedwait(&_event_id, &_m_mutex, &ts);
unlock();

but when ever I make that call it always return with EPERM

My system is ubuntu 11.X I have done a lot of google search a lot on
implementation are like that ...If I use gettimeofday a make the proper
adjustment the call to timewait never return ...in other cases when I use
CLOCK_REALTIME it always return with EPERM ... can you give me any pointer
how should this call be implemented on linux...

thanks for any help..

ephlodur

Rainer Weikusat

unread,
Jan 29, 2013, 10:19:31 AM1/29/13
to
ephlodur <ephl...@linuxmail.org> writes:

[...]

> _ret = pthread_cond_timedwait(&_event_id, &_m_mutex, &ts);
> unlock();
>
> but when ever I make that call it always return with EPERM

According to the UNIX(*) standard,

[EPERM]
The mutex type is PTHREAD_MUTEX_ERRORCHECK or the
mutex is a robust mutex, and the current thread does
not own the mutex.

This would mean that the thread invoking pthread_cond_timedwait didn't
lock the _m_mutex mutex.

ephlodur

unread,
Jan 29, 2013, 10:28:51 AM1/29/13
to
As show in my code snippet below from my class CEvent the thread is locl
before the call...
lock();//lock in this case is define as a base
_ret = pthread_cond_timedwait(&_event_id, &_m_mutex, &ts);
unlock();

btw I'm not using any attribute on the mutex..

thanks for any help

Sergei Organov

unread,
Jan 29, 2013, 10:58:03 AM1/29/13
to
When you ask for help, it's a good idea to provide minimal example that
anyone here can compile and run to reproduce the problem.

When you prepare one, you will probably see what's wrong yourself and
won't need to ask anybody ;-)

-- Sergei.

Richard Kettlewell

unread,
Jan 29, 2013, 11:32:34 AM1/29/13
to
ephlodur <ephl...@linuxmail.org> writes:

> my problem is with pthread... I have a class that handle access to a msg
> queue with the below code..
> this the part that is causing me problem...the init for the
> pthread_con_timewait is below..
> protected:
> pthread_cond_t _event_id;
> pthread_condattr_t cattr;
>
> // Prevent copying or assignment
> CEvent(const CEvent& _arg);
> CEvent& operator=(const CEvent& _rhs);
> public:
> CEvent(){
> //pthread_attr_init(&cattr);
> pthread_condattr_setclock(&cattr,
> CLOCK_MONOTONIC);

SUS4 says: “The behavior is undefined if the value specified by the attr
argument to pthread_condattr_getclock() or pthread_condattr_setclock()
does not refer to an initialized condition variable attributes object.”
Though whether that’s really the problem here I couldn’t say...

--
http://www.greenend.org.uk/rjk/

ephlodur

unread,
Jan 29, 2013, 12:30:29 PM1/29/13
to
ok here is the code that I'm using but I cannot see the error in
it ..thank for any help..

the base class for my event
class Lock
{
private:
int _ret;
protected:
pthread_mutex_t _m_mutex;
pthread_mutexattr_t _attr;
// Prevent copying or assignment
Lock(const Lock& _arg);
Lock& operator=(const Lock& _rhs);

public:
Lock(){
_ret = pthread_mutexattr_init(&_attr);
_ret = pthread_mutexattr_settype
(&_attr, PTHREAD_MUTEX_NORMAL);
_ret = pthread_mutex_init
(&_m_mutex,&_attr);
if(_ret != 0) throw "Error";
}
~Lock(){
pthread_mutex_destroy(&_m_mutex);
pthread_mutexattr_destroy(&_attr);
}

int lock(){
_ret = pthread_mutex_lock(&_m_mutex);
return _ret;
}

int unlock(){
_ret = pthread_mutex_unlock(&_m_mutex);
return _ret;
}

};

below is my event class..

class CEvent : public Lock
{
private:
struct timespec _ts;
struct timeval _tp;
int _ret;

protected:
pthread_cond_t _event_id;
pthread_condattr_t cattr;

// Prevent copying or assignment
CEvent(const CEvent& _arg);
CEvent& operator=(const CEvent& _rhs);
public:
CEvent(){
pthread_condattr_init(&cattr);
_ret = pthread_condattr_setclock(&cattr,
CLOCK_MONOTONIC);
pthread_cond_init(&_event_id,&cattr);

}
~CEvent(){
pthread_condattr_destroy(&cattr);
pthread_cond_destroy(&_event_id);
}

int wait(){
lock();
_ret = pthread_cond_wait(&_event_id, &_m_mutex);
unlock();
return _ret;
}

int wait_timeout(int delay)
{

struct timespec ts;
clock_gettime(CLOCK_MONOTONIC,&ts);

ts.tv_sec += delay;


lock();
_ret = pthread_cond_timedwait(&_event_id, &_m_mutex, &ts);
unlock();
return _ret;
}

int notify(){
_ret = pthread_cond_signal(&_event_id);
return _ret;
}

};

this is my msg queue..

struct gLog_queue
{

public:
void addMsg(LOGMSG *msg) {
_result = _m_Event.lock();
_queue.push_back(_msg);
_result = _m_Event.notify();
_result = _m_Event.unlock();
}

LOGMSG *getMsg() {
_msg = NULL;

_result = _m_Event.lock();
if(_queue.size() != 0)
{
_msg = _queue.front();
_queue.pop_front();
}
_result = _m_Event.unlock();

return _msg;
}

int wait_for_msg_time(int delay){return _result =
_m_Event.wait_timeout(delay);}

int wait_for_msg(){return _result = _m_Event.wait();}

int getResult(){return _result;}

private:
CEvent _m_Event;
unsigned int _count;
pthread_t _thread_id;
std::list<LOGMSG* > _queue;
int _result;
LOGMSG* _msg;
};


this is the definition for the log queue
extern struct gLog_queue app_log_queue;

and this is my call that return EPERM..
ret = app_log_queue.wait_for_msg_time(delay);

Ps: the wait for message work wait_for_msg_time() it he
wait_for_msg_time_time() does not work..


Thanks again for any help

ephlodur.


Sergei Organov

unread,
Jan 29, 2013, 1:44:40 PM1/29/13
to
ephlodur <ephl...@linuxmail.org> writes:

>> On Tue, 29 Jan 2013 19:58:03 +0400, Sergei Organov wrote:
[...]
>> When you ask for help, it's a good idea to provide minimal example that
>> anyone here can compile and run to reproduce the problem.
>>
>> When you prepare one, you will probably see what's wrong yourself and
>> won't need to ask anybody ;-)
>>
>> -- Sergei.
>
> ok here is the code that I'm using but I cannot see the error in
> it ..thank for any help..
>
> the base class for my event
> class Lock
> {
[...]

Sorry, you didn't understand. What you provided won't compile, and it is
definitely not minimal. Write small, minimal C program, say,
"cond_wait.c" that has particular sequence of calls that demonstrates
the problem and that could be compiled and run. Then, the body of your
mail should contain the exact output of

$ cat cond_wait.c

command.

BTW, also notice that C program won't have any classes at all.

-- Sergei.

ephlodur

unread,
Jan 30, 2013, 6:28:39 AM1/30/13
to
Thank you Sergei for your help...I manage to get it to work...
0 new messages