板凳——————————————————c++(95)

Linux C与C++ 一线开发p390
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#include <string.h>
#include <unistd.h>

void mycleanfunc(void *arg){
printf(“mycleanfunc:%d\n”, *((int *)arg));
}

void *thfrunc1(void * arg){
int m = 1, n = 2;
printf(“thfrunc1 comes \n”);
pthread_cleanup_push(mycleanfunc, &m);
pthread_cleanup_push(mycleanfunc, &n);
pthread_cleanup_pop(1);
pthread_exit(0);
//return (void *)0;
pthread_cleanup_pop(0);
}

void *thfunc2(void *arg){
int i = 1;
printf(“thread start ---------\n”);
pthread_cleanup_push(mycleanfunc, &i);
while(1){
i++;
printf(“i = %d\n”, i);
}
printf(“this line will not run\n”);
pthread_cleanup_pop(0);
return (void *) 0;
}

int main(void){
pthread_t pid1;
int res;
res = pthread_create(&pid1, NULL, thfrunc1, NULL);
if(res){
printf(“pthread_create failed:%d\n”, strerror(res));
exit(1);
}
pthread_join(pid1, NULL);

   printf("main over\n");
   
   void * ret = NULL;
   int iret = 0;
   pthread_t  tid;
   pthread_create(&tid, NULL, thfunc2, NULL);
   sleep(1);
   pthread_cancel(tid);
   pthread_join(tid, &ret);
   if(ret == PTHREAD_CANCELED)
       printf("thread has stopped, and exit code: %d\n", ret);
   else
       printf("some error occured");
       
   return 0;

}
/*
wannian07@wannian07-PC:~$ g++ -o c13 c13.cpp -lpthread
wannian07@wannian07-PC:~$ ./c13
thfrunc1 comes
mycleanfunc:2
mycleanfunc:1
main over

thread start ---------
i = 2
i = 3
:
:
i = 332980
mycleanfunc:332980
thread has stopped, and exit code: -1

*/

#include <stdio.h>
#include <stdlib.h>
#include
#include
#include
#include <unistd.h>
#include
#include

// p394 8-20 2020年03月04日 星期三 19时37分52秒
void thfunc1(int n){
std::cout << “thfunc:” << n << std::endl;
}

void thfunc2(){
std::cout << " I am c++ 11 thread func " << std::endl;
}

typedef struct{
int n;
const char * str;
}MYSTRUCT;

void thfunc3(void* arg){
MYSTRUCT p = (MYSTRUCT) arg;
std::cout << "thfunc: " << p->n << ", str = " << p->str << std::endl;
}

void thfunc4(int n, int m, int *pk, char s[]){

 std::cout << "in thfunc:n= " << n << ", m = " << m 
      << ", k = " << *pk << "\nstr=" << s <<  std::endl;
      *pk = 5000;

}

void fun(int & n){
std::cout << "fun: " << n << “\n”;
n += 20;
std::this_thread::sleep_for(std::chrono::milliseconds(10));
}

void thfunc6(int n, int m, int *k, char s[]){

 std::cout << "in thfunc:n= " << n << ", m = " << m 
      << ", k = " << *k << "\nstr=" << s <<  std::endl;
      *k = 5000;

}
int main(int argc, const char * argv[]){
// std::thread threads[5];
// std::cout << “create 5 threads…\n”;
// for(int i = 0; i < 5; i++)
// threads[i] = std::thread(thfunc1, i+1);

// for(auto& t : threads)
// t.join();

// std::cout << “All threads joined.\n” ;
// return EXIT_SUCCESS;

  std::thread t2(thfunc2);
  sleep(1);

// return 0; //terminate called without an active exception 已放弃
t2.join(); // t.detach();

 MYSTRUCT mystruct;
 mystruct.n = 110;
 mystruct.str = "hello world";
 
 std::thread t3(thfunc3, &mystruct);
 t3.join();
 
 int n4 = 110, m4 = 200, k4 = 5;
 char str4[] = "hello, world";

 std::thread t4(thfunc4, n4, m4, &k4, str4);
 t4.join();
 std::cout << "k4 = " << k4 << std::endl;
 
 int n5 = 0;
 std::cout << "n5 = " << n5 << "\n";
 n5 = 10;
  std::thread t5(fun, std::ref(n5));
  std::thread t6(move(t5));
  t6.join();
  std::cout << "n5 =" << n5 << "\n";
  
  int n6 = 110, m6 = 200, k6 = 5;
 char str6[] = "hello, world";

std::thread t7(thfunc6, n6, m6, &k6, str6);
t7.detach();
std::cout << "k6 = " << k6 << std::endl;
pthread_exit(NULL);

std::cout << "this line will not run" <<std::endl;
return 0;

}
/*
wannian07@wannian07-PC:~$ g++ -std=c++17 -o c13 c13.cpp -lpthread
wannian07@wannian07-PC:~$ ./c13
create 5 threads…
thfunc:thfunc:2
1
thfunc:3
thfunc:4
thfunc:5
All threads joined.

wannian07@wannian07-PC:~$ g++ -std=c++17 -o c13 c13.cpp -lpthread
wannian07@wannian07-PC:~$ ./c13
I am c++ 11 thread func

https://www.cnblogs.com/leoncumt/p/10505754.html
使用detach()函数,让主线程和子线程分开运行,即使主线程运行完了,也就是这个进程已经结束了,而子线程还在继续运行,它将会交由操作系统托管,这将会导致很多的问题。
如果使用detach():
1.如果线程传参数传递int这种简单的类型,不要传递引用,亲测编译器会报错。
2.如果传递的是一个类对象,不要让编译器进行隐式类型转换,在创建线程的同时就构建出来一个临时对象,并且在函数声明的时候使用引用来接收,否则会右调用一次拷贝构造函数,再重新创建一个对象。
3.创建线程的时候使用了一个临时变量,在主线程结束的之后,临时变量所占用的内存已经被回收了,这是子线程再去使用这一片已经被系统回收的内存,就会造成内存泄漏的危险行为。
so:能用join就不要用detach。

thfunc: 110, str = hello world

in thfunc:n= 110, m = 200, k = 5
str=hello, world
k = 5000

n5 = 0
fun: 10
n5 =30

n5 =30
k6 = 5
in thfunc:n= 110, m = 200, k = 5
str=hello, world

*/

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值