2

I need a thread that will call continuously in while(1), but when i use to call thread function by pthread_create() a new thread creates.
I need help on following points :~
1) Is there any method to call thread function without creating thread.
2) Is there any method to destroy the previous thread.

Sample code is

void main()
{
pthread_t thread1;
 while(1)
 {
        pthread_create( &thread1, NULL, myFun,  (void*) NULL);
 }
}
void * myFun(void *ptr)
{
printf("Hello");
}


* We can not create more than 380 threads, Here we have to use only single thread.

pramod kumar
  • 41
  • 1
  • 2
  • 8

4 Answers4

1

um, I think what you really want to do is like that:

bool received_data = false;
bool beExit = false;    
struct recvPacket;

pthread_mutex_t recvMutex;

void main()
{
    pthread_t thread1;
    void *status;

    pthread_mutex_init(&recvMutex, NULL);
    pthread_create(&thread1, NULL, myFun,  (void*) NULL);        

    while(1)
    {
        if (received_data) {
           pthread_mutex_lock(&recvMutex);             // you should also synchronize received_data and beExit valuables, cuz they are shared by two threads
           /* do what you want with recvPacket */
           pthread_mutex_unlock(&recvMutex);

           received_data == false;
        }

        /* do else you want, or you can let beExit = true to stop the thread */
    }

    if (err = pthread_join(thr_main, &status))
      printf("pthread_join Error. %s\n", strerror(err)), exit(1);
}

void * myFun(void *ptr)
{
    while (!beExit) {
        if (true == tryRecvPacket()) {
           pthread_mutex_lock(&recvMutex);
           /* fill data to recvPacket */
           pthread_mutex_unlock(&recvMutex);
           received_data = true;
        }
    }
}
elson2885150
  • 86
  • 1
  • 3
  • You need to synchronize `beExit` and `receivedData` also. Ideally with `std::atomic`. – RedX Oct 18 '13 at 09:26
0

Suggested design is.

void * myFun(void *ptr);

volatile int thread_exit = 1; 

void main()
{
        pthread_t thread1;

        pthread_create( &thread1, NULL, myFun,  (void*) NULL);       

        while(1)
        {
             //Ask every 10 sec for killing thread like
             sleep(10);
             printf("Do you wish to kill [0/1]???");
             scanf("%d", &thread_exit); 
        } 


        //Add code here to make main() to wait till thread dies. 
        //pthread_join(&thread1);
}

void * myFun(void *ptr)
{
  while(thread_exit)
  {
     printf("Hello");
  }
}
Jeyaram
  • 9,158
  • 7
  • 41
  • 63
  • Thanks for your quick reply, But We can't use like this because We have to run while(1) in main() and some function ( like accept of socket programming) in thread.. – pramod kumar Oct 17 '13 at 06:03
  • @pramodkumar as per your code sample, it will keepon creating threads. which makes no sense. – Jeyaram Oct 17 '13 at 06:04
  • @Jeyram, Yes I know but how can I solve my problem, Actually I am writing code for accepting tcp client requests for socket, There is a function accept() to accept requests by the client, but accept waits for the incoming request and i can not hold the program because there are some other tasks are using in our program .. – pramod kumar Oct 17 '13 at 06:06
  • I think you confused with thread and function call. if you want some thing to be done parallely, then go for threads, else just use function call. – Jeyaram Oct 17 '13 at 06:09
  • I am not confuse function call can not run parallel, I require parallel executing. – pramod kumar Oct 17 '13 at 06:10
  • Thanks for your program design .. But how to kill a thread please reply, i have used pthread_kill, pthread_cancel, pthread_exit but not get satisfactory solution. – pramod kumar Oct 17 '13 at 06:25
  • 1. http://stackoverflow.com/questions/3438536/when-to-use-pthread-cancel-and-not-pthread-kill/17145588#17145588 2.http://stackoverflow.com/questions/4760687/cancelling-a-thread-using-pthread-cancel-good-practice-or-bad?rq=1 – Jeyaram Oct 17 '13 at 06:27
  • If thread killing is possible then target can be achieved, otherwise if i use the above code design after exit (when input is zero) the while(thread_exit) in myFun, i can not move again to the function body, help me out plz – pramod kumar Oct 17 '13 at 06:31
0

For destroying the thread, you can use pthread_kill function available

int pthread_kill(pthread_t thread, int sig);

But its not good practice to kill the thread forcefully as it might cause the problem of the memory leak.

When the parent thread is finished, then all the child threads are finished abruptly. In your example, as child thread will be in the infinite loop so child thread never finishes. But parent thread-main thread will finish after executing the return 0 statement. So main thread will terminate the child thread also.

To overcome this situation i.e. wait until all child threads have finished, pthread_join(thread1) method is there. So the thread calling this function will wait unless thread1 has finished.

ajay_t
  • 2,347
  • 7
  • 37
  • 62
0

Thanks to all for quick reply..
Problem has been fixed by using following code.

setsockopt (socket_desc, SOL_SOCKET, SO_RCVTIMEO, (char *)&timeout,sizeof(timeout));


Here i set the timeout for accept the tcp clients request in microseconds.. now code is working OK.

pramod kumar
  • 41
  • 1
  • 2
  • 8