开发者

how to run thread in main function infinitely without causing program to terminate

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.


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 ();  


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).


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.


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


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?

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