Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 348
  • Last Modified:

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.
0
jeffreyg
Asked:
jeffreyg
  • 2
1 Solution
 
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
 
YensidModCommented:
Question has been PAQed and 200 points refunded.

YensidMod
Experts Exchange Moderator
0

Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now