Discussion:
[omniORB] omniThread problem
Bjørn Kristian Larsen
2008-11-13 23:07:27 UTC
Permalink
I got a segmentation-fault when trying to join omni_thread's to my main
thread. Both when using omni_thread::join() and omni_condition::wait ...

Is there anything logically wrong with my example?
Can i only call join() from another omni_thread?

class TestThread : public omni_thread {
public:
static int workingThreads;

private:
omni_condition& q_working;
int i, j, id, jd;
static omni_mutex * w_mutex;

public:
TestThread(int _i, int _j, omni_condition& working) :
omni_thread(), q_working(working)
{
i = id = _i;
j = jd = _j;
w_mutex->lock();
workingThreads ++;
w_mutex->unlock();
start_undetached();
}
//virtual void run(void* arg);
void* run_undetached(void* ptr) {
while(--i > 0) {
j = jd;
while(--j > 0){
int k = 100000;
while(--k > 0){
double d = (k%j) + (i%j) + (i%j) + (i%j);
}
}
cerr << id << ":" << i << ", ";
}
cerr << "done: " << id << endl;
w_mutex->lock();
workingThreads --;
w_mutex->unlock();
q_working.signal();
}
};

int TestThread::workingThreads = 0;
omni_mutex * TestThread::w_mutex = new omni_mutex();

int main(int argc, char *argv[]) {
cerr << "starting test" << endl;
omni_mutex mutex;
omni_condition condition(&mutex);
{
TestThread t1(1,300,condition);
TestThread t2(3,300,condition);
TestThread t3(4,300,condition);
TestThread t4(5,300,condition);
TestThread t5(6,300,condition);
TestThread t6(7,300,condition);

while(TestThread::workingThreads != 0){
cerr << "Waiting for " << TestThread::workingThreads << "
threads" << endl;
condition.wait();
}
}
cerr << "terminating test" << endl;
return 0;
}
Bruce Visscher
2008-11-14 00:17:47 UTC
Permalink
On Thu, Nov 13, 2008 at 12:07 PM, Bj?rn Kristian Larsen
I got a segmentation-fault when trying to join omni_thread's to my main thread. Both when using omni_thread::join() and omni_condition::wait ...
Is there anything logically wrong with my example?
Yes.

You must always lock the associated mutex before waiting on a
condition. This is required. Also, while it isn't required to lock
the associated mutex before signaling the condition I believe it is
recommended. So, I think you should only have one mutex and you
w_mutex->lock();
workingThreads --;
w_mutex->unlock();
q_working.signal();
until after you have signalled the condition. (Which reminds me: it
is generally preferred to create instances of locking classes like
omni_mutex_lock rather than procedurally calling omni_mutex::lock and
omni_mutex::unlock.)
Can i only call join() from another omni_thread?
I don't know of any restrictions on joining omni_threads from the main thread.
class TestThread : public omni_thread {
static int workingThreads;
omni_condition& q_working;
int i, j, id, jd;
static omni_mutex * w_mutex;
omni_thread(), q_working(working)
{
i = id = _i;
j = jd = _j;
w_mutex->lock();
workingThreads ++;
w_mutex->unlock();
start_undetached();
}
//virtual void run(void* arg);
void* run_undetached(void* ptr) {
while(--i > 0) {
j = jd;
while(--j > 0){
int k = 100000;
while(--k > 0){
double d = (k%j) + (i%j) + (i%j) + (i%j);
}
}
cerr << id << ":" << i << ", ";
}
cerr << "done: " << id << endl;
w_mutex->lock();
workingThreads --;
w_mutex->unlock();
q_working.signal();
}
};
int TestThread::workingThreads = 0;
omni_mutex * TestThread::w_mutex = new omni_mutex();
int main(int argc, char *argv[]) {
cerr << "starting test" << endl;
omni_mutex mutex;
omni_condition condition(&mutex);
{
TestThread t1(1,300,condition);
TestThread t2(3,300,condition);
TestThread t3(4,300,condition);
TestThread t4(5,300,condition);
TestThread t5(6,300,condition);
TestThread t6(7,300,condition);
while(TestThread::workingThreads != 0){
cerr << "Waiting for " << TestThread::workingThreads << " threads" << endl;
condition.wait();
}
}
cerr << "terminating test" << endl;
return 0;
}
_______________________________________________
omniORB-list mailing list
http://www.omniorb-support.com/mailman/listinfo/omniorb-list
Bruce Visscher

Loading...