LinuxQuestions.org
View the Most Wanted LQ Wiki articles.
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 09-28-2005, 06:02 AM   #1
kourage
LQ Newbie
 
Registered: Sep 2005
Posts: 5

Rep: Reputation: 0
Compiling thread.c


Hi :]
First time on the forums, and i did try to do a search on google but got no help for for it.
Unless i searched the wrong thing but here it goes.

I already wrote this program in class and it works at the unix lab in my school. I just installed FC3 on my pc and did all the updates. But yet the program will not compile on my home pc.
Here is the code for the program

Code:
    4      4  #include <pthread.h>
      5      5  #include <iostream.h>
      6      6  #include <stdlib.h>
      7      7  #include <unistd.h>
      8      8
      9      9  void *run(void *param);
     10     10
     11     11  int main(int argc, char *argv[])
     12     12  {
     13     13          //First we take the input of argv[1] and convert it to an integer type.
     14     14          //Also iCounter will be our counter for amount of threads.
     15     15          int iAmtOfThrds = atoi(argv[1]), iCounter;
     16     16
     17     17          pthread_t tid[100]; //Thread id array for 'x' amount of threads.
     18     18          pthread_attr_t attr; //Thread attributes
     19     19          attr_init(&attr); //Initial thread attributes
     20     20
     21     21          //Start for...loop to create 'x' amount of threads
     22     22          //Then pass it to the run() function.
     23     23          for(iCounter = 0; iCounter < iAmtOfThrds; iCounter++)
     24     24                  pthread_create(&tid[iCounter], &attr, run, argv[1]); //Create a new thread multiple times
     25     25
     26     26          //Start for...loop to wait for each thread to finish
     27     27          for(iCounter = 0; iCounter < iAmtOfThrds; iCounter++)
     28     28                  pthread_join(tid[iCounter], NULL); //Wait for the new thread to finish
     29     29  }
     30     30
     31     31  /*
     32     32  This function we pass the argv[1] and
     33     33  let the thread sleep for 10 seconds.
     34     34  */
     35     35  void *run(void *param)
     36     36  {
     37     37          cout << "Starting New Thread.\n";
     38     38          sleep(10);//System Call to let the thread sleep for 10 seconds.
     39     39          cout << "Thread finished, exiting function.\n";
     40     40          pthread_exit(0);//Exits thread.
     41     41  }
But i get the following errors:
Quote:
$ g++ thread.c -lpthread -o thread.out
thread.c:1: error: expected unqualified-id before numeric constant
thread.c:1: error: expected `,' or `;' before numeric constant
thread.c:4: error: invalid token
thread.c:5: error: invalid token
thread.c:6: error: invalid token
thread.c:7: error: invalid token
thread.c:10: error: expected unqualified-id before numeric constant
thread.c:10: error: expected `,' or `;' before numeric constant
thread.c:30: error: expected unqualified-id before numeric constant
thread.c:30: error: expected `,' or `;' before numeric constant
thread.c:42: error: expected unqualified-id before numeric constant
thread.c:42: error: expected `,' or `;' before numeric constant
is there something that i do not have installed on this linux? at first i thought maybe i didn't have pthread.h but then it would have said something about not being able to find it.

Any help would be great b/c i'd like to work on it at home instead of going to school or working through ssh.
Thank you :]

Last edited by kourage; 09-28-2005 at 06:04 AM.
 
Old 09-28-2005, 06:07 AM   #2
sind
Member
 
Registered: Jun 2005
Posts: 75

Rep: Reputation: 15
Hi kourage,

Are the line numbers actually in the source file? If so, (going by the errors) that'll be what's causing the problem.

~sind
 
Old 09-28-2005, 06:09 AM   #3
kourage
LQ Newbie
 
Registered: Sep 2005
Posts: 5

Original Poster
Rep: Reputation: 0
yes that is the actual line numbers but that still doesn't make sense..b/c you'll see there are errors on line 1 and those were comments.
The code compiled perfectly using SSH and in the physical unix labs.
 
Old 09-28-2005, 06:30 AM   #4
sind
Member
 
Registered: Jun 2005
Posts: 75

Rep: Reputation: 15
Hmm... What compiler do you use in your lab?

My version of GCC (3.3.4) does not seem to accept line numbers as part of the source file. AFAIK it's not valid to have numbers before a line in C/C++.

I get these errors compiling your source file:

Code:
$ g++ -lpthread -Wall thread.cpp -o thread
thread.cpp:1: error: parse error before numeric constant
thread.cpp:1: error: parse error before `#' token
thread.cpp:2: error: parse error before `#' token
thread.cpp:3: error: parse error before `#' token
thread.cpp:4: error: parse error before `#' token
thread.cpp:20: error: `iAmtOfThrds' was not declared in this scope
thread.cpp:20: error: parse error before `;' token
thread.cpp:20: error: syntax error before `++' token
thread.cpp:24: error: `iAmtOfThrds' was not declared in this scope
thread.cpp:24: error: parse error before `;' token
thread.cpp:24: error: syntax error before `++' token
If I remove the numbers, and change attr_init to pthread_attr_init, it compiles and runs fine.



~sind

Last edited by sind; 09-28-2005 at 06:33 AM.
 
Old 09-28-2005, 07:34 AM   #5
kourage
LQ Newbie
 
Registered: Sep 2005
Posts: 5

Original Poster
Rep: Reputation: 0
omg im such an idiot, i can't believe i did that... i know what i did wrong now
wow thanks alot for ur help and helping out the biggest n00b :X
 
Old 09-28-2005, 07:39 AM   #6
sind
Member
 
Registered: Jun 2005
Posts: 75

Rep: Reputation: 15
I'm glad that I could help. Good luck with your studies.

~sind
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
Main thread sending notification to child thread rajesh_b Programming 1 09-22-2004 09:15 AM
configure qt thread issue (just compiled qt w/ -thread option) cleff Linux - Software 8 05-07-2004 11:11 PM
compiling Qt with thread support? timberwolf Linux - Software 4 12-06-2003 04:15 PM
compiling qt with thread support stevenhasty Linux - Software 1 07-03-2003 12:07 PM
Compiling Problems - New Thread ryanstrayer Linux - General 12 02-14-2002 02:31 AM


All times are GMT -5. The time now is 01:18 AM.

Main Menu
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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration