-
October 28th, 2017, 03:45 AM
#1
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);
}
-
November 8th, 2017, 05:10 AM
#2
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|