Need help with threads (pthread library)

First, here's all the relevant code:

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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
#include "lab.h"
#include "rob.h"
#include "rob1.h"
#include "rob2.h"
void* RoboLoop(void*);

labyrinth* lab;
unsigned int threadsToFinish;

int main(int argc, char *argv[]){

    int i, threadnum=0;
    int type1 = 1, type2 = 0, type3 = 0;

    for (i=0; i<argc; i++) {
        printf("Argument:%d %s\n",i,argv[i]);
    }
    //read parameters
	char *labname = "./bin/Debug/maze3_braid.txt";

	if (argv[1] != 0){
        labname = argv[1]; //read the lab file
	}else{
	    cout<<"Als Labyrinthname wurde 'maze1_small.txt' angenommen. Zur Änderung mit './labrob DATEINAME -t1 ARG -t2 ARG -t3 ARG' starten"<<endl;
	};


	if (argc > 2){ //read the number of type1 robots
    //labrob DATEINAME [-t1 ROBANZAHL] [–t2 ROBANZAHL] [–t3 ROBANZAHL] [-h]
        if ((strcmp(argv[2],"-t1 ") && (argv[3] != 0))){
            type1 = atoi(argv[3]);
        }
        if ((strcmp(argv[4],"-t2 ") && (argv[5] != 0))){
            type2 = atoi(argv[5]);
        }
        if ((strcmp(argv[6],"-t2 ") && (argv[7] != 0))){
            type3 = atoi(argv[7]);
        }
	}

    cout<<type1<<" "<<type2<<" "<<type3<<endl;
    threadnum = type1 + type2 + type3;


    //Create new labyrinth
    lab = new labyrinth();
    //read labyrinthfile and save in lab
    lab->savelabyrinth(labname);
    cout<<"ex: "<<lab->entry[0]<<"    ey: "<<lab->entry[1]<<endl;
    cout<<"fx: "<<lab->exit[0]<<"    fy: "<<lab->exit[1]<<endl;


	// Creating Threads...
    pthread_attr_t attr;
    pthread_t thread[threadnum];
    pthread_attr_init(&attr);
    pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE);

    for(int i = 0; i < type1; i++)  //Create as many threads for Type1 as needed
    {
        // Start threads:
        threadsToFinish++;
        pthread_create(&thread[i], &attr, RoboLoop, (void*)"roboloop"); //Error somewhere in here

        //int pthread_create(pthread_t *thread, const pthread_attr_t *attr,void *(*start_routine)(void*), void *arg);
        //pthread_create(&thread[i], &attr, DoCalculation, &valueArray[i]);
    }

    while (threadsToFinish > 0) //Wait for threads
    {
        sleep(10);
    }
    //rob* robo = new rob1(laby);
    //robo->dolab();

    //delete laby;
    return 0;
}

void* RoboLoop(void*)   //Create a new Roboter
{
    rob* i = new rob1(lab); //new instance
    i->dolab();
    threadsToFinish--;
    return 0;
}


Now i'm getting a 'undefined reference to 'pthread_create'' error aswell as a 'undefined reference to 'vtable' in rob2'
of course there is a #include <pthread.h> in lab.h

As far as I know the error is when i try to create the thread using this code:
1
2
3
4
5
6
    for(int i = 0; i < type1; i++)  //Create as many threads for Type1 as needed
    {
        // Start threads:
        threadsToFinish++;
        pthread_create(&thread[i], &attr, RoboLoop, (void*)"roboloop"); //Error somewhere in here
    }


the void RoboLoop looks like this:
1
2
3
4
5
6
7
void* RoboLoop(void*)   //Create a new Roboter
{
    rob* i = new rob1(lab); //new instance
    i->dolab();
    threadsToFinish--;
    return 0;
}


Hope you can help me as I just can't find a solution to it

If the code isn't understandable, please tell me and I'll try to format it a bit better.
Last edited on
#include <pthread.h>
as i said, pthread.h is included in "lab.h", which is included in the file.

I including it right here, but it doesn't change anything
You have to link to pthread.
you mean when compiling? the -lpthread?
Last edited on
Yes.
I just did it exactly as it is described here:
http://gabrielshen.blogspot.com/2008/06/how-to-run-pthread-in-codeblocks.html

No change :(
makefile won't compile either


€:
Stupid code::blocks wanted me to write -pthread in both compiler and linker options. Then it worked.
Thank's for your help! :)
Last edited on
Topic archived. No new replies allowed.