Programming Forums

Programming Forums (http://www.programmingforums.org/forumindex.php)
-   C (http://www.programmingforums.org/forum60.html)
-   -   Setting up (http://www.programmingforums.org/showthread.php?t=15781)

nannu May 5th, 2008 5:56 PM

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:
:

  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:
:

  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;
:

  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

The Dark May 5th, 2008 6:57 PM

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.

nannu May 5th, 2008 7:11 PM

Re: Setting up
 
Thank you very much Sir. I appreciate the help

BstrucT May 7th, 2008 8:09 AM

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++.

The Dark May 7th, 2008 8:23 AM

Re: Setting up
 
Yes, you can use printf in c++.

Alias May 7th, 2008 8:36 AM

Re: Setting up
 
Why mix C and C++? That's just unprofessional, not to mention ugly.

BstrucT May 8th, 2008 12:11 AM

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.


All times are GMT -5. The time now is 3:50 PM.

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