小编Ana*_*and的帖子

pthread_join函数在执行后杀死线程还是需要调用pthread_cancel/pthread_exit?

pthread_join()函数执行后杀死线程还是需要调用pthread_cancel()/ pthread_exit()

我正在调用pthread_cancel()/ pthread_kill()返回3,即没有使用thread_id附加的线程.

#include <stdio.h>
#include <pthread.h>
#include <unistd.h>
#include <signal.h>

void * run (void *);

int main() {
pthread_t p1, p2;
int a = 9;
printf("%d\n", pthread_create(&p1, NULL, &run, (void*)&p1));
printf("%d\n", pthread_create(&p2, NULL, &run, (void*)&p2));

printf("%d\n", pthread_join(p1, NULL));
//usleep(1000);
printf("%d\n", pthread_join(p2, NULL));

printf("before exit\n");
printf("%d\n", pthread_cancel(p1));
printf("after exit\n");
printf("%d\n", pthread_cancel(p2));
printf("both thread exited\n");

printf("%d\n", pthread_join(p1, NULL));
printf("%d\n", pthread_join(p2, NULL));
printf("terminated\n");

printf("%d\n", pthread_kill(p1, 0));
printf("%d\n", pthread_kill(p2, 0));
printf("ext\n");

printf("%d\n", pthread_join(p1, NULL));
printf("%d\n", …
Run Code Online (Sandbox Code Playgroud)

c linux pthreads

2
推荐指数
1
解决办法
7325
查看次数

释放一个无效指针

如何释放void指针.

struct vStruct {
    void *vPtr;
    struct vStruct *next;
};

struct vStruct sObj;
struct vStruct *sObjNew = sObj;

delete sObjNew->vPtr; -----------> Is this correct way to delete void pointer
delete sObjNew;
Run Code Online (Sandbox Code Playgroud)

显示错误运算符'delete',应用于具有未定义行为的void*参数,并且很可能不会调用对象的析构函数.

c++ pointers void-pointers

0
推荐指数
1
解决办法
1762
查看次数

使用delete运算符删除void指针

什么错误的代码.应该是什么.因为它抛出一个错误.运算符'delete',应用于void*参数.

int i;
void *ptr = &i;

delete ptr;
Run Code Online (Sandbox Code Playgroud)

c++ void-pointers

-4
推荐指数
1
解决办法
5105
查看次数

标签 统计

c++ ×2

void-pointers ×2

c ×1

linux ×1

pointers ×1

pthreads ×1