板凳——————————————————c++(96)

Linux C与C++ 一线开发p401
//2020年07月16日 14时42分29秒
#include
#include
#include
#include
#include
#include <time.h>
#include <stddef.h>

std::thread::id main_thread_id = std::this_thread::get_id();
void is_main_thread(){
     if(main_thread_id == std::this_thread::get_id())
	std::cout << "This is the main thread.\n";
	else
	std::cout << "This is not the main thread.\n";
}

    std::atomic<bool> ready(false);

void thfunc(int id){
     while (!ready)
      std::this_thread::yield();
     
     for(volatile int i = 0; i < 1000000; i++)
     {}
      std::cout << id << ", ";
 
}

void getNowTime(){
     timespec time;
     struct tm nowTime;
     clock_gettime(CLOCK_REALTIME, & time);
     
     localtime_r(&time.tv_sec, & nowTime);
     char current[1024];
     printf(
     	"%04d-%02d-%02d  %02d:%02d:%02d\n", 
     	nowTime.tm_year + 1900,
     	nowTime.tm_mon + 1, 
     	nowTime.tm_mday, 
     	nowTime.tm_hour,
     	nowTime.tm_min, 
     	nowTime.tm_sec);
}
	
int main(){
    is_main_thread();
    std::thread th(is_main_thread);
    th.join();
    
    
     std::thread threads[10];
     std::cout << "race of 10 threads that count to 1 million:\n";
    for(int i = 0; i < 10; i++)
    threads[i] =  std::thread(thfunc, i);
    ready = true;
    for(auto& th : threads) th.join();
     std::cout << '\n';

// using std::chrono::system_clock;
// std::time_t tt = system_clock::to_time_t(system_clock::now());
// struct std::tm * ptm = std::localtime(&tt);
// getNowTime();
// std::cout << “Waiting for the next minute to begin…\n”;
// ++ptm->tm_min;
// ptm->tm_sec = 0;
// std::this_thread::sleep_until(system_clock::from_time_t(mktime(ptm)));
// getNowTime();

	std::cout << "countdown:\n";
	for(int i = 5; i > 0; --i){
	   std::cout << i << std::endl;
	   std::this_thread::sleep_for(std::chrono::seconds(1));
	}
	std::cout << "Lift off!\n";
    return 0;
}

/*
wannian07@wannian07-PC:~$ g++ -std=c++17 -o c13 c13.cpp -lpthread
wannian07@wannian07-PC:~$ ./c13
This is the main thread.
This is not the main thread.

race of 10 threads that count to 1 million:
03, 4, , 9, 2, 1, 5, 6, 7, 8,

2020-07-16 14:32:04
Waiting for the next minute to begin…

countdown:
5
4
3
2
1
Lift off!
*/
//#include
#include <stdio.h>
#include <arpa/inet.h>
#include <stdlib.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <string.h>
#include <unistd.h>
#include <errno.h>

int main(int argc, const char* argv[]){
struct in_addr ia;
inet_aton(“172.16.2.6”, &ia);
printf(“ia.s_addr = 0x%x\n”, ia.s_addr);
printf(“real_ip = %s\n”, inet_ntoa(ia) );
Linux C与C++ 一线开发p501
int sfp, nfp;
struct sockaddr_in s_add, c_add;
socklen_t sin_size;
unsigned short portnum = 10051;
struct sockaddr_in serv;
socklen_t serv_len = sizeof(serv);

sfp = socket(AF_INET, SOCK_STREAM, 0);
if(-1 == sfp){
     printf("socket fail! \r\n");
     return -1;
}
printf("socket ok !\r\n");
printf("ip = %s, port = %d\r\n", inet_ntoa(serv.sin_addr), ntohs(serv.sin_port));

int on = 1;
setsockopt(sfp, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on));
bzero(&s_add, sizeof(struct sockaddr_in));
s_add.sin_family = AF_INET;
s_add.sin_addr.s_addr = inet_addr("192.168.0.2");
s_add.sin_port = htons(portnum);
if(-1 == bind(sfp, (struct sockaddr *)(&s_add), sizeof(struct sockaddr))){
     printf("bind fail: %d!\r\n", errno);
     return -1;
}
printf("bind ok !\r\n");
getsockname(sfp, (struct sockaddr *)&serv, &serv_len );
printf("ip = %s, port = %d\r\n", inet_ntoa(serv.sin_addr), ntohs(serv.sin_port));
return 0;
return 0;

}
/*
wannian07@wannian07-PC:~$ g++ -std=c++17 -o c13 c13.cpp
wannian07@wannian07-PC:~$ ./c13
ia.s_addr = 0x60210ac
real_ip = 172.16.2.6

socket ok !
ip = 0.0.0.0, port = 256
bind fail: 99!

*/

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值