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

  • Home
  • SEARCH
  • 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 6103629
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T13:45:02+00:00 2026-05-23T13:45:02+00:00

I’m approaching pthreads library in C, I have written down some dummy code in

  • 0

I’m approaching pthreads library in C, I have written down some dummy code in order to get acquainted, but as I try to compile this code, I get an undefined reference to pthread_create, even if I have included the proper library and the number of parameters passed to the function is correct. Can you please help me out?

My code is:

#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#include <sys/time.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <unistd.h>
#include <string.h>

void *dummy_thread(void *arg)
{
    pthread_exit(NULL);
}

void *dummy_fork(void *arg)
{    
    exit(0);
}

void pthread_perror(char *string, int errcode)
{
    char errmsg[256];
    strerror_r(errcode, errmsg, 256);
    printf("%s:%s\n", string, errmsg);
}

int main(int argc, char** argv) 
{
    pthread_t *threads;
    int rc, nt, *pids;
    long threads_microsecs, fork_microsecs, t;
    struct timeval ora, dopo;
    float perc;

    if (argc != 2)
    {
        printf("Numero di argomenti %d non valido.\n", argc);
        exit(1);
    }
    nt = strtol(argv[1], NULL, 10);
    if (nt < 0)
    {
        printf("Numero di thread non valido: %d", nt);
        exit(1);
    }

    threads = (pthread_t *) malloc (nt * sizeof(pthread_t));
    pids = (int *) malloc (nt * sizeof(int));

    if (pids == NULL)
    {
        perror("malloc");
        exit(1);
    }

    gettimeofday(&ora, NULL);

    for (t = 0; t < nt; t++)
    {
        if (rc = pthread_create(&threads[t], NULL, dummy_thread, NULL)) 
        {
            pthread_perror("pthread_create", rc);
            exit(1);
        }
        gettimeofday(&dopo, NULL);
        threads_microsecs = dopo.tv_usec - ora.tv_usec;
        threads_microsecs += 1000000 * (dopo.tv_sec - ora.tv_sec);
        printf("Tempo per la creazione dei thread %ld nsec.\n", threads_microsecs);
        gettimeofday(&ora, NULL);

        for (t = 0; t < nt; t++)
        {
            if ((pids[t] = fork()) < 0)
            {
                perror("fork");
                exit(1);
            }
            if (pids[t] == 0)
            {
                dummy_fork(NULL);
            } 
        }

         gettimeofday(&dopo, NULL);
         fork_microsecs = dopo.tv_usec - ora.tv_usec;
         fork_microsecs += 1000000 * (dopo.tv_sec - ora.tv_sec);
         printf("Tempo per la creazione dei processi %ld nsec", fork_microsecs);
         perc = 100 * (((float) fork_microsecs - threads_microsecs)/(float)fork_microsecs);
         printf("(%.2f%%)\n", perc);


    }
    return (EXIT_SUCCESS);
}
  • 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-05-23T13:45:03+00:00Added an answer on May 23, 2026 at 1:45 pm

    Compile (err link) with -pthread

    gcc -Wall -Wextra -o prog prog.c -pthread
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have just tried to save a simple *.rtf file with some websites and
I have a French site that I want to parse, but am running into
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
I have some data like this: 1 2 3 4 5 9 2 6
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I want to count how many characters a certain string has in PHP, but
For some reason, after submitting a string like this Jack’s Spindle from a text
I have a jquery bug and I've been looking for hours now, I can't
this is what i have right now Drawing an RSS feed into the php,
Seemingly simple, but I cannot find anything relevant on the web. What is the

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.