开发者

Inter-thread communication with Sockets without blocking methods

开发者 https://www.devze.com 2023-02-09 15:54 出处:网络
I now have successfully started the amount of threads I want to start for m开发者_如何转开发y vector clock test program. The next problem seems to be the inter-thread communication; the config files t

I now have successfully started the amount of threads I want to start for m开发者_如何转开发y vector clock test program. The next problem seems to be the inter-thread communication; the config files the threads read have a list of IP address (localhost) and port combinations and they should each start listening on the port given to them and at the same time read through the input file, which tells them to either increment their logical clock or send a message to a certain other thread.

If I first create ServerSockets for each thread, the accept() method will apparently block until someone tries to connect to this certain thread via a socket and meanwhile this thread cannot perform other actions that might've been instructed in the input file (which only has lines in the format ("int sendMessageTo int" or "int incrementClock k" - the integers represent lines in the config file, which contains the host & port listing). Is there a way to avoid this?


On linux you can use accept4 instead of accept and pass SOCK_NONBLOCK as the flags parameter.

On POSIX.1-2001, SVr4, 4.4BSD conforming system you can set O_NONBLOCK using fcntl.

0

精彩评论

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

关注公众号