The uv_timer_stop() does not appear to stop and reset the timer. How can I stop and reset the timer correctly?
#include <iostream>
#include <thread>
#include <unistd.h> // usleep
#include <uv.h>
uv_loop_t *loop;
uv_timer_t timer_req;
float timerSeconds = 2.0f;
float sleepSeconds = 1.0f;
void run() {
while (true) uv_run(loop, UV_RUN_DEFAULT);
}
void timerCallback(uv_timer_t* handle) {
std::cout << "Callback" << std::endl;
}
int main() {
loop = uv_default_loop();
std::thread t(run);
uv_timer_init(loop, &timer_req);
while (true) {
std::cout << "sleep" << std::endl;
uv_timer_start(&timer_req, timerCallback, 1000*timerSeconds, 0);
usleep(1000*1000*sleepSeconds);
uv_timer_stop(&timer_req);
};
return 0;
}
output on Debian Linux with libuv1-dev 1.24.1:
$ g++ main.cc -o main `pkg-config --libs libuv`
$ ./main
sleep
sleep
sleep
sleep
sleep
Callback
sleep
^C
Expected output: The timer is set to call the callback after two seconds (float timerSeconds = 2.0f). But we stop (and restart) the timer after every second (float sleepSeconds=1.0f), so the timer should never run long enough for the callback to be executed and the output should show only the 'sleep' messages, instead of the 'Callback' message. That is, it should be:
$ g++ main.cc -o main `pkg-config --libs libuv`
$ ./main
sleep
[...]
I've tried:
stopping the timer inside the callback
reinitialising the timer every time: using uv_timer_init() inside the while loop, before calling uv_timer_start()
Why I am doing this: I'm writing a program to detect the long-press of a button (a short-press of a button triggers a different action). There is a callback to detect a button-press, and a button-depress. The button-press should start the timer, and button-depress should stop the timer. If the button has been pressed down long enough (two seconds), the timer callback executes, and if we reach this callback, then we know that the button has been long-pressed.
Related
If I have a C++ code with an infinite loop inside i want a command that will kill the execution after certain time.
so i came up with something like this-
g++ -std=c++20 -DLOCAL_PROJECT solution.cpp -o solution.exe & solution.exe & timeout /t 0 & taskkill /im solution.exe /f
But the problem with this was that it would first execute the program so due to the infinite loop it won't even come to timeout and taskkill part.
Does anybody have any solution to it or other alternatives instead of timeout?
I am using windows 10 and my compiler is gnu 11.2.0
Also in case there is No TLE i don't want taskkill to show this error
ERROR: The process "solution.exe" not found.
Your main loop could exit after a certain time limit, if you're confident it is called regularly enough.
#include <chrono>
using namespace std::chrono_literals;
using Clock = std::chrono::system_clock;
int main()
{
auto timeLimit = Clock::now() + 1s;
while (Clock::now() < timeLimit) {
//...
}
}
Alternatively you could launch a thread in your main throwing an exception after a certain delay:
#include <chrono>
#include <thread>
using namespace std::chrono_literals;
struct TimeOutException {};
int main()
{
std::thread([]{
std::this_thread::sleep_for(1s);
std::cerr << "TLE" << std::endl;
throw TimeOutException{};
}).detach();
//...
}
terminate called after throwing an instance of 'TimeOutException'
I have tried to write a program that run in ubuntu terminal .Program will open a new gnome terminal and run command in that new terminal to open new abcd.txt using vim.And then when i Ctrl+C in the first terminal which run the program ,new gnome terminal will shut vim down and have an announcement in the first terminal
I have tried system("`gnome-terminal`<< vim abcd.txt");
and this system("vim abcd.txt>>`gnome-terminal`");
but the new one terminal cannot recieve command
My full code
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/wait.h>
#include <pthread.h>
int loop=1;
void DEF()
{
system("kill -9 pidof vim");
loop=0;
}
void *subthreads(void *threadid)
{
loop=1;
long tid;
tid=(long)threadid;
system("`gnome-terminal`<< vim abcd.txt");
signal(SIGINT,DEF);
while(loop){}
pthread_exit(NULL);
}
void main()
{
int loop=1;
pthread_t threads;
int check;
long tID;
check= pthread_create(&threads,NULL,&subthreads,(void*)tID);
while(loop){}
printf("Ctrl+C is pressed!\n");
}
Not sure what you are trying to achieve in the end. But here are a few ideas, starting from your code:
The terminal command (in system()) should be something like Mark Setchell pointed out, like for example system("gnome-terminal -e vim file.txt");
The system() command is blocking further execution of your code, so the call to signal() is not happening until you terminate the system() call.
pidof is not working on my Linux system. I would use pkill <program>. Still, that would kill all running instances of , for example vim or your terminal.
You are declaring the variable loop in the global scope first and then redeclaring it in main(). If you really want to use it as a global variable, it should just be loop=1 in main().
You are not using the variable tid for anything.
Here is an improved version of your program, with additional printf calls to explain to the user what is happening. I also used xterm and nano because I don't have gnome-terminal, and I didn't want to interfere with my running instance of vim. But it still is maybe not exactly what you are trying to do. The main problem is that system("xterm -e sh &") is blocking and when you press Ctrl-C, that system call will terminate xterm so that the def() function will do nothing when it is called later.
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/wait.h>
#include <pthread.h>
int loop = 1;
void def()
{
printf("In def\n");
system("pkill xterm");
loop=0;
}
void *subthreads(void *threadid)
{
printf("Starting subthread\n");
loop = 1;
long tid;
tid = (long)threadid;
signal(SIGINT, def);
system("xterm -e sh -c nano &"); // Note: xterm will still exit when you press Ctrl-C
printf("Terminal exited in subthread\n");
while (loop);
printf("Exited loop in subthread\n");
pthread_exit(NULL);
}
void main()
{
pthread_t threads;
int check;
long tID;
check = pthread_create(&threads, NULL, &subthreads, (void*)tID);
printf("In main after thread creation\n");
while (loop);
printf("Ctrl+C is pressed!\n");
}
Another option is to use fork() instead of pthread to split into a separate process. (Note that processes are like separate applications while threads are processor threads in the same application.)
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/wait.h>
void def()
{
system("pkill nano");
printf("def(): Killed nano\n");
}
int subprocess()
{
signal(SIGINT, def);
pid_t parent_id = getpid(); // Get process ID of main process
fork(); // Fork into two identical copies of the running app.
if (getpid() != parent_id) { // The part in the if block is only done in the second process!
system("xterm -e sh -c nano &");
printf("subprocess(): system call ended in forked process\n");
exit(0);
}
}
int main()
{
subprocess();
printf("Entering while loop in main process\n");
while (1);
printf("Exited main thread\n");
}
The one flaw with this version is the same as the previous one: when Ctrl-C is pressed, xterm/nano is killed and def() will subsequently do nothing except catch any Ctrl-C done afterwards.
If you explain further what your final goal is, maybe I can give some suggestions.
Like, why do you want to start vim in a terminal from a C application and then kill vim? Do you want to kill the whole terminal or only vim?
#include <unistd.h>
#include <stdio.h>
#include <cstring>
#include <thread>
void test_cpu() {
printf("thread: test_cpu start\n");
int total = 0;
while (1) {
++total;
}
}
void test_mem() {
printf("thread: test_mem start\n");
int step = 20;
int size = 10 * 1024 * 1024; // 10Mb
for (int i = 0; i < step; ++i) {
char* tmp = new char[size];
memset(tmp, i, size);
sleep(1);
}
printf("thread: test_mem done\n");
}
int main(int argc, char** argv) {
std::thread t1(test_cpu);
std::thread t2(test_mem);
t1.join();
t2.join();
return 0;
}
Compile it with g++ -o test test.cc --std=c++11 -lpthread
I run the program in Linux, and run top to monitor it.
I expect to see ONE process however I saw THREE.
It looks like std::thread is creating threads, why do I end up with getting processes?
Linux does not implement threads. It only has Light Weight Processes (LWP) while pthread library wraps them to provide POSIX-compatible thread interface. The main LWP creates its own address space while each subsequent thread LWP shares address space with main LWP.
Many utils, such as HTOP (which seems to be on the screenshot) by default list LWP. In order to hide thread LWPs you can open Setup (F2) -> Display Options and check Hide kernel threads and Hide userland process threads options. There is also an option to highlight threads - Display threads in different color.
My application uses libhiredis with libev backend. I need to send Redis async commands and process the resulting Redis async callback. However, unlike the simple example from here I cannot use the default event loop. The following code approximates the example with a custom event loop. However, when compiled with only the redisLibevAttach() induced libev io watcher, the event loop thread terminates immediately. You can see this by running
g++ -g -std=c++11 -Wall -Wextra -Werror hiredis_ev.cpp -o hiredis_ev -lpthread -lhiredis -lev && gdb ./hiredis_ev
where GDB happily prints that a new thread is created and almost immediately terminates. This is further confirmed by running info thread in GDB which does not show my_ev_loop. However, if I change the code to add any other libev watcher, like a timer, then everything is good. You can see this by running
g++ -g -DTIMER -std=c++11 -Wall -Wextra -Werror hiredis_ev.cpp -o hiredis_ev -lpthread -lhiredis -lev && ./hiredis_ev
I should not need a dummy libev timer to keep the event loop running. What am I missing?
#include <iostream>
#include <thread>
#include <hiredis/hiredis.h>
#include <hiredis/async.h>
#include <hiredis/adapters/libev.h>
static struct ev_loop *loop = nullptr;
static void redis_async_cb(redisAsyncContext *, void *, void *)
{
std::cout << "Redis async callback" << std::endl;
fflush(nullptr);
}
#ifdef TIMER
static ev_timer timer_w;
static void ev_timer_cb(EV_P_ ev_timer *, int)
{
std::cout << "EV timer callback" << std::endl;
fflush(nullptr);
}
#endif
int main()
{
loop = ev_loop_new(EVFLAG_AUTO);
#ifdef TIMER
ev_timer_init(&timer_w, ev_timer_cb, 0, 0.1);
ev_timer_start(loop, &timer_w);
#endif
redisAsyncContext* async_context = redisAsyncConnect("localhost", 6379);
if (nullptr == async_context)
{
throw std::runtime_error("No redis async context");
}
redisLibevAttach(loop, async_context);
std::thread ev_thread(ev_run, loop, 0);
pthread_setname_np(ev_thread.native_handle(), "my_ev_loop");
ev_thread.detach();
// Give the event loop time to start
while (!ev_iteration(loop))
{
std::this_thread::sleep_for(std::chrono::milliseconds(1));
}
// Send a SUBSCRIBE message which should generate an async callback
if (REDIS_OK != redisAsyncCommand(async_context, redis_async_cb, nullptr, "SUBSCRIBE foo"))
{
throw std::runtime_error("Could not issue redis async command");
}
std::cout << "Waiting for async callback" << std::endl;
fflush(nullptr);
fflush(nullptr);
// Wait forever (use CTRL-C to terminate)
while (true)
{
std::this_thread::sleep_for(std::chrono::milliseconds(1));
}
return 0;
}
I found out that hiredis community has their own GitHub instance where I can ask questions. Since I had't yet received the answer here, I asked there. The answer can be found at https://github.com/redis/hiredis/issues/801#issuecomment-626400959
MinTTY does not seem to raise a signal to my mingw-w64 program when I hit CTRL+C. In CMD with the same identical program the signal is correctly raised. Why is this?
The program is compiled under msys2 mingw-w64 with g++ -static -static-libstdc++ -std=c++14 -Wall -Wextra -pedantic testan.cpp. In both cases, signal() does not return SIG_ERR so the handler seems to be correctly installed.
code:
#include <chrono>
#include <thread>
#include <iostream>
#include <csignal>
using namespace std;
void signalHandler( int x ) {
cout << "Interrupt: " << x << endl;
exit( 123 );
}
int main () {
if( signal(SIGINT, signalHandler) == SIG_ERR )
cout << "received SIG_ERR" << endl;
while( true ) {
cout << "waiting for CTRL+C" << endl;
this_thread::sleep_for( 1s );
}
return 0;
}
mintty output:
$ ./a.exe
waiting for CTRL+C
waiting for CTRL+C
waiting for CTRL+C
$
CMD output:
C:\Users\Xunie\Desktop\project>a.exe
waiting for CTRL+C
waiting for CTRL+C
Interrupt: 2
C:\Users\Xunie\Desktop\project>
MinTTY is a POSIX-oriented terminal emulator, it's using Cygwin/MSYS2 PTYs which don't interface well with native (non-Cygwin non-MSYS2) programs. This includes signals, detection of interactive input etc. MinTTY doesn't attempt to fix this, but Cygwin has recently (since v3.1.0) improved its support of this use case by using the new ConPTY API. As of May 2020, MSYS2 hasn't yet integrated these changes to its runtime, so you can't see the benefits there yet. In the meantime (and on older Windows versions), you can use the winpty wrapper, installable using pacman.