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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T21:57:18+00:00 2026-06-14T21:57:18+00:00

I have a program that is trying to use create and cancel through an

  • 0

I have a program that is trying to use create and cancel through an implemented pool.

The creation is as follows:

int threadsNum=10;
while (created<threadsNum){
    pthread_t newThread;
    pthread_struct *st; //Open the thread that handle the deleting of the sessions timeout.
    st = (pthread_struct*)malloc(sizeof(pthread_struct));
    st->id = created;
    st->t = newThread;
    pthread_mutex_lock( &mutex_threadsPool );
    readingThreadsPool[created] = st;
    pthread_mutex_unlock( &mutex_threadsPool );
        if((threadRes1 = pthread_create( &newThread, NULL, pcapReadingThread, (void*)created)))
        {
        syslog(LOG_CRIT, "Creating Pcap-Reading Thread %d  failed.",created); 
                printf( "Creating Pcap-Reading Thread %d  failed.\n",created);
                exit(1);
        }
    syslog(LOG_INFO, "Created Pcap-Reading Thread %d Successfully.",created); 
    created++;
}

Later I try to cancel them and restart them :

    pthread_t t;
pthread_struct* tstr;
int i;
pthread_mutex_unlock( &mutex_threadsPool );
//first go on array and kill all threads
for(i = 0; i<threadsNum ; i++ ){
    tstr = readingThreadsPool[i];
    if (tstr!=NULL){
        t = tstr->t;
        if (pthread_cancel(t)!=0){
            perror("ERROR : Could not kill thread");
        }
        else{
            printf("Killed Thread %d \n",i);
        }
    }
}

So far so good, but the only problem is that the output is
Error : Could not kill thread : Illegal Seek
Killed Thread 1

Killed Thread 2

Killed Thread 3

Killed Thread 4

Killed Thread 5

Killed Thread 6

Killed Thread 7

Killed Thread 8

Killed Thread 9

Why doesn’t it kill the thread in the 0 index as well?

And I couldn’t find anything about Illegal Seek..

Thanks for your help people

Thanks

  • 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-14T21:57:19+00:00Added an answer on June 14, 2026 at 9:57 pm

    The problem is that newThread is being used before it is initialized:

    pthread_t newThread;
    pthread_struct *st;
    st = (pthread_struct*)malloc(sizeof(pthread_struct));
    st->id = created;
    st->t = newThread;
    

    but newThread does not receive a value until after the successful invocation of pthread_create(). It appears that newThread variable is retaining its previous value on subsequent iterations of the loop, which results in the correct cancelling of all threads except for last thread started as its id is never inserted into the readingThreadsPool array.

    You need to populate the st->t member after the call to pthread_create().

    As the code currently stands, it is possible for an entry to be inserted into the readingThreadsPool array even though it is not actually a thread yet. Put the insertion logic after the call to pthread_create():

    if((threadRes1 =
            pthread_create(&(st->t), NULL, pcapReadingThread, (void*)created)))
    {
        syslog(LOG_CRIT, "Creating Pcap-Reading Thread %d  failed.",created); 
        printf( "Creating Pcap-Reading Thread %d  failed.\n",created);
        exit(1);
    }
    pthread_mutex_lock( &mutex_threadsPool );
    readingThreadsPool[created] = st;
    pthread_mutex_unlock( &mutex_threadsPool );
    

    or if the pcapReadingThread() function accesses readingThreadsPool and expects an entry for itself (which I think might be the case due to created being passed) then enclose the pthread_create() inside the lock of mutex_threadsPool.

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

Sidebar

Related Questions

I have a program that is trying to use create and cancel through an
I have a small program that I'm trying to create to get ip addresses
I have been trying to create a Ruby program that will be running online
i'm trying to create a program that generates images for use as multi-screen backgrounds,
I have a c program that is trying to read in a file. Using
I have a calendar program that I am trying to convert to iPad (using
I have a C++ program that writes to stdout that I am trying to
I have a program that reads input from a file. I am trying to
I am trying to polish up a program a program that I have converted
I have been trying, in vain, to make a program that reads text out

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.