This is the mail archive of the
gcc-help@gcc.gnu.org
mailing list for the GCC project.
Re: Code(using pthreads) working in gcc2.96,NOT working in gcc3.2
- From: Mihnea Balta <dark_lkml at mymail dot ro>
- To: Ulrich Prakash <uprakash at npd dot hcltech dot com>, gcc-help at gcc dot gnu dot org
- Date: Tue, 25 Mar 2003 10:30:17 +0200
- Subject: Re: Code(using pthreads) working in gcc2.96,NOT working in gcc3.2
- References: <5.1.0.14.0.20030324195442.00aeb3e0@pilex>
There are two errors :)
First, you're declaring thread_ptr as a pointer which points to nowhere, so
the pthread library writes the thread descriptor at a random memory address.
Replace the declaration with:
pthread_t thread_ptr;
Then, when you create the thread, do:
if (0 != pthread_create (thread_ptr, &attr, start_proc, NULL)){
Secondly, the thread function must be something like:
void* start_proc(void*){
...
}
If you declare it something else, your program will end up by trashing the
stack when that function returns, and you will get a segfault. You could also
replace the pthread_exit(NULL) at the end with a simpler "return 0" or
"return NULL".
On Monday 24 March 2003 16:25, Ulrich Prakash wrote:
> Hi all,
>
> The following code works fine in gcc2.96 but CRASHES in gcc3.2..
> The code is a simple program demonstrating the use of mutex....
>
> Please help me find out the reason for the CRASH,as also i could not use
> GDB with gcc3.2(but could use well with gcc2.96)...
> **************OUTPUT***********
> [uprakash at voicedsp trial]$ ./a.out
> Thread started
> Main Program
> New Thread id is 1026l
> Parent Thread id is 1024l
> End of Program
> Segmentation fault
> [uprakash at voicedsp trial]$
> *********END OF OUTPUT****************
>
> *********CODE**********************
> #include <pthread.h>
>
> static pthread_mutex_t mutex_part = PTHREAD_MUTEX_INITIALIZER;
>
> void start_proc();
>
> int main()
> {
> pthread_attr_t attr;
> pthread_attr_t *temp_attr = NULL;
> pthread_t *thread_ptr;
>
> if (0 != pthread_attr_init (&attr)) {
> perror ("pthread_attr_init");
> goto thr_init;
> }
> if (0 != pthread_attr_setdetachstate (&attr,
> PTHREAD_CREATE_DETACHED)) {
> perror ("pthread_attr_setdetachstate");
> goto thr_init;
> }
> temp_attr = &attr;
>
>
> thr_init:
> if (0 != pthread_create (thread_ptr, temp_attr, (void
> *)start_proc, NULL)) {
> perror ("pthread_create");
> }
>
> printf ("Main Program\n");
>
> sleep (5);
> pthread_mutex_lock (&mutex_part);
> printf ("Parent Thread id is %ul\n",pthread_self() );
> pthread_mutex_unlock (&mutex_part);
>
> printf ("End of Program\n");
>
> return 0;
>
> }
>
>
> void start_proc()
> {
>
> printf ("Thread started\n");
> pthread_mutex_lock (&mutex_part);
> sleep (15);
> printf ("New Thread id is %ul\n",pthread_self() );
> pthread_mutex_unlock (&mutex_part);
>
> pthread_exit (NULL);
>
>
> }
>
>
> ------------------------------------------------------------------------
> http://felicitari.mymail.ro/