Can't close C++ Console Application without R6010? - c++

I decided to take the plunge in C++ after using Python for a couple of years now. Right now, I am trying to make a program that flashes LEDs on my keyboard every 60 minutes to remind me to stretch using their keyboard SDK. The flashing and timing work, but the problem is closing the program when you don't want or need it open.
I can exit the program before I first call the alarm() function. After I call the that alarm function, I get the Windows pop saying that I can either Abort, ignore, or retry with the code R6010.
I currently have it setup to try to catch a Ctrl-C event and end the while loop, but that ends with the same error anyways.
#include "stdafx.h"
#include <iostream>
#include "Windows.h"
#include "SDKDLL.h"
#include <string>
#include <chrono>
#include <thread>
#include <signal.h>
#include <stdlib.h>
#include <stdio.h>
#include <ctime>
using namespace std;
using namespace std::this_thread;
using namespace std::chrono;
BYTE r = 0;
BYTE g = 0;
BYTE b = 0;
bool exiter = false;//Exit variable
void my_handler(int s){// Tells me when CTRL C was pressed
printf("Caught signal %d\n", s);
::exiter = true;
}
void alarm(){
//Basically turns on and off LEDs 5 times
for (int x = 1; x < 5; x++){
Sleep(500);
r = 255;
g = 255;
b = 255;
SetFullLedColor(r, g, b);
Sleep(500);
r = 0;
g = 0;
b = 0;
SetFullLedColor(r, g, b);
}
}
int _tmain(int argc, _TCHAR* argv[])
{
signal(SIGINT, my_handler);//Trying to find a way for the program to exit nicely, this way uses CTRL C
SetControlDevice(DEV_MKeys_M_White);
while (::exiter == false){
sleep_until(system_clock::now() + seconds(4));//waits X seconds to do the sequence again
EnableLedControl(true);//Allows for control of LED
alarm();//Turns off and on the LEDS
EnableLedControl(false);//Disables the LEDs
}
return 0;
}
I have been researching this error for a couple hours with no solution working. Any help would be greatly appreciated!

Related

C + + cannot find the thread it created on ubuntu 18

I write a simple c++ code. In my code, I create two threads, then I name the two threads TCPCall30003Proc and TCPCall30004Proc, but I can not find them using top command with option -H. My os is ubuntu 18.
#include <pthread.h>
#include <thread>
#include <stdio.h>
#include <time.h>
#include <iostream>
#include <stdlib.h>
#include <chrono>
#include <unistd.h>
void f1(int num)
{
printf("1\n");
while(1)
{
sleep(1);
}
}
void f2(int num)
{
printf("2\n");
while(1)
{
sleep(1);
}
}
int main(int argc, char **argv)
{
std::thread thd_1 = std::thread(f1, 1);
std::thread thd_2 = std::thread(f2, 1);
pthread_setname_np(thd_1.native_handle(), "TCPCall30003Proc");
pthread_setname_np(thd_2.native_handle(), "TCPCall30004Proc");
while(1)
{
sleep(1);
}
return 0;
}
From the pthread_setname_np manual page:
The thread name is a meaningful C language string, whose length is restricted to 16 characters, including the terminating null byte ('\0').
[Emphasis mine]
You names are 17 characters, including the null-terminator.
If you check what pthread_setname_np returns it should return -1 and with errno set to ERANGE.
You need to shorten your names.

the relationship between thread running time, cpu context switching and performance

I did an experiment to simulate what happened in our server code, I started 1024 threads and every thread execute a system call, this takes about 2.8s to finish execution on my machine. Then I add usleep(1000000) in function of every thread, the execution time increase to 16s and time will decrease to 8s when I run same program at second time. I guess this maybe caused by cpu cache and the cpu context switch, but I'm not quite sure how to explain it.
Besides, what is the best practice to avoid this happening (increasing the running time for every threads a little lead to the decreasing for whole program performance).
I attached the test code here, thanks a lot for your help.
//largetest.cc
#include "local.h"
#include <time.h>
#include <thread>
#include <string>
#include "unistd.h"
using namespace std;
#define BILLION 1000000000L
int main()
{
struct timespec start, end;
double diff;
clock_gettime(CLOCK_REALTIME, &start);
int i = 0;
int reqNum = 1024;
for (i = 0; i < reqNum; i++)
{
string command = string("echo abc");
thread{localTaskStart, command}.detach();
}
while (1)
{
if ((localFinishNum) == reqNum)
{
break;
}
else
{
usleep(1000000);
}
printf("curr num %d\n", localFinishNum);
}
clock_gettime(CLOCK_REALTIME, &end); /* mark the end time */
diff = (end.tv_sec - start.tv_sec) * 1.0 + (end.tv_nsec - start.tv_nsec) * 1.0 / BILLION;
printf("debug for running time = (%lf) second\n", diff);
return 0;
}
//local.cc
#include "time.h"
#include "stdlib.h"
#include "stdio.h"
#include "local.h"
#include "unistd.h"
#include <string>
#include <mutex>
using namespace std;
mutex testNotifiedNumMtx;
int localFinishNum = 0;
int localTaskStart(string batchPath)
{
char command[200];
sprintf(command, "%s", batchPath.data());
usleep(1000000);
system(command);
testNotifiedNumMtx.lock();
localFinishNum++;
testNotifiedNumMtx.unlock();
return 0;
}
//local.h
#ifndef local_h
#define local_h
#include <string>
using namespace std;
int localTaskStart( string batchPath);
extern int localFinishNum;
#endif
The read of localFinishNum should also be protected by mutex, otherwise the results are unpredictable based on where (i.e. on which cores) threads get scheduled, when and how the cache gets invalidated, etc.
In fact, the program might not even terminate if you compile it in optimized mode if the compiler decides to put localFinishNum in the register (instead of always loading it from memory).

