Blocking input

Discussion of chess software programming and technical issues.

Moderators: hgm, Rebel, chrisw

maelic
Posts: 12
Joined: Wed Jul 05, 2017 12:44 pm
Location: Czech Republic

Blocking input

Post by maelic »

Hello!

I've been building my own engine in python (I know c++ is better but I chose differently) and created two threads - one is worker and does the calculation, one watchdog and waits for input. I've done the same thing in C++ for comparison and they behave identically - I cannot pass info strings with my calculation results from worker until watchdog stops blocking the channel waiting for input. Once it stops (gets command from GUI...) then all the info from worker is printed at once.

I did my best to research non-blocking input functions in python but after I wrote the same thing in C++ and it also did not work, I ralized it might not be language problem but approach problem. Can you please hint me how this task is usually handeled? My solutions do not seem to work...

Thank you very much!
Best regards,
Miloš
D Sceviour
Posts: 570
Joined: Mon Jul 20, 2015 5:06 pm

Re: Blocking input

Post by D Sceviour »

maelic wrote: Fri Nov 02, 2018 4:56 pm Hello!

I've been building my own engine in python (I know c++ is better but I chose differently) and created two threads - one is worker and does the calculation, one watchdog and waits for input. I've done the same thing in C++ for comparison and they behave identically - I cannot pass info strings with my calculation results from worker until watchdog stops blocking the channel waiting for input. Once it stops (gets command from GUI...) then all the info from worker is printed at once.

I did my best to research non-blocking input functions in python but after I wrote the same thing in C++ and it also did not work, I ralized it might not be language problem but approach problem. Can you please hint me how this task is usually handeled? My solutions do not seem to work...

Thank you very much!
Your second watchdog thread might also be refered to as a polling thread. Do not try to pass strings through the polling thread. This would require a mutex. Instead, set a flag inside the polling thread to indicate some information is available. Then, print your information when the main thread is ready.
Aleks Peshkov
Posts: 892
Joined: Sun Nov 19, 2006 9:16 pm
Location: Russia

Re: Blocking input

Post by Aleks Peshkov »

You need to do std::flush() after printing each info string.
maelic
Posts: 12
Joined: Wed Jul 05, 2017 12:44 pm
Location: Czech Republic

Re: Blocking input

Post by maelic »

Aleks Peshkov wrote: Fri Nov 02, 2018 5:27 pm You need to do std::flush() after printing each info string.
Such an elegant solution!! I cannot believe I have not found any mentions of it during my 12 days research on non-blocking input...
Thank you very much :)
Best regards,
Miloš