LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 04-24-2005, 05:22 AM   #1
live_dont_exist
Member
 
Registered: Aug 2004
Location: India
Distribution: Redhat 9.0,FC3,FC5,FC10
Posts: 257

Rep: Reputation: 30
pthread_create problem


Hi Guys,
I'm trying to use threads and write soem code for TCP timers and seem to have got totlly stuck right at the start .It gives me a segmentation fault whenever I use pthread_create .Here's the code :-

Code:
#include <iostream>
#include <pthread.h>

using namespace std;

int store_ack_tid[20][20];
int i=0; int j=0;

void * dynamic_timer_calc(void *)
{
  cout<<"INSIDE DYNAMIC TIMER CALC"<<endl;
}

void start_timer(int storeack)
{
  int temp=0;
  cout<<"Storeack is "<<storeack<<endl;
  pthread_t tid;   /*=new pthread_t*/
  temp=pthread_create(&tid,NULL,dynamic_timer_calc,NULL);
  cout<<"Error code is "<<temp<<endl;
}

void send_segment()
{
  int seq=5;   // IN THE REAL CODE THIS WILL NOT BE THERE AS THE SEQ WILL ALREADY BE DEFINED
  cout<<"CODE FOR SENDING SEGMENT"<<endl;
  start_timer(seq);
}

int main()
{
  cout<<"inside main"<<endl;

  for (i=0; i<19; i++)
   for (j=0; j<19; j++)
     store_ack_tid[i][j]=0;

  send_segment();
  return 0;
Whenever I comment that code related to pthread it runs okay . I've tried running it with and without arguments and the result is the same.

Here's my system type :-
[root@LABPC02 timers]#
uname -a 19,3 All
Linux LABPC02 2.4.20-8 #1 Thu Mar 13 17:54:28 EST 2003 i686 i686 i386 GNU/Linux
[root@LABPC02 timers]#


Do try and see what's going wrong here. Any help/advice is appreciated.

Thnx
Arvind
 
Old 04-24-2005, 05:53 AM   #2
live_dont_exist
Member
 
Registered: Aug 2004
Location: India
Distribution: Redhat 9.0,FC3,FC5,FC10
Posts: 257

Original Poster
Rep: Reputation: 30
Hey guys, never mind...I figured it out . I wasnt linking it up...so it should be
g++ -o timer -lpthread timer.cpp and it works properly . Iwasn't linking it previously.
Thnx for reading though.
Arvind
 
Old 04-24-2005, 05:56 AM   #3
Ephracis
Senior Member
 
Registered: Sep 2004
Location: Sweden
Distribution: Ubuntu, Debian
Posts: 1,109

Rep: Reputation: 50
It should work. Make sure you link it with the pthread library after you have compiled it. With g++ you should use the -lpthread switch.
 
Old 04-24-2005, 06:06 AM   #4
Hivemind
Member
 
Registered: Sep 2004
Posts: 273

Rep: Reputation: 30
Yeah, I tried and it doesn't segfault. But here's one thing you might want to fix:
Code:
for (i=0; i<19; i++)
   for (j=0; j<19; j++)
     store_ack_tid[i][j]=0;
That block doesn't set all elements to zero, the condition must me < 20 for both i and j for that. And why on earth are i and j globals??
Anyway, if you want to set all elements to zero, simply do:
Code:
memset(store_ack_tid, 0, sizeof(store_ack_tid));
Include <cstring> for std::memset. And use it with care, if the array has decayed to a pointer after being passed to a function, sizeof() will return the size of an int*, not the array itself. But if it's global like in your case, it will work.
 
Old 04-24-2005, 06:12 AM   #5
Hivemind
Member
 
Registered: Sep 2004
Posts: 273

Rep: Reputation: 30
And, also, I would change the call to pthread_create() to something like:
Code:
int return_value = 0;
if((return_value = pthread_create(&tid,NULL,dynamic_timer_calc,NULL)) != 0)
     cerr << "pthread_create() failed." << endl; /* TODO: print error code and description */
  else
     cout << "pthread_create() was successful." << endl;

Last edited by Hivemind; 04-24-2005 at 06:25 AM.
 
Old 04-24-2005, 06:21 AM   #6
live_dont_exist
Member
 
Registered: Aug 2004
Location: India
Distribution: Redhat 9.0,FC3,FC5,FC10
Posts: 257

Original Poster
Rep: Reputation: 30
hey...it works guys...thnx a million 4 all your suggestions...will try all of them out...thnx ...will post back if I have other problems..
Thnx
Arvind
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
pthread_create... os2 Programming 4 05-26-2005 03:09 PM
error on pthread_create? adamjmac Programming 2 05-23-2005 06:21 PM
Pthread_create rahul_kulkarni Programming 4 02-10-2005 11:48 AM
pthread_create tushar123 Programming 2 06-07-2004 08:15 AM
pthread_create parameters AquamaN Programming 3 03-16-2004 10:05 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 02:04 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration