Thread number seems ridiculously high.

Hi, I have a program that is supposed to print out the thread ID along with other information, but...

The thread numbers are super high. 6295792 high.
That doesn't seem right at all.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
  void withdraw(int tid){

    pthread_mutex_lock(&mutex);
    for (int i=0; i<10; i++){
        int readbalance = balance;
        printf("At time %d, the balance for withdrawal thread %d is %d", i, tid, balance);
        if (readbalance < 10)
            printf("not enough");
        else {
            readbalance -= 10;
            usleep(1);
            balance = readbalance;
            printf("At time %d, the balance after withdrawal thread %d is %d", i, tid, balance);
            usleep(1);
        }
    }
    pthread_mutex_unlock(&mutex);
    pthread_exit(0);

}


Here's the rest, just in case.

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
#include <pthread.h>
#include <stdio.h>
#include <unistd.h> 

int balance = 100;
pthread_t wThread1;
pthread_t wThread2;
pthread_t dThread1;
pthread_t dThread2;

pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
void *p1;
void *p2;
void *p3;
void *p4;

void withdraw(int tid){

    pthread_mutex_lock(&mutex);

    for (int i=0; i<10; i++){
        int readbalance = balance;
        printf("At time %d, the balance for withdrawal thread %d is %d", i, tid, balance);
        if (readbalance < 10)
            printf("not enough");
        else {
            readbalance -= 10;
            usleep(1);
            balance = readbalance;
            printf("At time %d, the balance after withdrawal thread %d is %d", i, tid, balance);
            usleep(1);
        }
    }

    pthread_mutex_unlock(&mutex);
    pthread_exit(0);

}

void deposit(int tid){

    pthread_mutex_lock(&mutex);

    for (int i=0; i<10; i++){
        int readbalance = balance;
        printf("At time %d, the balance before depositing thread %d is %d", i, tid, balance);
        readbalance += 11;
        usleep(10);
        balance = readbalance;
        printf("At time %d, the balance after depositing thread %d is %d", i, tid, balance);
        usleep(10);
    }

    pthread_mutex_unlock(&mutex);
    pthread_exit(0);

}

int main(){

    pthread_create(&wThread1, NULL, (void *)withdraw, &p1);
    pthread_create(&wThread2, NULL, (void *)withdraw, &p2);
    pthread_create(&dThread1, NULL, (void *)deposit, &p3);
    pthread_create(&dThread2, NULL, (void *)deposit, &p4);

    pthread_join(wThread1, NULL);
    pthread_join(wThread2, NULL);
    pthread_join(dThread1, NULL);
    pthread_join(dThread2, NULL);

    return 0;

}
Last edited on
The thread ID is just a token given to you. It is not correlated to the number of active threads. (And is probably just some kind of hash.)
I appreciate you responding. I have been looking everywhere for something relevant, and haven't found much.

So, if I changed withdraw(int tid) to (void *tid)
and printf(%p, tid) and got a #4565dea (Or something like that), that wouldn't have been a problem?
It's all wrong.
http://man7.org/linux/man-pages/man3/pthread_create.3.html

1. void withdraw(int tid)
A thread function takes a void* parameter, and returns a void* result.
Pretending that it's an int won't get you anything.

2. pthread_create(&wThread1, NULL, (void *)withdraw, &p1);
If you had proper definitions for your thread functions, you wouldn't need that cast.
Also, those p1,p2,p3,p4 have absolutely nothing to do with "what's my thread".
Use http://man7.org/linux/man-pages/man3/pthread_self.3.html if you want a "this is me" identifier.

3. Your use of mutexes is all but pointless.
Holding the mutex for all the iterations of your loop just makes those 4 functions run sequentially.
The only slight doubt would be which order they happen in when the system resolves the contention as to who goes next at the point unlock is called.
I'm glad you responded when you did. I have an hour to fix it all.

I'll go back and try to fix them one by one. I'll start with the 1st.

Should I cast it as an int inside the function?

Point 2 will take me some time to understand.

Point 3. I have since moved the locks and unlocks inside of the loop. Beginning and end.
Is that less pointless?
1
2
3
4
void *worker ( void *p ) {
    pthread_t *ptr = p;
    printf("This is me %lu\n", (unsigned long)*ptr);
}

Since you can't assume anything about the underlying type of pthread_t, displaying it can be a challenge.
https://stackoverflow.com/questions/1759794/how-to-print-pthread-t

You would call this worker from main as follows.
1
2
3
4
int main ( ) {
    pthread_create(&wThread1, NULL, worker, &wThread1);
}
Thank you. I tried doing int *ptr = (int *)tid but that just gave me a 0 for every ID.

I'm going to read more about point 2 now.

I see now that you addressed point 2 in that post. Thank you again lol.
Last edited on
Shrug.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
$ cat main.c
#include <stdio.h>
#include <pthread.h>

void *worker ( void *p ) {
    pthread_t *ptr = p;
    printf("This is me %lu\n", (unsigned long)*ptr);
    return NULL;
}

int main ( ) {
    pthread_t wThread1;
    pthread_create(&wThread1, NULL, worker, &wThread1);
    pthread_join(wThread1,NULL);
}
$ gcc main.c -pthread
$ ./a.out 
This is me 140651391973120
$ ./a.out 
This is me 139897773295360

I wonder why it gives me a zero for each ID.
Topic archived. No new replies allowed.