LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 12-07-2017, 06:25 PM   #1
minux
LQ Newbie
 
Registered: Dec 2015
Location: Jakarta
Distribution: Tiny Core
Posts: 13
Blog Entries: 1

Rep: Reputation: Disabled
Question Quit from thread loop epoll


Hello,
I need a better way to quit from the thread loop without changing epoll timeout(-1) or creating a dummy socket

test.c
Code:
#include <stdio.h>
#include <pthread.h>
#include <sys/epoll.h>
#include <sys/socket.h>
#include <sys/un.h>
#include <unistd.h>

#define SOCKNAME "/tmp/blah.socket"

int sock = -1,running;

void* myth(void* arg) {
	struct epoll_event ev,events[1];
	int efd = epoll_create1(0);
        ev.events = EPOLLIN;
        ev.data.fd = sock;
        if (epoll_ctl(efd, EPOLL_CTL_ADD,sock, &ev) < 0) return NULL;
	int nc = 1;
	while(running){
		printf("waiting ...\n");
		int nfd = epoll_wait(efd,events,nc,-1);
		if (nfd == -1) break;
		
		for (int i = 0; i < nfd; i++) {
			//code
		}
	}
	printf("closing \n");
	close(efd);
	return NULL;
}

int init_socket(const char *s){
	struct sockaddr_un sa;
	unlink(s);
	int sck = socket(AF_UNIX, SOCK_STREAM, 0);
	sa.sun_family = AF_UNIX;
	strcpy(sa.sun_path,s);
	if (bind(sck,(const struct sockaddr *)&sa,(socklen_t)sizeof(sa)) <0) {
		return -1;
	}
	if (listen(sck,5) < 0 )return -1;
	return sck;
}

int main(){
	pthread_t th;
	sock = init_socket(SOCKNAME);
	running = 1;
	pthread_create(&th,NULL,myth,NULL);
	sleep(3);
	running = 0;
	
	struct sockaddr_un sa;
	int sock1 = socket(AF_UNIX,SOCK_STREAM, 0);
	sa.sun_family = AF_UNIX;
	strcpy(sa.sun_path,SOCKNAME);
	connect(sock1,(const struct sockaddr *)&sa,sizeof(sa));
	
	printf("joining ...\n");
	pthread_join(th,NULL);
	close(sock1);
	close(sock);
	printf("closed\n");
	return 0;
}
Code:
gcc -o test test.c -lpthread
Thanks.

Last edited by minux; 12-08-2017 at 07:39 PM. Reason: Grammar
 
Old 12-11-2017, 12:42 AM   #2
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,847

Rep: Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309
Hm. I think the usual way is to set running to 0.
Otherwise please use:
Code:
gcc -pthread -o test test.c # no -lpthread
 
Old 12-11-2017, 10:05 PM   #3
minux
LQ Newbie
 
Registered: Dec 2015
Location: Jakarta
Distribution: Tiny Core
Posts: 13

Original Poster
Blog Entries: 1

Rep: Reputation: Disabled
Quote:
Originally Posted by pan64 View Post
Hm. I think the usual way is to set running to 0.
Otherwise please use:
Code:
gcc -pthread -o test test.c # no -lpthread
Thanks, I'll check that.
Browsing from Win at the moment.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Using EPOLL Library YehudaSinger Linux - Desktop 1 12-03-2015 09:03 AM
How to quit continuous "while" loop jefferj54 Linux - Newbie 6 09-02-2013 02:53 AM
Asynchronous I/O and epoll sols2008 Linux - Networking 1 06-28-2011 10:08 PM
epoll jopu Linux - Networking 0 11-05-2007 11:27 PM
epoll problem Parahat Melayev Programming 1 03-17-2005 05:22 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 01:47 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration