function 2 only one time execute in thread
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2

Thread: function 2 only one time execute in thread

  1. #1
    Join Date
    Apr 2017
    Posts
    3

    function 2 only one time execute in thread

    I wrote this code in eclipse Linux and when run it only one time function shade() execute and do something after that only function socketHandler() execute,
    why this is happen?

    Code:
    int main(){
     ////bind and listen 
    
      while (true) {
    
        cout << "\n waiting for a connection" << endl;
        csock = (int*) malloc(sizeof(int));
        if ((*csock = accept(hsock, (sockaddr*) &sadr, &addr_size)) != -1) {
        printf("---------------------\nReceived connection from %s\n",inet_ntoa(sadr.sin_addr));
    
    	pthread_t th1, th2, th3;
    
           pthread_create(&thread_id, 0, &SocketHandler, (void*) csock);// thread object
           pthread_create(&th1, 0, Shade, 0);	// create it
    
           pthread_detach(thread_id);
           pthread_detach(th1);
    
        }
    }
    void *SocketHandler(void *lp) {
    
      while (1) {
    int *csock = (int*) lp;
    
        //do something
    
          fflush(stdout);
          usleep(100);
    
    	}
      pthread_exit(0);
    
    }
    void * Shade(void *rt) {
    	while (1) {
    
        //do something
    
          fflush(stdout);
          usleep(200);
    	}
      pthread_exit(0);
    }

  2. #2
    Join Date
    Aug 2017
    Posts
    36

    Re: function 2 only one time execute in thread

    You can create an ExecutorService that only allows a single thread with the Executors.newSingleThreadExecutor method. Once you get the single thread executor, you can call execute with a Runnable parameter:

    Executor executor = Executors.newSingleThreadExecutor();
    executor.execute(new Runnable() { public void run() { /* do something */ } });

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This a Codeguru.com survey!


On-Demand Webinars (sponsored)