Threads Error! Pls help

Discussion of programming on Linux, including shell scripting, perl, python, c/c++, mono, java. Whatever tickles your fancy.
Post Reply
Linus_khan
Cadet
Posts: 14
Joined: Wed Jul 14, 2004 1:46 am

Threads Error! Pls help

Post by Linus_khan »

A.O.A
I have these two starting tutorials on POSIX threads programing,but when I compile it on either Redhat or Mandrake 10,this error is generated
undefined attribute in pthread_create().I think it shows the error due to soem arguemenst in this funcaion but I have downloaded this code from a tutorial so do't know what's wrong.Here is the code

#include<stdio.h>
#include<pthread.h>

void print(void * ptr);

int main(int argc,char*argv[])
{
pthread_t thread1;
sleep(1);
char *msg="Hye";
print(msg);
pthread_create(&thread1,NULL,print,(void *) msg);
printf("hello %s\n",argv[1]);
}

void print(void * ptr)
{
char *message;
message=(char*)ptr;
printf("%s",message);
pthread_exit(0);
}
/////// Code 2 ////////////
void print(void * ptr);

int main(int argc,char * argv[])
{
pthread_t thread1,thread2;
char *message1="Hello";
char *message2="World";

pthread_create(&thread1,pthread_attr_default,(void*)&print,(void*)message1);
sleep(10);
pthread_create(&thread2,pthread_attr_default,(void*)&print,(void*)message2);
sleep(10);
exit(0);


}


void print(void * ptr)
{
char *message;
message=(char*)ptr;
printf("%s",message);
pthread_exit(0);

}

Pls respond quickly,Thanks
jess
Company Havaldaar Major
Posts: 198
Joined: Sat Jul 31, 2004 7:05 am
Location: Toronto
Contact:

Post by jess »

Look in the header file <pthread.h>, as to what arguments it expects, and what you are passing to it. Header files are generally in /usr/include or /usr/src/include.

Tutorial examples may not work from kernel version to version.
Post Reply