LinuxQuestions.org
Help answer threads with 0 replies.
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 12-24-2004, 11:59 PM   #1
Ephracis
Senior Member
 
Registered: Sep 2004
Location: Sweden
Distribution: Ubuntu, Debian
Posts: 1,109

Rep: Reputation: 50
What happened to my threads?


I used threads in a C++ program before and now I have done a new part to it. When I put the parts together the thread did not work. The program died with a segmentation fault.

So I tried to do a simpe thread-program:
Code:
#include <iostream>
using namespace std;

void *newfunc(void*);

int main() {
        pthread_t th;
        int iret;
        int arg = 1;

        cout << "main: create thread" << endl;
        iret = pthread_create(&th, NULL, newfunc, (void*)arg);
        cout << "main: thread dead" << endl;
}

void *newfunc(void*) {
        cout << "thread: i am alive" << endl;
}
This can be compiled but cannot run properly:
Code:
$ g++ temp.cc -o blah
$ ./blah
main: create thread
Segmentation fault
Why is this? I am kinda confused...
 
Old 12-25-2004, 12:10 AM   #2
btmiller
Senior Member
 
Registered: May 2004
Location: In the DC 'burbs
Distribution: Arch, Scientific Linux, Debian, Ubuntu
Posts: 4,290

Rep: Reputation: 378Reputation: 378Reputation: 378Reputation: 378
Let's see ... arg is 1 and you're passing it as a void pointer. So basically you're passing the memory address 1 as the argument. Memory address 1 is probably not anywhere valid for your program, so this is likely what's causing the problem. You never do dereference the argumentt, though, so I'm slightly surprised it's segfaulting, still, you probably meant:

iret = pthread_create(&th, NULL, newfunc, (void*)&arg);

FYI, I tested this out, and you don't see the thread's output unless it happens to run before the main process exits. You should put a sleep call before the thread dead output in main, because there is no guarantee that the thread you create will run before the main thread. The correct way to wait on a thread is with pthread_join. HTH.
 
Old 12-25-2004, 12:19 AM   #3
Ephracis
Senior Member
 
Registered: Sep 2004
Location: Sweden
Distribution: Ubuntu, Debian
Posts: 1,109

Original Poster
Rep: Reputation: 50
Yeah, I mean (void*)&.

But hell. The problem was not the code. It all worked when I compiled with "-lpthread". I didn't know that not using -lpthread would cause segmentation fault but when I used it everything worked.

Pretty simple. I hate things like this. :P
 
Old 12-27-2004, 12:27 AM   #4
cppkid
Member
 
Registered: Jul 2004
Location: Pakistan
Distribution: Ubuntu
Posts: 185

Rep: Reputation: 30
When you use -lpthread it links your program with the pthread library, that library is responsable for many thread operations. It must have to compile it using -lpthread. Segmentation Fault means that your program is accessing some memory that is not allocated to your program. maybe some of your memory allocation have to be allocated by pthread library, so if you don't use it you get the error.
None of your thread program will run properly without compiling using -lpthread aurgument. You must read about shared libraries.
 
Old 01-10-2005, 04:16 PM   #5
davidr69
LQ Newbie
 
Registered: Jan 2005
Posts: 3

Rep: Reputation: 0
Unhappy pthreads segfaults - only on Linux

I have code that compiles on both Linux and Solaris 2.6, but it segfaults on Linux, while it runs on Solaris. This is my very first pthreads app, so I'm not sure what might be wrong.

I have a linked list (not typedef'd) which you can use dynamically {struct list *blah=(struct list *) malloc(sizeof(struct list));} or statically {struct list blah;}. In this case, I'm creating it "statically" because I need 64 lists, and I don't want a list of lists, which would have been required for "dynamic" lists. If I've confused you, just look at the code:


struct list commands[MAX_THREADS];

/* some initialization and population takes place here */

thread_status=pthread_create(thread_id, NULL, download, (void *) &commands[offset]);


and the function is declared as:

void *download(void *login)
{
struct list *commands=(struct list *) login;


There is way too much code to include it all. Let me just state that I read the previous post and was able to compile and run the short example (with the sleep included, and a printf in the newfunc thread to confirm).

Even with an empty "download" function, I still segfault in Linux. It works fine in Solaris. I would hate to have to admit this to my co-workers. I can't seem to figure out why.

Last edited by davidr69; 01-10-2005 at 04:23 PM.
 
Old 01-11-2005, 10:27 AM   #6
davidr69
LQ Newbie
 
Registered: Jan 2005
Posts: 3

Rep: Reputation: 0
Re: pthreads segfaults - only on Linux

Never mind; I figured it out. It was so stoooopid.

I was doing:

pthread_t *thread_id;
pthread_create(thread_id...

Syntactically, it's correct; i.e., I am passing a pointer. I feel so stupid for this, because I did it correctly everywhere else in my code. I didn't malloc! I could have done:

pthread_t *thread_id=(pthread_t *) malloc(sizeof(pthread_t));

but that's silly, because:

pthread_t thread_id;
pthread_create(&thread_id ...

is much more consice. I don't need memory for a thread id dynamically allocated. Let me just remember to KISS!
 
  


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
Java threads listed using kill -3 does not contain all threads found using ps -auxww coneheed Programming 2 11-14-2005 08:57 AM
what happened ?? bejiita Slackware 5 03-03-2005 03:56 AM
What ever happened to ..... ? t3gah General 2 03-02-2005 07:48 AM
What just happened.... midgcool Linux - Software 2 11-25-2004 05:10 PM
Java Threads vs Native Threads rjmendez Programming 0 08-16-2004 05:58 AM

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

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