LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
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 05-15-2009, 07:19 PM   #1
stulli
LQ Newbie
 
Registered: May 2009
Posts: 5

Rep: Reputation: 0
Compiling pthreads using g++


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
 
Old 05-15-2009, 09:35 PM   #2
rriggs
Member
 
Registered: Mar 2009
Location: Colorado, US
Distribution: Fedora 13, Fedora 14, RHEL6 Beta
Posts: 46

Rep: Reputation: 17
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
 
Old 05-16-2009, 07:44 AM   #3
stulli
LQ Newbie
 
Registered: May 2009
Posts: 5

Original Poster
Rep: Reputation: 0
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.
 
Old 05-16-2009, 11:27 AM   #4
dwhitney67
Senior Member
 
Registered: Jun 2006
Location: Maryland
Distribution: Kubuntu, Fedora, RHEL
Posts: 1,541

Rep: Reputation: 335Reputation: 335Reputation: 335Reputation: 335
Please post the code for Camera.cpp so that we can take a look at the constructor.

Edit: Btw, cout is not thread-safe. Also, do you not require the pthread_join()?

Last edited by dwhitney67; 05-16-2009 at 11:29 AM.
 
Old 05-16-2009, 12:01 PM   #5
jiml8
Senior Member
 
Registered: Sep 2003
Posts: 3,171

Rep: Reputation: 116Reputation: 116
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.
 
Old 05-16-2009, 01:51 PM   #6
stulli
LQ Newbie
 
Registered: May 2009
Posts: 5

Original Poster
Rep: Reputation: 0
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.
Code:
#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>

void *run(void *dummyPtr) {
	printf("I am a thread...\n");
	return NULL;
}

int main(int argc, char **argv) {
	printf("Main start...\n");
	pthread_t connector;
	pthread_create(&connector, NULL, run, NULL);
	pthread_join(connector, NULL);
	printf("Main end...\n");
	return 0;
}
 
Old 05-16-2009, 02:52 PM   #7
Sergei Steshenko
Senior Member
 
Registered: May 2005
Posts: 4,481

Rep: Reputation: 454Reputation: 454Reputation: 454Reputation: 454Reputation: 454
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'.

I am not a C++ guy, though.
 
Old 05-16-2009, 03:43 PM   #8
dwhitney67
Senior Member
 
Registered: Jun 2006
Location: Maryland
Distribution: Kubuntu, Fedora, RHEL
Posts: 1,541

Rep: Reputation: 335Reputation: 335Reputation: 335Reputation: 335
Quote:
Originally Posted by stulli View Post
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.
 
Old 05-16-2009, 06:16 PM   #9
stulli
LQ Newbie
 
Registered: May 2009
Posts: 5

Original Poster
Rep: Reputation: 0
Quote:
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
I get the same segmentation fault. I´m using a crosscompiler and the command I use is:
Code:
mipsel-linux-g++ ThreadTest.cpp -lpthread -o ThreadTest -static
About the libraries... If I understand correctly the -static flag compiles the libraries needed. The output file is at least alot bigger.
 
Old 05-16-2009, 09:13 PM   #10
dwhitney67
Senior Member
 
Registered: Jun 2006
Location: Maryland
Distribution: Kubuntu, Fedora, RHEL
Posts: 1,541

Rep: Reputation: 335Reputation: 335Reputation: 335Reputation: 335
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.
 
Old 05-17-2009, 05:32 AM   #11
stulli
LQ Newbie
 
Registered: May 2009
Posts: 5

Original Poster
Rep: Reputation: 0
Removing the -static works
Thanks a million for your help, basicly saved my semester.
 
  


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
Pthreads a12ctic Linux - Software 1 06-09-2005 10:05 PM
pthreads ftgow Linux - Software 0 07-08-2004 03:55 AM
pthreads Keith Hampton Linux - Software 1 10-23-2003 01:46 PM
pthreads h/w Programming 5 10-09-2003 11:11 AM
pthreads fr0ggeh Linux - Software 9 07-18-2003 10:43 AM

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

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