Solved

Stopping a thread.

Posted on 2003-12-03
4
340 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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

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 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…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now