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) 如何释放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*参数,并且很可能不会调用对象的析构函数.
什么错误的代码.应该是什么.因为它抛出一个错误.运算符'delete',应用于void*参数.
int i;
void *ptr = &i;
delete ptr;
Run Code Online (Sandbox Code Playgroud)