Solved

Stopping a thread.

Posted on 2003-12-03
4
342 Views
Last Modified: 2010-04-22
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
Comment
Question by:jeffreyg
  • 2
4 Comments
 
LVL 45

Expert Comment

by:sunnycoder
ID: 9873305
>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
 

Author Comment

by:jeffreyg
ID: 9882148
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
 

Accepted Solution

by:
jeffreyg earned 0 total points
ID: 9882391
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
 

Expert Comment

by:YensidMod
ID: 9986128
Question has been PAQed and 200 points refunded.

YensidMod
Experts Exchange Moderator
0

Featured Post

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
automating commands in linux on start up 2 47
troubleshooting a process that crashes within 3 mins 11 98
how can i fully uninstall a puppet client? 2 105
awk file 6 56
Have you ever been frustrated by having to click seven times in order to retrieve a small bit of information from the web, always the same seven clicks, scrolling down and down until you reach your target? When you know the benefits of the command l…
The purpose of this article is to demonstrate how we can upgrade Python from version 2.7.6 to Python 2.7.10 on the Linux Mint operating system. I am using an Oracle Virtual Box where I have installed Linux Mint operating system version 17.2. Once yo…
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

832 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question