Please try not to laugh, but-- why does this not work:
-----snip!-----
#include<iostream>
#include<cc++/config.h>
#include<cc++/macros.h>
#include<cc++/thread.h>
class TThread : public Thread
{
private:
void Run(void)
{
cout << "Hello, World!" << endl;
}
};
int main()
{
TThread* t = new TThread;
t->Start();
}
-----snip!-----
Is seems to do simply nothing. Probably I made
some kind of newbie mistake, but which?
Thanx in advance,
Kalessin
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
First, there is no true default constructor for thread and I am not sure which way your compiler would choose to handle this particular scenareo. Hence, I would suggest:
class TThread : public Thread
{
..... your stuff
public:
TThread() : Thread(NULL) {};
};
and to use a "TThread *t = new TThread".
You will also want to make sure you use the CXXFLAGS from config.def when compiling.
David
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
I tried your suggestions. It didn't do anything. Still
nothing happens. After some further experiments, the code
fragment looks like this:
-----snip!-----
#include<iostream>
#include<cc++/config.h>
#include<cc++/macros.h>
#include<cc++/thread.h>
class TThread : public Thread {
public:
TThread(Semaphore *s=NULL) : Thread(s) {}
TThread() : Thread((Semaphore*) NULL) {}
int main() {
TThread *t = new TThread(NULL);
t->Start();
}
-----snip!-----
I get no error, but it doesn't work either, regardless
which constructor I use. BTW, I use Linux 2.2.?/glibc 2.1,
gcc 2.95.2, and the following compiler call:
Any further ideas? The above construct works just fine
in Java, as simplest possibility. I didn't want to start
with semaphore objects right away, if possible, but is it
perhaps necessary to use one?
Thanx, Kalessin
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
You need a "delete t" before you exit main. This will cause the main thread to wait for the child thread to terminate (or reach a cancellation point) and will then exit with both threads terminated. You may also need to issue a setCancel in the TThread class's Initial member to make sure that the thread is not cancellable until it exits Run, although the default is cancellation deferred, and this should be okay for a cout operation.
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
I am not too sure on how a thread in the THREAD_CANCEL_DEFERRED mode works, but what is sure is that in this mode the thread execution is interrupted by default even if you delete it.
So if you want your program to work you should use the setCancel(THREAD_CANCEL_DISABLED) method in the Initial member (or in the Main member).
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
Yes. Deleting the object *and* THREAD_CANCEL_DISABLED did the trick. I was mainly confused because other implementations of oo threads I used (mainly Java) behave differently. But knowing now where to look in the class docs, the issue is of course clear.
Hmm. A bit of documentation "connecting" the simple class docs and a bit more detailed than OVERVIEW.TXT (perhaps giving examples and/or containing a tutorial) would be nice. Anything in this direction planned?
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
Hi!
Please try not to laugh, but-- why does this not work:
-----snip!-----
#include<iostream>
#include<cc++/config.h>
#include<cc++/macros.h>
#include<cc++/thread.h>
class TThread : public Thread
{
private:
void Run(void)
{
cout << "Hello, World!" << endl;
}
};
int main()
{
TThread* t = new TThread;
t->Start();
}
-----snip!-----
Is seems to do simply nothing. Probably I made
some kind of newbie mistake, but which?
Thanx in advance,
Kalessin
First, there is no true default constructor for thread and I am not sure which way your compiler would choose to handle this particular scenareo. Hence, I would suggest:
class TThread : public Thread
{
..... your stuff
public:
TThread() : Thread(NULL) {};
};
and to use a "TThread *t = new TThread".
You will also want to make sure you use the CXXFLAGS from config.def when compiling.
David
I tried your suggestions. It didn't do anything. Still
nothing happens. After some further experiments, the code
fragment looks like this:
-----snip!-----
#include<iostream>
#include<cc++/config.h>
#include<cc++/macros.h>
#include<cc++/thread.h>
class TThread : public Thread {
public:
TThread(Semaphore *s=NULL) : Thread(s) {}
TThread() : Thread((Semaphore*) NULL) {}
public:
void Run(void) {
cout << "Hello, World!" << endl;
}
};
int main() {
TThread *t = new TThread(NULL);
t->Start();
}
-----snip!-----
I get no error, but it doesn't work either, regardless
which constructor I use. BTW, I use Linux 2.2.?/glibc 2.1,
gcc 2.95.2, and the following compiler call:
g++ -c main.cpp -D_REENTRANT -D_THREAD_SAFE -pthread -O2 -m486 -fno-strength-reduce
g++ -o Threading -lccxx -lpthread main.o
Any further ideas? The above construct works just fine
in Java, as simplest possibility. I didn't want to start
with semaphore objects right away, if possible, but is it
perhaps necessary to use one?
Thanx, Kalessin
You need a "delete t" before you exit main. This will cause the main thread to wait for the child thread to terminate (or reach a cancellation point) and will then exit with both threads terminated. You may also need to issue a setCancel in the TThread class's Initial member to make sure that the thread is not cancellable until it exits Run, although the default is cancellation deferred, and this should be okay for a cout operation.
I am not too sure on how a thread in the THREAD_CANCEL_DEFERRED mode works, but what is sure is that in this mode the thread execution is interrupted by default even if you delete it.
So if you want your program to work you should use the setCancel(THREAD_CANCEL_DISABLED) method in the Initial member (or in the Main member).
Yes. Deleting the object *and* THREAD_CANCEL_DISABLED did the trick. I was mainly confused because other implementations of oo threads I used (mainly Java) behave differently. But knowing now where to look in the class docs, the issue is of course clear.
Hmm. A bit of documentation "connecting" the simple class docs and a bit more detailed than OVERVIEW.TXT (perhaps giving examples and/or containing a tutorial) would be nice. Anything in this direction planned?