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.
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;
}
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:
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.
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.
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.
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 */
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.
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:
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.