Stopping a thread.

I have a function to allow thread creation and I use the following to kick of my threaded function :

pthread_create(&pThread, NULL, (void *)MonitorThread, NULL);

with my thread function being :

void *AppPing::MonitorThread(void *)

Now the creation works and it starts sending infor out of the serial port ttyS0 and reveiving info back, displaying the data sent out / received.

I have another function which disables the thread by setting a static bool. The thread checks this and if it is true, disables the thread by using the

pthread_exit(NULL); command.

Now this seems ok but if I enable the thread again I get a burst of activity on the serial port as if the thread never stopped sending info out of the serial port.

Am I killing the thread correctly? I fear not!

regards,

Gordon.
jeffreygAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

sunnycoderCommented:
>Am I killing the thread correctly? I fear not!
Yes you are killing the thread correctly .... pthread_exit() will terminate the thread immediately ... the burst of activity on creation of new thread could be due to ..

1. some pending data from previous thread which you did not process
2. High speed of (initial) data generation in current thread
0
jeffreygAuthor Commented:
Ok, one question within the thread, I write to the com port and retrieve data from it.

The code is as follows :

hCom = open("/dev/ttyS0", O_RDRW | O_NOCTTY | O_NDELAY);

int controlbits;
ioctl(hCom, TIOCMGET, &controlbits);
controlbits &= TIOCM_DTR;
controlbits &= TIOCM_RTS;
ioctl(hCom, TIOCMSET, &controlbits);

// create the thread with the pthread_create command
pthread_create(&pThread, NULL, (void *)MonitorThread, NULL);

Now in the thread function I do the following :

while (!KillThread)
{
  char toWrite[2] = "6\n";
  int wordsWritten = write(hCom, toWrite, 2);

  char toRead[2];
  int wordsRead = read(hCom, toRead, 2);

  usleep(1000000);    // sleep a second to allow other processes to work

  printf(toRead);
}

The problem comes in the thread that on first time it is called it takes almost 20 seconds to read the data from the com port. It writes data ok as the return value is the number of bytes written, but cannot read it.

Then all of a sudden .... boom it starts reading it! What is up with this?????

regards,

Gordon.
0
jeffreygAuthor Commented:
It's ok, I figured out what was causing the delay. Don't know why though. I commented out setting the RTS and DTR. Hey presto, the serial loopback comes though straight away!

Strange!

regards,

Gordon.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
YensidModCommented:
Question has been PAQed and 200 points refunded.

YensidMod
Experts Exchange Moderator
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Linux OS Dev

From novice to tech pro — start learning today.