* Code(using pthreads) working in gcc2.96,NOT working in gcc3.2
@ 2003-03-24 14:53 Ulrich Prakash
2003-03-25 8:32 ` Mihnea Balta
0 siblings, 1 reply; 3+ messages in thread
From: Ulrich Prakash @ 2003-03-24 14:53 UTC (permalink / raw)
To: gcc-help
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@voicedsp trial]$ ./a.out
Thread started
Main Program
New Thread id is 1026l
Parent Thread id is 1024l
End of Program
Segmentation fault
[uprakash@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);
}
^ permalink raw reply [flat|nested] 3+ messages in thread
* Re: Code(using pthreads) working in gcc2.96,NOT working in gcc3.2
2003-03-24 14:53 Code(using pthreads) working in gcc2.96,NOT working in gcc3.2 Ulrich Prakash
@ 2003-03-25 8:32 ` Mihnea Balta
2003-03-25 9:38 ` Mihnea Balta
0 siblings, 1 reply; 3+ messages in thread
From: Mihnea Balta @ 2003-03-25 8:32 UTC (permalink / raw)
To: Ulrich Prakash, gcc-help
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@voicedsp trial]$ ./a.out
> Thread started
> Main Program
> New Thread id is 1026l
> Parent Thread id is 1024l
> End of Program
> Segmentation fault
> [uprakash@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/
^ permalink raw reply [flat|nested] 3+ messages in thread
* Re: Code(using pthreads) working in gcc2.96,NOT working in gcc3.2
2003-03-25 8:32 ` Mihnea Balta
@ 2003-03-25 9:38 ` Mihnea Balta
0 siblings, 0 replies; 3+ messages in thread
From: Mihnea Balta @ 2003-03-25 9:38 UTC (permalink / raw)
To: Ulrich Prakash, gcc-help
On Tuesday 25 March 2003 10:30, Mihnea Balta wrote:
> 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)){
Sorry, I meant "pthread_create(&thread_ptr, &attr, start_proc, NULL)".
^ permalink raw reply [flat|nested] 3+ messages in thread
end of thread, other threads:[~2003-03-25 8:32 UTC | newest]
Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2003-03-24 14:53 Code(using pthreads) working in gcc2.96,NOT working in gcc3.2 Ulrich Prakash
2003-03-25 8:32 ` Mihnea Balta
2003-03-25 9:38 ` Mihnea Balta
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).