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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T03:18:50+00:00 2026-06-18T03:18:50+00:00

I am writing a test program at this point to get a base down

  • 0

I am writing a test program at this point to get a base down but when I run this it is feeding trash values into my struct.

If this is a bit incomplete I apologize, I’ve been working with this, searching the net for hours and everything I’m doing seems to be correct however I am getting garbage inserted into some critical values when I pass them through the pthread_create function.

Thanks for any help!

This code is giving me the following output:

main function running!

initial set of gWorkerid = 0

workerID = 319534848

Howdy Doody!

end sleep

gWorkerid is now trash value = -946297088

I am expecting:

main function running!

initial set of gWorkerid = 0

workerID = 0

Howdy Doody!

end sleep

gWorkerid is now trash value = 0

    #include <sys/socket.h>
    #include <netinet/in.h>
    #include <arpa/inet.h>
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include <sys/types.h>
    #include <pthread.h>

    #define MAX_CONN 3
    #define MAX_LINE 1000

    typedef struct worker_t
    {
        int id;
        int connection;
        pthread_t thread;
        int used;
    }WORKER_T;

    struct sockaddr_in gServ_addr;

    WORKER_T gWorker[MAX_CONN];
    char sendBuff[1025];

    // Thread function
    void * worker_proc(void *arg)
    {
        WORKER_T *me = (WORKER_T*) arg;

        printf("Howdy Doody!\n");

        return NULL;
    }

    int main(int argc, char *argv[])
    {
        printf("main function running!\n");
        pthread_t threadTest;
        int i = 0;

        gWorker[i].id = i;
        printf("initial set of gWorkerid = %d\n", gWorker[i].id);
        gWorker[i].connection = i;
        gWorker[i].used = 1;
        pthread_create(&gWorker[i], NULL, worker_proc, &gWorker[i]);

        sleep(1);

        printf("end sleep\n");
        printf("gWorkerid is now trash value = %d\n", gWorker[i].id);

        return 0;
    }
  • 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-18T03:18:52+00:00Added an answer on June 18, 2026 at 3:18 am

    The line:

    pthread_create (&gWorker[i], NULL, worker_proc, &gWorker[i]);
    

    should actually be:

    pthread_create (&(gWorker[i].thread), NULL, worker_proc, &gWorker[i]);
    

    That first parameter to pthread_create() is where to store the thread ID and, in the case of your code, it stores it at the start of the structure, overwriting id.

    By passing the address of the thread ID part of the structure, id should be left untouched by pthread_create().

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

Sidebar

Related Questions

Hi there I am writing this program but I can't get it to even
I'm writing a test for an FTP server in Perl, but I cannot get
I've ran into a strange situation. I'm writing some test cases for my program.
I'm writing a simple test program to pass multidimensional arrays. I've been struggling to
So I'm writing a program to test the endianess of a machine and print
I'm writing a program similar to a diagnostic program , that runs a test
I'm fairly new to writing test cases and this will be my first major
I feel bad asking this question but I am currently not able to program
I'm writing a program in c# for Windows7 that works very fine... But now
I want my program by default to stdout, but give the option of writing

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.