简体   繁体   English

在pthread_cancel中非法寻求

[英]Illegal Seek in pthread_cancel

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 到目前为止,还不错,但是唯一的问题是输出是错误:无法杀死线程:非法寻求杀死线程1

Killed Thread 2 杀死线程2

Killed Thread 3 杀死线程3

Killed Thread 4 杀死线程4

Killed Thread 5 杀死线程5

Killed Thread 6 杀死线程6

Killed Thread 7 杀死线程7

Killed Thread 8 杀死线程8

Killed Thread 9 杀死线程9

Why doesn't it kill the thread in the 0 index as well? 为什么它也不会杀死0索引中的线程?

And I couldn't find anything about Illegal Seek.. 而且我找不到关于非法寻求的任何信息。

Thanks for your help people 谢谢您的帮助

Thanks 谢谢

The problem is that newThread is being used before it is initialized: 问题是在初始化之前正在使用newThread

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() . 但是在成功调用pthread_create()之后, newThread不会接收到值。 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. 看来newThread变量在循环的后续迭代中保留了其先前的值,这导致所有线程的正确取消,除了最后一个启动的线程,因为其ID从未插入到readingThreadsPool数组中。

You need to populate the st->t member after the call to pthread_create() . 调用pthread_create()之后,需要填充st->t成员。

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. 就目前的代码而言,即使实际上不是线程,也可以将条目插入到readingThreadsPool数组中。 Put the insertion logic after the call to pthread_create() : 将插入逻辑放在对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 . 或者,如果pcapReadingThread()函数访问readingThreadsPool并期望readingThreadsPool输入一个条目(我认为可能是由于created传递而导致的情况),然后将pthread_create()封装在mutex_threadsPool锁内。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM