1

I have a function say void *WorkerThread ( void *ptr).

The function *WorkerThread( void *ptr) has infinite loop which reads and writes continously from Serial Port

example

void *WorkerThread( void *ptr)
{  
while(1)  
     {  
     // READS AND WRITE  from Serial Port USING MUXTEX_LOCK AND MUTEX_UNLOCK 


    } //while ends  

}     

The other function I worte is ThreadTest example

int ThreadTest()  
{  
    pthread_t Worker;  
    int iret1;  
    pthread_mutex_init(&stop_mutex, NULL);  
    if( iret1 = pthread_create(&Worker, NULL, WorkerThread, NULL) == 0)  
    {  
     pthread_mutex_lock(&stop_mutex);  
     stopThread = true;  
     pthread_mutex_unlock(&stop_mutex);  
    }  

 if (stopThread != false)  
     stopThread = false;  
pthread_mutex_destroy(&stop_mutex);  

return 0;    
}    

In main function
I have something like

int main(int argc, char **argv)  
{  

    fd = OpenSerialPort();  
    if( ConfigurePort(fd) < 0) return 0;  

    while (true)   
    {

        ThreadTest();  
    }
return 0;  
}  

Now, when I run this sort of code with debug statement it runs fine for few hours and then throw message like "can't able to create thread" and application terminates. Does anyone have an idea where I am making mistakes. Also if there is way to run ThreadTest in main with using while(true) as I am already using while(1) in ThreadWorker to read and write infinitely. All comments and criticism are welcome. Thanks & regards, SamPrat.

samprat
  • 2,150
  • 8
  • 39
  • 73

5 Answers5

1

You are creating threads continually and might be hitting the limit on number of threads. Pthread_create man page says:

EAGAIN Insufficient resources to create another thread, or a system-imposed
              limit on the number of threads was encountered.  The latter case may
              occur in two ways: the RLIMIT_NPROC soft resource limit (set via
              setrlimit(2)), which limits the number of process for a real user ID,
              was reached; or the kernel's system-wide limit on the number of
              threads, /proc/sys/kernel/threads-max, was reached.

You should rethink of the design of your application. Creating an infinite number of threads is not a god design.

[UPDATE]

you are using lock to set an integer variable:

pthread_mutex_lock(&stop_mutex);  
     stopThread = true;  
     pthread_mutex_unlock(&stop_mutex);

However, this is not required as setting an int is atomic (on probably all architectures?). You should use a lock when you are doing not-atomic operations, eg: test and set

take_lock ();
if (a != 1)
a = 1
release_lock ();  
Vikram.exe
  • 4,565
  • 3
  • 29
  • 40
  • exe Thanks for pointing it , I guess that may be the reason for termination of my program but in function WorkerThread, I am clearly killing muxtex . So I guess thread gets killed . May be I am wrong, do you have alternative suggestions? Thanks and regards, Samprat – samprat Jan 04 '11 at 11:21
  • int is very often not atomic, even just for set. – Flexo Jan 04 '11 at 11:26
  • @ samprat, killing or destroying a mutex doesn't kill the threads. You will have to kill the threads separately. Also I see that you are destroying the mutex in `threadTest` function, and as mentioned in your comment, if you are destroying it again, you might get a crash. – Vikram.exe Jan 04 '11 at 11:27
  • To reuse the thread, you should create a fixed number of threads and modify your `workerThread` function so that every individual thread repeats its task after finishing it once. – Vikram.exe Jan 04 '11 at 11:30
  • as pointed by `awoodland`, if that's the case, you should ignore my update part of the answer. (I am not deleting it though, hoping it might help some one) – Vikram.exe Jan 04 '11 at 11:32
1

You create a new thread each time ThreadTest is called, and never destroy these threads. So eventually you (or the OS) run out of thread handles (a limited resource).

Péter Török
  • 114,404
  • 31
  • 268
  • 329
1

Threads consume resources (memory & processing), and you're creating a thread each time your main loop calls ThreadTest(). And resources are finite, while your loop is not, so this will eventually throw a memory allocation error.

You should get rid of the main loop, and make ThreadTest return the newly created thread (pthread_t). Finally, make main wait for the thread termination using pthread_join.

jweyrich
  • 31,198
  • 5
  • 66
  • 97
1

Your pthreads are zombies and consume system resources. For Linux you can use ulimit -s to check your active upper limits -- but they are not infinite either. Use pthread_join() to let a thread finish and release the resources it consumed.

Do you know that select() is able to read from multiple (device) handles ? You can also define a user defined source to stop select(), or a timeout. With this in mind you are able to start one thread and let it sleeping if nothing occurs. If you intent to stop it, you can send a event (or timeout) to break the select() function call.

An additional design concept you have to consider is message queues to share information between your main application and/or pthread. select() is compatible with this technique so you can use one concept for data sources (devices and message queues).

Here a reference to a good pthread reading and the best pthread book available: Programming with POSIX(R) Threads, ISBN-13:978-0201633924

Community
  • 1
  • 1
Raphael Bossek
  • 1,904
  • 14
  • 25
0

Looks like you've not called pthread_join() which cleans up state after non-detached threads are finished. I'd speculate that you've hit some per process resource limit here as a result.

As others have noted this is not great design though - why not re-use the thread rather than creating a new one on every loop?

Community
  • 1
  • 1
Flexo
  • 87,323
  • 22
  • 191
  • 272
  • how to reuse the thread? Actually I am trying to achieve something like thats why I dont want to call While (true) inside main. – samprat Jan 04 '11 at 11:23
  • The thread still exists, so rather than letting it exit you usually loop, blocking after completing the work, until something signals that there is more work to be done. The blocking can be implemented with a mutex for example. – Flexo Jan 04 '11 at 11:25