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 03-06-2022, 07:40 PM   #1
Skaperen
Senior Member
 
Registered: May 2009
Location: center of singularity
Distribution: Xubuntu, Ubuntu, Slackware, Amazon Linux, OpenBSD, LFS (on Sparc_32 and i386)
Posts: 2,693
Blog Entries: 31

Rep: Reputation: 176Reputation: 176
which languages have a way to wait on 2 or more things at the same time?


which languages have a way to wait on 2 or more things at the same time. easily, without doing programming tricks? suppose you want to wait on either event X or event Y, whichever happens next, waking up immediately when it happens. how would this be best coded in the languages that can do this. anything must be waitable, including I/O and timers and messages from other tasks (threads or processes).
 
Old 03-07-2022, 02:43 AM   #2
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 22,101

Rep: Reputation: 7365Reputation: 7365Reputation: 7365Reputation: 7365Reputation: 7365Reputation: 7365Reputation: 7365Reputation: 7365Reputation: 7365Reputation: 7365Reputation: 7365
for example bash (shells) has the command wait. Is this what you are looking for?
 
Old 03-07-2022, 05:21 AM   #3
GazL
LQ Veteran
 
Registered: May 2008
Posts: 6,922

Rep: Reputation: 5040Reputation: 5040Reputation: 5040Reputation: 5040Reputation: 5040Reputation: 5040Reputation: 5040Reputation: 5040Reputation: 5040Reputation: 5040Reputation: 5040
Quote:
which languages have a way to wait on 2 or more things at the same time.
Pretty much, all of them.

Quote:
... without doing programming tricks?
If by that you mean without writing code to handle it, pretty much non of them.

Typically you write an event loop and handle whatever occurs. Or, you use a ready made async event framework such as libevent.
 
1 members found this post helpful.
Old 03-07-2022, 06:52 AM   #4
boughtonp
Senior Member
 
Registered: Feb 2007
Location: UK
Distribution: Debian
Posts: 3,643

Rep: Reputation: 2561Reputation: 2561Reputation: 2561Reputation: 2561Reputation: 2561Reputation: 2561Reputation: 2561Reputation: 2561Reputation: 2561Reputation: 2561Reputation: 2561
Quote:
Originally Posted by GazL View Post
Typically you write an event loop and handle whatever occurs. Or, you use a ready made async event framework such as libevent.
Or use a language that comes event-driven out-of-the-box.

 
Old 03-07-2022, 07:41 AM   #5
rtmistler
Moderator
 
Registered: Mar 2011
Location: USA
Distribution: MINT Debian, Angstrom, SUSE, Ubuntu, Debian
Posts: 9,885
Blog Entries: 13

Rep: Reputation: 4931Reputation: 4931Reputation: 4931Reputation: 4931Reputation: 4931Reputation: 4931Reputation: 4931Reputation: 4931Reputation: 4931Reputation: 4931Reputation: 4931
Might be helpful to understand the purpose for your question.

I agree many, if not all languages have the capability to block on several conditions simultaneously.
 
Old 03-07-2022, 09:00 AM   #6
EdGr
Senior Member
 
Registered: Dec 2010
Location: California, USA
Distribution: I run my own OS
Posts: 1,005

Rep: Reputation: 476Reputation: 476Reputation: 476Reputation: 476Reputation: 476
The pthread library has a general mechanism: the application program does both the wait and the wake-up on different threads. The conditions can be anything.
Ed
 
1 members found this post helpful.
Old 03-07-2022, 09:38 AM   #7
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,886
Blog Entries: 1

Rep: Reputation: 1872Reputation: 1872Reputation: 1872Reputation: 1872Reputation: 1872Reputation: 1872Reputation: 1872Reputation: 1872Reputation: 1872Reputation: 1872Reputation: 1872
Well, yes, multiple threads. There is select/poll for IO, wait(2) for child-processes, XNextEvent for XWindow, semop/sema_wait for semaphores, also pthread_mutex_lock etc
 
Old 03-07-2022, 03:20 PM   #8
dugan
LQ Guru
 
Registered: Nov 2003
Location: Canada
Distribution: distro hopper
Posts: 11,263

Rep: Reputation: 5339Reputation: 5339Reputation: 5339Reputation: 5339Reputation: 5339Reputation: 5339Reputation: 5339Reputation: 5339Reputation: 5339Reputation: 5339Reputation: 5339
All of them.

Although Node and Go are actually designed around this.
 
Old 03-07-2022, 06:13 PM   #9
dugan
LQ Guru
 
Registered: Nov 2003
Location: Canada
Distribution: distro hopper
Posts: 11,263

Rep: Reputation: 5339Reputation: 5339Reputation: 5339Reputation: 5339Reputation: 5339Reputation: 5339Reputation: 5339Reputation: 5339Reputation: 5339Reputation: 5339Reputation: 5339
Do be aware that the architecture you seem to be talking about, where you do a blocking read, and the kernel puts the process to sleep until data comes in, would not work if you're reading more than one event source. Instead, you'd use the following:

https://en.wikipedia.org/wiki/Event_loop

Many of the replies in this thread have made suggestions as to how to achieve this.
 
Old 03-07-2022, 08:03 PM   #10
Skaperen
Senior Member
 
Registered: May 2009
Location: center of singularity
Distribution: Xubuntu, Ubuntu, Slackware, Amazon Linux, OpenBSD, LFS (on Sparc_32 and i386)
Posts: 2,693

Original Poster
Blog Entries: 31

Rep: Reputation: 176Reputation: 176
Quote:
Originally Posted by EdGr View Post
The pthread library has a general mechanism: the application program does both the wait and the wake-up on different threads. The conditions can be anything.
Ed
so, i could have thread 1 just wait for event 1, and thread 2 just wait for event 2. then there is an existing thread call to wait on 2 or more threads? or would i have to have threads 1 and 2 send a message to thread 3?

