I'm trying to learn about pthreads for a class I'm taking. I found a nice little threaded program online, at hpcf.nersc.gov/vendor_docs/ibm/aixprggd/genprogc/create_threads.htm and tried it out:
--------------------------------------------------------------------------------
//thread.cpp
#include <pthread.h>
#include <iostream>
#include <fstream>
#include <unistd.h>
void *Thread(void *string)
{
while(1)
printf("%s\n", (char *)string);
pthread_exit(NULL);
}
int main()
{
char *e_str = "Hello";
char *f_str = "Bonjour";
pthread_t e_th;
pthread_t f_th;
int rc;
rc = pthread_create(&e_th, NULL, Thread, (void *)e_str);
if(rc)
exit(-1);
rc = pthread_create(&f_th, NULL, Thread, (void *)f_str);
if(rc)
exit(-1);
sleep(5);
exit(0);
}
---------------------------------------------------------------------------------
My Makefile:
----------------------------------------------------------------------------------
LDFLAGS=-lstdc++
all: main
clean:
-rm -f main thread.o
main: thread.o
thread.o: thread.cpp
--------------------------------------------------------------------------------
I run Make, and it works fine. But when I try to run the program all it give me is:
"Segmentation Fault"
I'm running this thing on a Knoppix 3.3 install, if it helps any.
Assistance appreciated. I'm a pretty
Linux programmer, and barely know what I'm doing. A friend wrote the Makefile for another program, and I adapted it for this one.
I saw some very old stuff online that said some libraries don't seem to like pthreads, but nothing in recent years.