I am wanting to generate a number of threads based off of how many a user wishes to create, and each of these threads is to carry out one function "readFiles". However, before the threads can each execute any of the meaningful code from readFiles, (represented by the "..." 's), I want ALL of the threads to each print out their thread ID's in a sort of introductory notification message, so something like...
"Thread ID _____ is processing!"
"Thread ID _____ is processing!"
"Thread ID _____ is processing!"
...
...
...
...but obviously with the code I have, each thread is only running one at a time so the thread ID's are only printing out when each new thread starts executing meaningful code, so this...
"Thread ID _____ is processing!"
...
"Thread ID _____ is processing!"
...
"Thread ID _____ is processing!"
...
Is there a way to get thread ID's before I call pthread_create() on each specific new thread? Or is there a way in which I can make these threads (which are supposed to run concurrently), hold off or pause their execution of the meaningful code within readFiles (their "..." 's) until they have each printed out the thread ID messages first? Thank you everybody so much!!! This is my code to give you a better idea of what I have at the moment...
static pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
uint64_t gettid()
{
pthread_t actual_tid = pthread_self();
uint64_t threadId = 0;
memcpy( &threadId, &actual_tid, sizeof( actual_tid ) );
return threadId;
}
void *readFiles( void *args )
{
pthread_mutex_lock(&mutex);
uint64_t current_id = gettid();
printf("Thread id = ");
printf("%" PRIu64 , current_id );
...
...
...
pthread_mutex_unlock(&mutex);
}
void alphabetcountmulthreads(int num_threads)
{
pthread_t ids[num_threads];
for ( int t = 0; t < num_threads; t++ )
{
if ( pthread_create( &ids[t], NULL, &readFiles, NULL ) != 0 )
{
fprintf( stderr, "error: Cannot create thread # %d\n", t );
break;
}
}
for ( int u = 0; u < num_threads; ++u )
{
if ( pthread_join( ids[u], NULL ) != 0 )
{
fprintf( stderr, "error: Cannot join thread # %d\N", u );
}
}
}