i hope the waiting syscalls don't block the whole process.

right now my specific waits are a repeating one second tick that does not accumulate offset errors, and the arrival of an ICMP ping reply. this is to detect a lost WIFI route. disconnecting and reconnecting the WIFI fixes it. i want detect the situation as fast as possible. so every second, if the past N seconds has had no received ICMP ping reply, run the reconnect code.

thinking about this in a supposedly portable language i wondered if any language directly supported this in simple uniform code.
 
Old 03-07-2022, 11:40 PM   #11
Michael Uplawski
Senior Member
 
Registered: Dec 2015
Posts: 1,624
Blog Entries: 40

Rep: Reputation: Disabled
Multitasking and Multithreading are not things that you learn on one day and once for all occasions.
While I want to throw in polling as another – often just as useful, sometimes impossible, usually simpler – technique, here is all you must know about Threads in 'C':
https://www.gruble.de/search?q=DAvid...language=en-US

I am not sure, but remember that the importance of Threads is about the same with Linux as that of Tasks with MS® Windows® or vice versa. This may become important, sometimes.., or not.

I love Threads and use them more often nowadays, but the very first time I thought it will be the right thing to do, my boss let me the freedom to punch my head against a wall (did that often) a few times, then told me to consider Polling, instead. He was right, of course.
 
Old 03-07-2022, 11:47 PM   #12
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 22,101

Rep: Reputation: 7365Reputation: 7365Reputation: 7365Reputation: 7365Reputation: 7365Reputation: 7365Reputation: 7365Reputation: 7365Reputation: 7365Reputation: 7365Reputation: 7365
Quote:
Originally Posted by Skaperen View Post

i hope the waiting syscalls don't block the whole process.
Theoretically you can implement whatever you want. Either blocking or non-blocking calls (with other words: sync or async calls) ...

Quote:
Originally Posted by Skaperen View Post
right now my specific waits are a repeating one second tick that does not accumulate offset errors, and the arrival of an ICMP ping reply. this is to detect a lost WIFI route. disconnecting and reconnecting the WIFI fixes it. i want detect the situation as fast as possible. so every second, if the past N seconds has had no received ICMP ping reply, run the reconnect code.
You don't need multithreading/multitasking for this:
ping continuously (in every second?) | grep pattern (will detect error condition and stop) && reconnect wifi && restart script/loop.
 
Old 03-08-2022, 12:27 AM   #13
Michael Uplawski
Senior Member
 
Registered: Dec 2015
Posts: 1,624
Blog Entries: 40

Rep: Reputation: Disabled
Quote:
Originally Posted by pan64 View Post
You don't need multithreading/multitasking for this:
ping continuously (in every second?) | grep pattern (will detect error condition and stop) && reconnect wifi && restart script/loop.
That is what I called Polling, above. I do not know if it is really a valid convention, but we used to have discussions about Polling versus Multi-T* to achieve the same.
 
Old 03-08-2022, 01:01 AM   #14
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 22,101

Rep: Reputation: 7365Reputation: 7365Reputation: 7365Reputation: 7365Reputation: 7365Reputation: 7365Reputation: 7365Reputation: 7365Reputation: 7365Reputation: 7365Reputation: 7365
Quote:
Originally Posted by Michael Uplawski View Post
That is what I called Polling, above. I do not know if it is really a valid convention, but we used to have discussions about Polling versus Multi-T* to achieve the same.
Yes, you are right, it is called polling. https://en.wikipedia.org/wiki/Polling_(computer_science)
 
Old 03-08-2022, 10:55 AM   #15
EdGr
Senior Member
 
Registered: Dec 2010
Location: California, USA
Distribution: I run my own OS
Posts: 1,005

Rep: Reputation: 476Reputation: 476Reputation: 476Reputation: 476Reputation: 476
Quote:
Originally Posted by Skaperen View Post
so, i could have thread 1 just wait for event 1, and thread 2 just wait for event 2. then there is an existing thread call to wait on 2 or more threads? or would i have to have threads 1 and 2 send a message to thread 3?
The latter. See the documentation for pthread_cond_wait () and pthread_cond_broadcast ().

Quote:
Originally Posted by Skaperen View Post
i hope the waiting syscalls don't block the whole process.
Waiting syscalls block the thread. Other threads can still run.

Quote:
Originally Posted by Skaperen View Post
right now my specific waits are a repeating one second tick that does not accumulate offset errors, and the arrival of an ICMP ping reply. this is to detect a lost WIFI route. disconnecting and reconnecting the WIFI fixes it. i want detect the situation as fast as possible. so every second, if the past N seconds has had no received ICMP ping reply, run the reconnect code.

thinking about this in a supposedly portable language i wondered if any language directly supported this in simple uniform code.
Now that you have described the problem, I think that multi-threading is a more powerful solution than you need. You should be able to get by with an event queue or polling as others have suggested.
Ed
 
  


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
Do we have to use "yum update" command every week or we have to wait more? yeknafar Linux - Newbie 8 07-16-2018 09:07 AM
how to understand user time, sys time, wait time, idle time of CPU guixingyi Linux - Server 1 08-24-2010 10:10 AM
prevent users to run the same script at the same time, on the same machine pvpnguyen Programming 2 09-05-2007 08:52 PM
I download then wait and download and wait... jsheffie SUSE / openSUSE 1 11-04-2005 04:43 PM
languages of linux: which languages can be choosen in suse and red-hat distributions? Klaus Schnorr Linux - Software 3 09-10-2005 02:19 AM

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

All times are GMT -5. The time now is 03:20 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