Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old May 5th, 2008, 5:56 PM   #1
nannu
Newbie
 
Join Date: Apr 2008
Posts: 19
Rep Power: 0 nannu is on a distinguished road
Setting up

Fairly new to programming. Any advice would be appreciated.
Thanks in advance

OK, I am using Microsoft Visual C++ for all of my C++ programming. We have done some very basic code just to get an overview of what is going on. I have found some code below, this code is pretty long compared to what we have done as we skimmed over the programs. But in MS Visual C++ how would I put this into the program.

THREADS:
c Syntax (Toggle Plain Text)
  1. #include <pthread.h>
  2. #include <stdlib.h>
  3. #include <unistd.h>
  4. #include <stdio.h>
  5.  
  6. int myglobal;
  7.  
  8. void *thread_function(void *arg) {
  9. int i,j;
  10. for ( i=0; i<20; i++ ) {
  11. j=myglobal;
  12. j=j+1;
  13. printf(".");
  14. fflush(stdout);
  15. sleep(1);
  16. myglobal=j;
  17. }
  18. return NULL;
  19. }
  20.  
  21. int main(void) {
  22.  
  23. pthread_t mythread;
  24. int i;
  25.  
  26. if ( pthread_create( &mythread, NULL, thread_function, NULL) ) {
  27. printf("error creating thread.");
  28. abort();
  29. }
  30.  
  31. for ( i=0; i<20; i++) {
  32. myglobal=myglobal+1;
  33. printf("o");
  34. fflush(stdout);
  35. sleep(1);
  36. }
  37.  
  38. if ( pthread_join ( mythread, NULL ) ) {
  39. printf("error joining thread.");
  40. abort();
  41. }
  42.  
  43. printf("\nmyglobal equals %d\n",myglobal);
  44.  
  45. exit(0);
  46. }
OK what I am doing is when I open up the project I create the work space and give it a name. Now when I copy this code into the project and click compile, I get 1 error saying "Cannot open include file: 'pthread.h': No such file or directory". Now I know that in C++ you can have are .cpp file and a .h file. Now how can I prevent this from happening. I tried changing it up a little. I created a header file and pasted this part in it:
c Syntax (Toggle Plain Text)
  1. #include <iostream>
  2. //#include <FK2008Sample2.h>
  3.  
  4. #include <pthread.h>
  5. #include <stdlib.h>
  6. #include <unistd.h>
  7. #include <stdio.h>
  8. #include <FK2008Sample>
  9. int myglobal;
  10.  
  11. void *thread_function(void *arg) {
  12. int i,j;
  13. for ( i=0; i<20; i++ ) {
  14. j=myglobal;
  15. j=j+1;
  16. printf(".");
  17. fflush(stdout);
  18. sleep(1);
  19. myglobal=j;
  20. }
  21. return NULL;
  22. }
and then I created a .cpp file and pasted this in it:
using namespace std;
c Syntax (Toggle Plain Text)
  1. int main(void) {
  2.  
  3. pthread_t mythread;
  4. int i;
  5.  
  6. if ( pthread_create( &mythread, NULL, thread_function, NULL) ) {
  7. printf("error creating thread.");
  8. abort();
  9. }
  10.  
  11. for ( i=0; i<20; i++) {
  12. myglobal=myglobal+1;
  13. printf("o");
  14. fflush(stdout);
  15. sleep(1);
  16. }
  17.  
  18. if ( pthread_join ( mythread, NULL ) ) {
  19. printf("error joining thread.");
  20. abort();
  21. }
  22.  
  23. printf("\nmyglobal equals %d\n",myglobal);
  24.  
  25. exit(0);
  26.  
  27. }
But I am still getting errors: I know I am making a silly mistake that will probably take a second to change.

Thanks in advance

Last edited by Ancient Dragon; May 5th, 2008 at 8:15 PM. Reason: add code tags
nannu is offline   Reply With Quote
Old May 5th, 2008, 6:57 PM   #2
The Dark
Expert Programmer
 
Join Date: Jun 2005
Posts: 810
Rep Power: 4 The Dark is on a distinguished road
Re: Setting up

pthread.h is a posix (linux/unix) include file. It is not included in Visual C++. For threading under Visual C++, you would need to use something like the _beginthreadex function, which is in process.h. It might be easier to search for a different sample, rather than trying to convert this program to be compilable under Visual C++. There is a sample program here: http://simplesamples.info/Windows/_beginthreadex.php that I found using Google.
The Dark is offline   Reply With Quote
Old May 5th, 2008, 7:11 PM   #3
nannu
Newbie
 
Join Date: Apr 2008
Posts: 19
Rep Power: 0 nannu is on a distinguished road
Re: Setting up

Thank you very much Sir. I appreciate the help
nannu is offline   Reply With Quote
Old May 7th, 2008, 8:09 AM   #4
BstrucT
Hobbyist Programmer
 
BstrucT's Avatar
 
Join Date: Dec 2007
Location: Durban, South-Africa
Posts: 118
Rep Power: 1 BstrucT is on a distinguished road
Send a message via MSN to BstrucT
Re: Setting up

Just a little question guys...

Can the printf() function be used in C++ as well as in C?
Or is only 'cout' allowed in C++?

This is probably a dumb question but I know a lot of C syntax can be used in C++.
__________________
>BstrucT
BstrucT is offline   Reply With Quote
Old May 7th, 2008, 8:23 AM   #5
The Dark
Expert Programmer
 
Join Date: Jun 2005
Posts: 810
Rep Power: 4 The Dark is on a distinguished road
Re: Setting up

Yes, you can use printf in c++.
The Dark is offline   Reply With Quote
Old May 7th, 2008, 8:36 AM   #6
Alias
Newbie
 
Join Date: Oct 2007
Posts: 29
Rep Power: 0 Alias is on a distinguished road
Re: Setting up

Why mix C and C++? That's just unprofessional, not to mention ugly.
Alias is offline   Reply With Quote
Old May 8th, 2008, 12:11 AM   #7
BstrucT
Hobbyist Programmer
 
BstrucT's Avatar
 
Join Date: Dec 2007
Location: Durban, South-Africa
Posts: 118
Rep Power: 1 BstrucT is on a distinguished road
Send a message via MSN to BstrucT
Re: Setting up

Thanks Alias, wasn't planning on mixing the two though, just wanted to know wether some of C syntax could be compatible with C++.
But thanks for the heads up man.

I started out with C, then did some Visual C#, and am now just familiarizing myself a bit with C++ syntax as most seems to be more or less the same as C, but I guess that will all change once I reach the OOP section of the tutorials I am busy with.
Oooh, and pointers.
__________________
>BstrucT
BstrucT is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

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 On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
[Batch] Setting up Visual Studio build script Klarre Other Scripting Languages 4 Mar 28th, 2008 8:03 PM
Setting up Apache jman05 Other Web Development Languages 9 Nov 10th, 2006 12:58 PM
need help setting up php and apache on my windows XP machine dark_omen PHP 3 Jul 10th, 2006 8:12 AM
Setting Process Name HackeZ C++ 11 Dec 26th, 2005 11:25 AM
Programmatically Setting Environment Variables ChiHappens C# 1 Sep 26th, 2005 2:51 PM




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 8:12 PM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC