Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8479653
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T19:05:33+00:00 2026-06-10T19:05:33+00:00

Reading through the pthread tutorial from LLNL I hit the following example code /******************************************************************************

  • 0

Reading through the pthread tutorial from LLNL I hit the following example code

/******************************************************************************
* FILE: hello.c
* DESCRIPTION:
*   A "hello world" Pthreads program.  Demonstrates thread creation and
*   termination.
* AUTHOR: Blaise Barney
* LAST REVISED: 08/09/11
******************************************************************************/
#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
#define NUM_THREADS 5

void *PrintHello(void *threadid)
{
   long tid;
   tid = (long)threadid;
   printf("Hello World! It's me, thread #%ld!\n", tid);
   pthread_exit(NULL);
}

int main(int argc, char *argv[])
{
   pthread_t threads[NUM_THREADS];
   int rc;
   long t;
   for(t=0;t<NUM_THREADS;t++){
     printf("In main: creating thread %ld\n", t);
     rc = pthread_create(&threads[t], NULL, PrintHello, (void *)t);
     if (rc){
       printf("ERROR; return code from pthread_create() is %d\n", rc);
       exit(-1);
       }
     }

   /* Last thing that main() should do */
   pthread_exit(NULL);
}

I can see why the long is cast through void * (as if it’s not and you pass a pointer to t the numbers printed out by the threads are garbled), my question is should this be considered kosher and always work? Or is this a quick hack to get the simplest possible example of threading working? Is this a standard C thing?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-10T19:05:35+00:00Added an answer on June 10, 2026 at 7:05 pm

    No, it’s not strictly kosher in terms of the ISO C standard since there’s no guarantee that a pointer is wide enough to hold a long.

    A kosher solution would be to pass a pointer to the long, either with one unique long per thread (such as one in an array) or having inter-thread communication (such as condition variables) between the creator thread and created thread so that the latter can make a copy before the former is allowed to change it for the next thread creation.

    However, the fact that it’s not strictly kosher doesn’t mean it won’t work in a specific implementation. Provided you can guarantee the the cast between void* and long won’t lose any information, it will probably work fine.

    From C11 6.3.2.3 Pointers (though it’s largely unchanged from C99):

    An integer may be converted to any pointer type. Except as previously specified, the
    result is implementation-defined, might not be correctly aligned, might not point to an
    entity of the referenced type, and might be a trap representation.

    Any pointer type may be converted to an integer type. Except as previously specified, the
    result is implementation-defined. If the result cannot be represented in the integer type,
    the behavior is undefined. The result need not be in the range of values of any integer
    type.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm reading through the excellent Ruby on Rails Tutorial and have encountered the following
When reading through this Async Sockets example , I find this code: // Get
Whilst reading through the DirectWrite source code I came across the following struct: ///
I'm reading through some MSDN example code on Composite controls and finding that there
Reading through documentation, I found following: 1.9.1 1.8.4 1.8.2 A version of 1.8.2 select
It's clear from reading through threads that I can call a PHP function using
I noticed while reading through my code that I have a static member function
Reading through the documentation for powershells add-type it seems you can add JScript code
Reading through more SICP and I'm stuck on exercise 1.3.8 . My code works
Reading through wikipedia I came across the concept of Sprints in Agile development. From

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.