Programming Forums
User Name Password Register
 

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

 
 
Thread Tools Display Modes
Prev Previous Post in Thread   Next Post in Thread Next
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
 

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 10:56 PM.

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