Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Stopping a thread.

Posted on 2003-12-03
4
Medium Priority
?
347 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Learn by Doing. Anytime. Anywhere.

Do you like to learn by doing?
Our labs and exercises give you the chance to do just that: Learn by performing actions on real environments.

Hands-on, scenario-based labs give you experience on real environments provided by us so you don't have to worry about breaking anything.

Question has a verified solution.

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

The purpose of this article is to fix the unknown display problem in Linux Mint operating system. After installing the OS if you see Display monitor is not recognized then we can install "MESA" utilities to fix this problem or we can install additio…
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…
Video by: ITPro.TV
In this episode Don builds upon the troubleshooting techniques by demonstrating how to properly monitor a vSphere deployment to detect problems before they occur. He begins the show using tools found within the vSphere suite as ends the show demonst…
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…

705 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