#C- Unique Thread Identifier

12 messages · Page 1 of 1 (latest)

undone wrenBOT
#

When your question is answered use !solved to mark the question as resolved.

Remember to ask specific questions, provide necessary details, and reduce your question to its simplest form. For tips on how to ask a good question run !howto ask.

#

I am aiming for each thread to have a unique identifier which I require for a bigger piece of code, but when ran it only pr```cpp
ints out the same identifier for each thread. What's the solution for this?

Code:

#include <pthread.h>
#include <stdio.h>

#define NTHREADS 10

void *thread_function(void *);

pthread_mutex_t mutex1 = PTHREAD_MUTEX_INITIALIZER;

int counter = 0;

int main() {
pthread_t thread_id[NTHREADS];
int i, j;

for (i = 0; i < NTHREADS; i++) {
pthread_create(&thread_id[i], NULL, thread_function, (void*)&i);
}

for (j = 0; j < NTHREADS; j++) {
pthread_join(thread_id[j], NULL);
}
}

void* thread_function(void* identifier) {
int threadIdentifier = ((int)identifier);

printf("Thread ID %d\n", threadIdentifier);
}

orange juice
pale crypt
#

(void*)&i you're passing every thread a pointer to the same variable i on the stack

#

You'll need NTHREADS different ints in memory, either an array or allocated with malloc or whatever.
Or you could do something kind of hacky: (void*)i and then (int)identifier in the thread

#

The "(void*)i and then (int)identifier in the thread" thing works

#

;compile c -lpthread

#include <pthread.h>
#include <stdio.h>

#define NTHREADS 10

void *thread_function(void *);

pthread_mutex_t mutex1 = PTHREAD_MUTEX_INITIALIZER;

int counter = 0;

int main() {
  pthread_t thread_id[NTHREADS];
  int i, j;

  for (i = 0; i < NTHREADS; i++) {
    pthread_create(&thread_id[i], NULL, thread_function, (void*)i);
  }

  for (j = 0; j < NTHREADS; j++) {
    pthread_join(thread_id[j], NULL);
  }
}

void* thread_function(void* identifier) {
  int threadIdentifier = (int*)identifier;

  printf("Thread ID %d\n", threadIdentifier);
}
rocky slateBOT
#
Program Output
Thread ID 0
Thread ID 1
Thread ID 2
Thread ID 3
Thread ID 4
Thread ID 5
Thread ID 6
Thread ID 7
Thread ID 8
Thread ID 9
Compiler Output
<source>: In function 'main':
<source>:17:58: warning: cast to pointer from integer of different size [-Wint-to-pointer-cast]
   17 |     pthread_create(&thread_id[i], NULL, thread_function, (void*)i);
      |                                                          ^
<source>: In function 'thread_function':
<source>:26:26: warning: initialization of 'int' from 'int *' makes integer from pointer without a cast [-Wint-conversion]
   26 |   int threadIdentifier = (int*)identifier;
      |                          ^
pale crypt
#

You could also take a look at

#

!man pthread_self

undone wrenBOT
#

pthread_self - obtain ID of the calling thread

Synopsis
#include <pthread.h>

pthread_t pthread_self(void);

Compile and link with -pthread.

pale crypt
#

Np

undone wrenBOT
#

@bright zodiac Has your question been resolved? If so, run !solved :)