ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
Hi.
I´m pretty new to programming in C++ and I´m using pthreads. I´m cross compiling my code for OpenWRT but for some reason I get segmentation fault when I run the program on my board but it runs fine on my PC. I suspect that the error occurs in the linking stage of the compilation because I tried a small C program and that worked fine. Also if I change the name of the file to .cpp and compile it with g++ it also works.
The main:
Code:
#define INPUT_PORT "20"
#define POWER_PORT "21"
#define OUTPUTPORT 3
#define HIGHVALUE 1
#define LOWVALUE 0
#define EMERGENCY_FOLDER "emergency"
#define NORMAL_FOLDER "normal"
#include <iostream>
#include <string>
#include <time.h>
#include <unistd.h>
#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
#include "sender.h"
#include "Camera.h"
#include "Ports.h"
#include "UDPServer.h"
using namespace std;
//initializes the ports
//Ports pow(POWER_PORT, "out", 0);
//Ports button(INPUT_PORT, "in", 0);
//Tread Functions
Camera* camera;
void *CPUSimFunction(void *);
void *securityThread(void *);
int main() {
camera = new Camera("192.168.1.67", "admin", "admin", "image.jpg", "/cameratest");
cout << "at least I´m running here" << endl;
pthread_t doorThread;
pthread_create(&doorThread, NULL, CPUSimFunction, NULL);
while (1) {
camera->savePicture(NORMAL_FOLDER);
sleep(20);
}
}
void *CPUSimFunction(void *dummyPtr) {
int x;
while (1) {
cout << "Press 1 to open and 0 to close: ";
cin >> x;
if (x == 1) {
pthread_t emergencyCamThread;
pthread_create(&emergencyCamThread, NULL, securityThread, NULL);
cout << "door is open" << endl;
} else if (x == 0) {
cout << "door is closed" << endl;
} else {
cout << "Choose 1 or 0";
}
}
}
void *securityThread(void *dummyPtr) {
for (int i = 0; i < 10; i++) {
cout << "saving emergency picture" << endl;
camera->savePicture(EMERGENCY_FOLDER);
usleep(1000000);
}
}
The output of the eclipse console when compiled:
Code:
**** Build of configuration Release for project ButtonProgram ****
make all
Building file: ../src/ButtonProgram.cpp
Invoking: GCC C++ Compiler
mipsel-linux-g++ -O3 -Wall -c -fmessage-length=0 -MMD -MP -MF"src/ButtonProgram.d" -MT"src/ButtonProgram.d" -o"src/ButtonProgram.o" "../src/ButtonProgram.cpp" -ldl -pthread
../src/ButtonProgram.cpp: In function 'void* CPUSimFunction(void*)':
../src/ButtonProgram.cpp:73: warning: no return statement in function returning non-void
../src/ButtonProgram.cpp: In function 'void* securityThread(void*)':
../src/ButtonProgram.cpp:81: warning: no return statement in function returning non-void
../src/ButtonProgram.cpp:81: warning: control reaches end of non-void function
mipsel-linux-g++: -ldl: linker input file unused because linking not done
Finished building: ../src/ButtonProgram.cpp
Building file: ../src/Camera.cpp
Invoking: GCC C++ Compiler
mipsel-linux-g++ -O3 -Wall -c -fmessage-length=0 -MMD -MP -MF"src/Camera.d" -MT"src/Camera.d" -o"src/Camera.o" "../src/Camera.cpp" -ldl -pthread
../src/Camera.cpp: In member function 'void Camera::savePicture(std::string) const':
../src/Camera.cpp:53: warning: unused variable 'mkdirOut'
mipsel-linux-g++: -ldl: linker input file unused because linking not done
Finished building: ../src/Camera.cpp
Building file: ../src/Ports.cpp
Invoking: GCC C++ Compiler
mipsel-linux-g++ -O3 -Wall -c -fmessage-length=0 -MMD -MP -MF"src/Ports.d" -MT"src/Ports.d" -o"src/Ports.o" "../src/Ports.cpp" -ldl -pthread
mipsel-linux-g++: -ldl: linker input file unused because linking not done
Finished building: ../src/Ports.cpp
Building file: ../src/UDPServer.cpp
Invoking: GCC C++ Compiler
mipsel-linux-g++ -O3 -Wall -c -fmessage-length=0 -MMD -MP -MF"src/UDPServer.d" -MT"src/UDPServer.d" -o"src/UDPServer.o" "../src/UDPServer.cpp" -ldl -pthread
mipsel-linux-g++: -ldl: linker input file unused because linking not done
Finished building: ../src/UDPServer.cpp
Building file: ../src/sender.cpp
Invoking: GCC C++ Compiler
mipsel-linux-g++ -O3 -Wall -c -fmessage-length=0 -MMD -MP -MF"src/sender.d" -MT"src/sender.d" -o"src/sender.o" "../src/sender.cpp" -ldl -pthread
mipsel-linux-g++: -ldl: linker input file unused because linking not done
Finished building: ../src/sender.cpp
Building target: ButtonProgram
Invoking: GCC C++ Linker
mipsel-linux-g++ -o"ButtonProgram" ./src/ButtonProgram.o ./src/Camera.o ./src/Ports.o ./src/UDPServer.o ./src/sender.o -static -pthread
Finished building target: ButtonProgram
Try returning NULL from void *securityThread(void *dummyPtr) and see if that helps. Always pay attention to the warnings from your compiler.
Code:
../src/ButtonProgram.cpp: In function 'void* securityThread(void*)':
../src/ButtonProgram.cpp:81: warning: no return statement in function returning non-void
../src/ButtonProgram.cpp:81: warning: control reaches end of non-void function
Hi
That didn´t work. I tried removing all thread code and compiling with the -lpthread and I still get segfault. Also I never get to the main since the "At least I´m running here" string doesn´t print. If I remove the -lpthread flag from the compiler the code (without the treads) runs on the board.
Looking at the Camera constructor seems to be a good plan. Since the first cout isn't printing, that is the place to start.
You also should look at your variable scope rules. You are asking for trouble with this statement: pthread_t emergencyCamThread;
being placed inside an if statement which is itself inside a while statement.
The above code might seem strange but the fact is I have been testing alot of different things so there might be things there that shouldn´t be used the way I´m using them. Also I have tried moving the camera into the main but with no results.
About the pthread_join()... Well I don´t know yet if I will need it. I´m using threads for the first time so im just experimenting and I don´t have a final picture of this in my head.
Here is another sample of a code that will run on my PC as expected but will only run on the board when compiled as a C program.
I think in/with C++ one should use 'boost' threads - it is also cross-platform. 'boost' developers have already taken care of system-specific things, so it should just compile. I also think that on Linux 'boost' threads in the end are implemented through 'pthreads'.
The above code might seem strange but the fact is I have been testing alot of different things so there might be things there that shouldn´t be used the way I´m using them. Also I have tried moving the camera into the main but with no results.
About the pthread_join()... Well I don´t know yet if I will need it. I´m using threads for the first time so im just experimenting and I don´t have a final picture of this in my head.
Here is another sample of a code that will run on my PC as expected but will only run on the board when compiled as a C program.
So you can get a C program to work, but not a C++ program? Maybe certain runtime libraries are missing from the board?
Here's a basic C++ thread implementation:
Code:
#include <pthread.h>
#include <iostream>
class Thread
{
public:
Thread() {}
void start() {
pthread_start(&m_tid, 0, helper, this);
}
void waitTillFinished() {
pthread_join(m_tid, 0);
}
private:
void run() {
std::cout << "thread is running." << std::endl; // not thread-safe!
}
static void* helper(void* arg) {
Thread* t = static_cast<Thread*>(arg);
t->run();
pthread_exit(0);
return 0;
}
pthread_t m_tid;
};
int main()
{
Thread thread;
thread.start();
thread.waitTillFinished(); // this is not typically done this way
}
Can you please build this app to test on your board and report the results? Here's the basics on how to build it with the standard g++:
Code:
g++ ThreadTest.cpp -lpthread -o ThreadTest
Note your board probably requires that you use a special cross compiler, perhaps not the same for building apps to run on your PC.
Last edited by dwhitney67; 05-16-2009 at 03:50 PM.
Try without the -static. This linker option is used to include within the executable the library instructions; without it, the shared-object (.so) library is accessed during runtime.
Unless you have a libpthread.a that has been specifically built for your target (mipsel-linux?), then you should not be using the -static option.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.