cv has no member CAP_PROP_POS_FRAMES

I'm trying to run a bit of code to add trackbars onto some video, it's from the Learning OpenCV Second Edition book, but I can't compile my code and gives the error "namespace cv has no member CAP_PROP_POS_FRAMES"
Here's the first bit of the code
#include <opencv2\highgui\highgui.hpp>
#include <opencv2\imgproc\imgproc.hpp>
#include <iostream>
#include <fstream>
using namespace std;
int g_slider_position = 0;
int g_run = 1, g_dontset = 0; //start out in a single step mode
cv::VideoCapture g_cap;
void onTrackbarSlide(int pos, void *) {
g_cap.set(cv::CAP_PROP_POS_FRAMES, pos);
if(!g_dontset)
g_run = 1;
g_dontset = 0;
}
It's CV_CAP_PROP_POS_FRAMES (note the S) and it should be brought in by highgui.hpp. It's an unnamed enum in the global namespace.

C++ Background problems

I have a c++ program that generates random files filled with gibberish, but for it to work it needs to run in the background. The method I am using generates a null window. I have made other programs using this background method, but it doesn't work in this program:
#include <iostream>
#include <string>
#include <time.h>
#include <stdlib.h>
#include <fstream>
#include <windows.h>
using namespace std;
string random(int len)
{
string a = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
string r;
srand(time(NULL));
for(int i = 0; i < len; i++) r.push_back(a.at(size_t(rand() % 62)));
return r;
}
int main(){
restart:
/*This is the background code*/
HWND window;
AllocConsole();
window - FindWindowA("ConsoleWindowClass", NULL);
ShowWindow(window, 0);
std::string file=random(1);
std::ofstream o(file.c_str());
o << random(999) << std::endl;
goto restart;
return 0;
}
I am using the dev C++ compiler
I just realized my problem, the goto statement needed to not include the null window rendering part, so the window wasn't re-rendered and de-rendered every time. Also there was a - that needed to be an =.

Extreme Confusion on Forks and Execlp

this is my first post.
I have a task to use a fork to create multiple processes and then use execlp to run another program to add 2 numbers.
The problem I am having is we are supposed to use the exit() call in the execlp to return the small integer. This is a bad way to communicate but it's what we are supposed to do for the sake of this program.
Here is my "coordinator" program
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <stdlib.h>
using namespace std;
int main (int argc,char* argv[])
{
const int size = argc-1;
int sizeArray = 0;
int numofProc =0;
int arrayofNum[size];
int status;
int value;
for(int y=1; y<argc; y++)
{
arrayofNum[y-1] = atoi(argv[y]);
sizeArray++;
}
if(sizeArray % 2 !=0)
{
arrayofNum[sizeArray] = 0;
sizeArray++;
}
numofProc = sizeArray/2;
//declaration of a process id variable
pid_t pid;
//fork a child process is assigned
//to the process id
pid=fork();
//code to show that the fork failed
//if the process id is less than 0
if(pid<0)
{
cout<<"Fork Failed";// error occurred
exit(-1); //exit
}
//code that runs if the process id equals 0
//(a successful for was assigned
else
if(pid==0)
{
//this statement creates a specified child process
execlp("./worker", "worker", arrayofNum[0], arrayofNum[1]);//child process
}
//code that exits only once a child
//process has been completed
else
{
waitpid(pid, &status, 0);
cout<<status;
}
//main
}
and here is the execlp process
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>
using namespace std;
int main(int argc, char* argv[])
{
int arrayofNum[argc-1];
arrayofNum[0] = atoi(argv[1]);
arrayofNum[1] = atoi(argv[2]);
int sum = arrayofNum[0] + arrayofNum[1];
exit(sum);
}
My problem is NO MATTER WHAT I DO, the status is ALWAYS printing a 0, I do not know how to retrieve the sum that is returned from the worker process.
My professor told me ""Only the higher byte of status will have the value returned by the worker. you need to extract it. It can be done by many ways. ""
In a nut shell, my question is, How do I retrieve that "sum" that is being sent from my worker process.
Please, I am so confused and have been up for 2 nights wondering about this
Thanks,
John
First up, you need to pass strings to your program, but you say:
execlp("./worker", "worker", arrayofNum[0], arrayofNum[1]);
And arrayofNum is an array of integers. Also, with execlp you also need to pass a NULL as the last argument. The standard says:
The arguments represented by arg0,... are pointers to null-terminated
character strings. These strings shall constitute the argument list
available to the new process image. The list is terminated by a null
pointer.
Second, after you call to waitpid(2) you need to do:
if (WIFEXITED(status))
code = WEXITSTATUS(status);