Results 1 to 10 of 14
Hi,
I have written one application, where i created one thread that handles one task. I am executing this thread after every 4 mins. and exiting from this thread after ...
- 11-24-2008 #1Just Joined!
- Join Date
- Feb 2008
- Posts
- 50
Help: Thread Failed
Hi,
I have written one application, where i created one thread that handles one task. I am executing this thread after every 4 mins. and exiting from this thread after use.
But when i put my application running, after 2 days the thread fails, but application running properly., but the task that i scheduled for this thread getting failed..
so can any tell me how to recover this failing of thread.
Please help me on this.
thanks in advance.
- 12-01-2008 #2Just Joined!
- Join Date
- Nov 2008
- Posts
- 9
Hi,
Thsi is very strange problem and unfortunately I can not help you but plz tell me did you find the solution and if yes plz share with us.
one possible scenario might be like the thread is doing some improper calculations because of wch it is receiving kill signal.
plz share.
- 12-01-2008 #3What exactly do you observe? Do you get any error indications from calls of library functions?so can any tell me how to recover this failing of thread--
Bill
Old age and treachery will overcome youth and skill.
- 12-01-2008 #4Just Joined!
- Join Date
- Feb 2008
- Posts
- 50
Thanks for replies.
I putted all my logs in one txt file, where it shows pthread_create returns < 0.
After that, I have added pthread_join to wait main process for this thread. and pthread_exit((void *)101); at end of thread to know thread correctly exited or not.
So till from my testing it showing correct pthread_exit from logs (log shows 101, after pthread_join)
Can I add more in code to find problem if thread fails again.
Thanks.
- 12-01-2008 #5Just Joined!
- Join Date
- Nov 2008
- Posts
- 9
Hi,
It will be good for analysis if you can add your code and your complete log.
- 12-01-2008 #6That won't be extremely useful.I putted all my logs in one txt file, where it shows pthread_create returns < 0.
With many library calls, a return of -1 means you can examine errno to see what the error is. But that is not the case with POSIX threads.
With functions in the POSIX threads library, returning 0 means no error, just as before. But if there is an error, the error code is returned; that makes error returns be greater than zero, not less than zero. So examine those greater-than-zero values; they'll have the same meanings as you'd use for the content of errno.--
Bill
Old age and treachery will overcome youth and skill.
- 12-02-2008 #7Just Joined!
- Join Date
- Feb 2008
- Posts
- 50
Yes Now, I have also added logs for return value of pthread_create like EAGAIN, EINVAL, EPERM etc.
One other questions is, here i am using 5 global structure variables within different threads but i haven't used any locking mechanism. Is it necessary within my code? I am confusing about whether to use locking or not!
- 12-02-2008 #8Yes indeed! Processing in one thread may be suspended at any time so that another thread may run. Here's an example.i am using 5 global structure variables within different threads but i haven't used any locking mechanism. Is it necessary within my code?
Let's say you have a global unsigned long called counter_1. Let's say the current value of that variable is 5.
Now let's say that there are two threads. I'll call them fred and barney. Both of those threads wish to add 1 to the value of that variable. This is the kind of thing that can happen:
- Fred is running. He says this:
The machine code picks up the current value, 5, and gets set to store the value 6 into the counter.Code:counter_1++;
- But before fred can do that, he gets suspended and barney runs. Barney also says this:
The machine code picks up the current value, 5, and stores the value 6 into the counter.Code:counter_1++;
- Eventually, fred continues running, and stores the value 6 into the counter, as he had originally planned.
Not a good situation. The code needs to lock that value before attempting to change it. In fact, it's a good idea to lock the value even if no changes are intended. If it's something as complicated as a struct, one thread could catch the struct in an inconsistent state, while another thread has modified one member of the struct but not yet another member which must be consistent with the first member somehow.
Hope this helps.--
Bill
Old age and treachery will overcome youth and skill.
- Fred is running. He says this:
- 12-02-2008 #9Just Joined!
- Join Date
- Feb 2008
- Posts
- 50
Thanks a lot Bill, You cleared my confusion.
Which should I prefer Mutex or Semaphore ? Which is better one in this condition?
- 12-02-2008 #10If you're using POSIX threads, use a mutex.Mutex or Semaphore ?
If you're using separate processes with shared memory, use a semaphore.--
Bill
Old age and treachery will overcome youth and skill.


Reply With Quote