Threading is a big topic, and *very* easy to get wrong, so this may be the
wrong place to ask that question; there are certainly better resources on the
internet to learn about multi-threading your code.
I'll give some pointers below, but without a firm grasp on threading concepts,
you may find the information I give you just helps you to really mess things
up!
So, anyway, be careful...
> I have created an thread in the following way.. But how to stop it or
> finish it.
The first thing you need to decide is: Under what conditions should the thread
terminate?
If you want to terminate a thread that has gone awry, you'll need to look at
pthread_cancel() or maybe pthread_kill(), but either of those might leave your
program in an indeterminate state, so you generally DO NOT want to use them
like that...
(And this assume you are using pthreads - if not, the calls are different, and
since fltk does not provide abstraction of the OS threading layers, you need to
use the appropriate calls yourself, or use a cross-platform threads library
that will do the Right Thing for you. OSX and Linux are of course pthread
based, so it is usually only Windows that you need a special case for.)
In the benign case, where your thread has a discrete job to do and has finished
its work, it can just return, noting more needs to be done.
If the thread is looping, then some sort of IPC can be used to send a message
to the thread to tell it there is no more work for it, and asking it to expire.
This can be as simple as setting a global variable that both the worker thread
and the main context can see:
volatile unsigned run_thread = 1;
:
:
void * a_thread(...)
{
while(run_thread)
{
:
}
return NULL;
}
:
:
int main(...)
{
:
:
run_thread = 0; // cause worker thread to expire...
:
}
> I am confused about locking and unlocking the thread.
What is it about locking/unlocking you are having issues with?
If the worker thread does not modify any fltk widgets, it need not use the fltk
locks at all.
If it does modify fltk widgets, the worker thread will need to acquire a lock
before doing so, and release the lock as soon as possible after.
You may also need to look at awake() to see how to tell the main thread that
the worker has modified some widgets that will now need to be scheduled for
redrawing.
If you do want to use the locks, then the main thread itself must call
Fl::lock(); itself, exactly one, very near the start of your program, and
preferably before it shows any windows.
Thereafter, the main thread must not explicitly call lock/unlock ever - the
fltk core will handle all that for you.
All worker threads however must call lock before they update a widget and
unlock again as soon as they possibly can.
> and kindly suggest me, is it a right way to create a thread for doing some
> heavy computation.
Looks OK, though I do not know the details of your code, so on the basis of
what is there, it looks OK.
As I suggested above, I'd replace the for(;;) with a while(run_thread), so as
to make it easy to exit the thread.
> as you see I am using the break but i don't think so
> its working. now what i am doing is to stop this thread, i switched using
> a button with if else statement.
See above.
> actually it is working in my current program but sometime it makes some
> short delay or something like that. Delay is because of some heavy
> computation i think so. But whatever the computation will be, it should
> work smoothly. right?
How many cores does your machine have?
If it has more than one, then the OS will *probably* dispatch threads to
separate cores and things will run in parallel, so a thread doing heavy
computation will not interfere too much with other threads.
However, if you only have a single core, the OS will have to timeslice to
multi-thread, so a thread doing heavy computation may affect the performance of
other threads. In that case, it might help to throw a few sched_yield(); (or
Sleep(0); on Windows hosts...) calls into the heavy thread, so as to force it
to let other threads have a shot...
Also, be careful with locking - applying locks/unlocks inappropriately will
badly mess up the refreshing of the display, so make sure you get that right!
> void* MultiThread(void* p)
> {
> for (;;)
> {
> if(isV)
> {
>
> if(nb > 0)
> for (int ix = 0; ix<nb; ix++)
> ComputeV(ix);
> }
> else
> break;
>
> }
> return 0L;
> }
>
> somewhere in program:
>
> Fl::lock();
> fl_create_thread(V_thread, MultiThread, 0);
Hmm, careful there... fl_create_thread() is not an officially supported API, it
is only provided to make the threads demo work and is not really meant for
general use.
Though it will probably be OK, just be aware...
SELEX Galileo Ltd
Registered Office: Sigma House, Christopher Martin Road, Basildon, Essex SS14
3EL
A company registered in England & Wales. Company no. 02426132
********************************************************************
This email and any attachments are confidential to the intended
recipient and may also be privileged. If you are not the intended
recipient please delete it from your system and notify the sender.
You should not copy it or use it for any purpose nor disclose or
distribute its contents to any other person.
********************************************************************
_______________________________________________
fltk mailing list
[email protected]
http://lists.easysw.com/mailman/listinfo/fltk