If so they are defined in (/usr/include) errno.h or maybe a lower level header file such as /usr/include/sys/errno.h, that is included in errno.h. They are the same as used for the errno value from most posix calls. They are returned in this case because errno is global so you'd have no idea which thread caused the errno value to have been set.
#define EINTR 3
I don't know if EINTR actually is 3 but the point is there are tens of them, all defined in the above header file. When you do a printf or cout of the error you will get 3.
You can use strerror_r() (MT safe) or strerror() to get a const char[] description of your error
1 2 3 4 5 6 7 8 9
#include <errno.h>
#include <pthread.h>
int result = pthread_create(. . .);
if (result != 0)
{
cout << "pthread_create() failed. " << strerror(result) << endl;
}
1) Why?
2) I thouth a thread wil need about 2 MB, so with 25 threads we've got 50mb...
What to do against it?
EDIT:
The function returns 0. After a while it changes and then it returns 12 (Cannot allocate memory). The "old" pthreads will exit when not used anymore and only 23 are running a constant time (105 minutes).
This is what Ithink you are saying; you are creating threads at regular intervals. That they are being created successfully for certain amount of time but after a while they fail with the memory allocation error.
Is the code that succeeds early on the same code that fails later? If it is, I strongly suspect you have a memory leak.
Yes, killed threads should release the memory. However what is the rest of your code doing? Something is using up your memory heap. In the words of Sherlock Holmes... once you have eliminated the impossible, whatever is left, however improbable, must be the truth.
I've got 23 threads running all the time. They are simulating "players" and a Ball-Observer (checking if a goal was scored etc). When an if-clause is true a new thread is started (i.e. to shoot on the goal) after the function finished, the thread ends with "pthread_exit(NULL);"
For each of the "players" there is a structure containing skills and some statistics of the game. Then there are some var's for the scored goals, the ballposition and the played time.
Maybe the structures need this much of memory? Or do I exit the threads a wrong way they keep running?
Usualli those temporary running threads look like this:
void *schiess (void *threadarg){
if(balllock==0 && eventlock==0){
eventlock=1;
balllock=1;
schuss_data *my_data;
my_data = ( schuss_data *) threadarg;
int xdist=my_data->xdist;
int ydist=my_data->ydist;
int zdist=my_data->zdist;
int twx=my_data->twx;
int times=my_data->times;
int myteam=my_data->myteam;
int myside=my_data->myside;
int mynum=my_data->mynum;
int standard=my_data->standard;
int shoot=schuss(mynum,xdist, ydist, zdist, twx, times,myteam,myside,standard);
eventlock=0;
balllock=0;
}
pthread_exit(NULL);
}
balllock and eventlock are global, shoot returns a value, the structure "schuss_data" contains some informations about the shoot which will be started.
Edit: It's typically not a good idea to use that many threads. Especially when developing a game. You should have a game loop and do AI calls etc on each iteration. Threads can cause you issues, especially with timings and locks (e.g 1 player may not move for 10x longer than another because of locks). With a single-threaded loop each player/object gets equal opportunity to act.
bnbertha: schuss() is just moving the "ball" and changing his x and y koordinates (and will return 0)
Zaita: This worked very well. But when I'm thinking about it, it might be better in one thread, because it's possible this program is running not only once a time (different simulations).
I've chosen threads because I'm able to "move" the players simultaneous, each for a different amount of seconds. How should I do this with one thread? (If you got an example or an approach for me that would be nice)
Thanks
Marco
EDIT: Also it HAS to be possible for a "player" to get to the "ball" while he's "flying" (to prevent the goal or whatever)
Unless you are running this on a multi-core system then in reality the players are not moving simultaneously. The processor gives each thread a slice of time (neither garunteed or accurately timed).
The problem is that with 23 threads, thats a HUGE amount of overhead for the CPU and will give you a massive performance hit.
Still wondering why you return a void pointer instead of just void.
Single Threaded Approach (The same as 99%+ Prof Games)
In 1 thread you have a loop that is continuous, and at the beginning of each loop you work out how much time has passed. Then you iterate through each object you want to "update" and pass in the amount of time that has passed since it was last called. On a simple game, you should expect >2000 iterations a second happily.
By using the time since last updated you are able to move each object at the exact time in space.
Now I see why this Error Occured when simulating more than 1 Game/Time.
Thanks for your hint Zaita. You haven't got a good Tutorial for this by chance? :) [Otherwise i'll just try out]