code not working..

i am trying to run a code having pthreads lib in it but i cant run it it gives error with pthreads.h can any one look up and solve it? code is given below.. mY Os is Windows. is this problem with Os?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
#define _MULTI_THREADED
#include <pthread.h>
#include <stdio.h>
#include "check.h"
#include <conio.h>
void *threadfunc(void *parm)
{
  printf("Thread created using an default attributes\n");
  return NULL;
}

int main(int argc, char **argv)
{
  pthread_t             thread;
  int                   rc=0;
  pthread_attr_t        pta;

  printf("Enter Testcase - %s\n", argv[0]);

  printf("Create a thread attributes object\n");
  rc = pthread_attr_init(&pta);
  checkResults("pthread_attr_init()\n", rc);

  printf("Create a thread using the attributes object\n");
  rc = pthread_create(&thread, &pta, threadfunc, NULL);
  checkResults("pthread_create()\n", rc);

  printf("Create a thread using the default attributes\n");
  rc = pthread_create(&thread, NULL, threadfunc, NULL);
  checkResults("pthread_create()\n", rc);

  printf("Destroy thread attributes object\n");
  rc = pthread_attr_destroy(&pta);
  checkResults("pthread_attr_destroy()\n", rc);

  /* sleep() isn't a very robust way to wait for the thread */
  sleep(5);

  printf("Main completed\n");
  getch();
}
}
What is the specific error you are getting (copy & paste it here)?
Last edited on
Do you have MinGW or some other implementation installed?

I don't think pthreads is installed on standard Windows environments.

What is the specific error you are getting (copy & paste it here)?

it gives me error that can't find pthread.h



Do you have MinGW or some other implementation installed?

I don't think pthreads is installed on standard Windows environments.

i have installed cygwin but am not running this code in it.. do i need some other things? can you tell me about windows.h? are these things same?
You have to designate the INCLUDE_PATH for your compiler to tell it where pthread.h was installed by cygwin (find the file pthread.h first).

For gcc, you use -I for this. For an IDE, you need to look up compiler settings.

You have to designate the INCLUDE_PATH for your compiler to tell it where pthread.h was installed by cygwin (find the file pthread.h first).

For gcc, you use -I for this. For an IDE, you need to look up compiler settings.


i really appreciate your help but am afraid that i can't understand what you mean.. would you kindly explain in simple words.. regards..
For every #include, you are specifying a file which exists on your hard-drive somewhere.

When compiling, a compiler does not know where to look. Some compilers will look in standard places like /usr/include or /usr/local/include. Since your header files may be installed anywhere, you have to tell the compiler where to look. This is known as an INCLUDE_PATH.

You can add as many paths to your INCLUDE_PATH as you like, but if you are using an IDE on Windows like Visual Studio, you have to find the settings where you can set your INCLUDE_PATH. However, before you do this, you have find where that pthread.h file is. For CygWin, it might be in:
/usr/sys/sinclude/
that was help full am trying this.. i have copied a code from site but my compiler is not compiling it can you check it...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29

 #include <process.h>

  #include <stdio.h>

  
  int a[ 5 ];
  
  void Thread( void* pParams )
  { int i, num = 0;
  
    while ( 1 )
    { 
       for ( i = 0; i < 5; i++ ) a[ i ] = num;
       num++;
    }
  }
  
  int main( void )
  { 
     _beginthread( Thread, 0, NULL );
  
     while( 1 )
        printf("%d %d %d %d %d\n", 
               a[ 0 ], a[ 1 ], a[ 2 ],
               a[ 3 ], a[ 4 ] );
  
   return 0;
  }
Your program has a race condition - even if you manage to compile this program, you can get unpredictable, erratic results.

I recommend that you tackle the basics of C, compiling, reading error messages, etc... ...before you attempt pthreads - doing so will save you many headaches and many hours.
appreciated... :).. i am on that...
If you are on windows, why do you need to use pthreads anyway ?
What's wrong with CreateThread(), _beginthread() or _beginthreadex() WinAPI functions ?




Your code posted does compile for me using MinGW and Visual Studio just fine.
@ modoran: The OP is using Cygwin, so even though his OS is windows they are technically writing in a *nix environment. This is NOT the route I would have suggested for any user to start writing C++ in for reasons just like this. +1 for your suggestion, I'm glad to see another competent Win32 contributor.
If you are on windows, why do you need to use pthreads anyway ?
What's wrong with CreateThread(), _beginthread() or _beginthreadex() WinAPI functions ?
can you specify what exact pthreads do?
I think on windows platform pthreads is just a wrapper around said WinAPI functions. Not 100% sure about this.
Right from my copy of "pthread.h"
/* This is an implementation of the threads API of POSIX 1003.1-2001.

and
* Purpose:
* Provides an implementation of PThreads based upon the
* standard:
*
* POSIX 1003.1-2001
* and
* The Single Unix Specification version 3

I'm reading the header file now, I'll get back to you.

It seems to check for what compiler and what language you are using then if for example, it finds that you are using MingW (which would indicate you are writting on Windows) it redefines a bunch of stuff so that either Win32 or POSIX terminology can be used when calling the functions that it defines from the DLL.

I'm not a pro at this but I think that when modoran called it a wrapper it was an accurate description.
Topic archived. No new replies allowed.