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 8019973
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T21:33:19+00:00 2026-06-04T21:33:19+00:00

If I create thread using Pthread library under Linux, I need to use function

  • 0

If I create thread using Pthread library under Linux, I need to use function pthread_create, as one of the arguments it takes void *, so I can pass a pointer to something, so my thread routine can get access to it, but is it safe to do something like this

{//some scope
    int a=5//scope variable
    pthread_create(&id,NULL,some_function,(void*)a);
}//end of scope

and in my routine:

void *some_function(void *_arg)
{
    int a=(int)arg;
    return NULL;
}

I want to do something like this, so I can keep the value of a variable on stack so I can access it from my thread routine but I don’t want to create struct for single variable or manually allocate memory.

I will be creating few threads like this, so I wanted to know if in situation like this I can get by and don’t use list or dynamic array.

  • 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-04T21:33:20+00:00Added an answer on June 4, 2026 at 9:33 pm

    What you’re doing is perfectly safe in the real world: conversion between int and void * is not undefined behavior, it’s implementation-defined, and all implementations define it in the natural, sane way. It’s also the only efficient way to pass single-integer arguments to a new thread. Other approaches all require expensive synchronization, either via explicit locking or by using malloc in the original thread and free in the new thread (which has implicit synchronization cost hidden in the malloc/free implementation).

    One thing you will discover, however, is that some compilers issue warnings for the conversion. This is because of old broken code that assumes int can represent the full range of values of void *; the compiler is unable to distinguish between the (valid) practice of storing an int in a void *, and the (invalid) practice of storing a void * in an int. As a solution, you might want to use intptr_t instead of int, which will avoid the warning. If your original variables are int, simply adding an extra intermediate cast through intptr_t will avoid the warnings.

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

Sidebar

Related Questions

I wrote a program in C++ under Linux. For thread I am using pthread.
I am using pthread library under Linux for creating threads and I have two
I am using the pthreads library and when I create a thread I am
I am trying to create a thread using pthread. So far I have this:
I'm trying to create a new System.Threading.Thread object using Jscript, but I can't get
I create a second thread to call a method that downloads several images using:
I am trying to create this QT gui using a thread but no luck.
I am using vc++ and creating a thread using CreateThread function .Can I restart
I create a thread in a function,and in another function,I wanna stop this thread.
I got an EAGAIN when trying to spawn a thread using pthread_create. However, 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.