c语言线程终止练习示例_C 语言

复制代码 代码如下:

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

void *t1(void *args) {
 return (void *) 0;
}

void *t2(void *args) {
 printf("thread 2 param[args] = %d\n", args);
 pthread_exit((void *) 3);
}

void *t3(void *args) {
 while(1) {
  printf("thread 3 is working\n");
  sleep(1);
 }
}

int main(int argc, char *argv[]) {
 pthread_t thread;
 int err;
 void *status;

 printf("creating thread 1\n");
 err = pthread_create(&thread, NULL, t1, NULL);
 if(err) {
  printf("Can not created thread 1\n");
  exit(-1);
 }
 pthread_join(thread, &status);
 printf("thread 1 exit return code %d\n\n", status);
 

 printf("creating thread 2\n");
 err = pthread_create(&thread, NULL, t2, (void *) 9);
 if(err) {
  printf("Can not created thread 2\n");
  exit(-2);
 }
 pthread_join(thread, &status);
 printf("thread 2 exit return code %d\n\n", status);

  
 printf("creating thread 3\n");
 err = pthread_create(&thread, NULL, t3, NULL);
 if(err) {
  printf("Can not created thread 3\n");
  exit(-3);
 }
 sleep(10);
 pthread_cancel(thread);
 pthread_join(thread, &status);
 printf("thread 3 exit return code %d\n", status);

 return 1;
}

时间: 2024-08-31 20:51:44

c语言线程终止练习示例_C 语言的相关文章

C语言socket编程开发应用示例_C 语言

实现步骤: 1. Server端 复制代码 代码如下: #include <stdio.h>                   //用于printf等函数的调用#include <winsock2.h>                //Socket的函数调用 #pragma comment (lib, "ws2_32")      //C语言引用其他类库时,除了.h文件外,还要加入对应的lib文件(这个不同于C#) 复制代码 代码如下: int main()

c语言strftime时间格式化示例_C 语言

函数原型: 复制代码 代码如下: size_t strftime (char* ptr, size_t maxsize, const char* format,const struct tm* timeptr ); 代码示例: 复制代码 代码如下: #include <stdio.h>#include <time.h> int main (){    time_t rawtime;    struct tm * timeinfo;    char buffer [128];    

利用c语言实现卷积码编码器示例_C 语言

实现(2, 1, 7)卷积码编码信息序列1001 1010 1111 1100生成序列g1 = 1011011;g2 = 1111001初始状态全0.以上参数可自行在main中修改. 复制代码 代码如下: /***This is an simple example program of convolutional encoder.   *The information sequence, the register initial states and the generation sequenc

c语言求阶乘精确值示例_C 语言

复制代码 代码如下: #include <stdio.h>#include <string.h>const int maxn = 3000;int f[maxn];int main(){ int i,j,n; scanf("%d",&n); memset(f,0,sizeof(f)); f[0] = 1; for(i = 2;i <= n;i++) {  int c = 0;  for(j = 0;j < maxn;j++)  {   int

c语言多进程tcp服务器示例_C 语言

server.h 复制代码 代码如下: #ifndef SERVER_H#define SERVER_H#include <sys/types.h>#include <sys/socket.h>#include <netinet/in.h>#include <stdio.h>#include <stdlib.h>#include <unistd.h>#include <arpa/inet.h>#include <as

c语言可变参数实现示例_C 语言

这段代码展示了如何不使用<stdarg.h>中的va_list.va_start.va_end宏来实现自定义可变参数以及如何改变默认的%d.%f.%s等格式字符. 复制代码 代码如下: #include <stdio.h>#include <stdlib.h> // itoa() and ltoa()#include <string.h> // strcat() and strlen() // echo("$i, $s, $l, $c",

c语言多线程编程使用示例_C 语言

复制代码 代码如下: #include <stdio.h>#include <stdlib.h>#include <pthread.h> #define THREAD_NUM 10 void *test(void *args) { printf("tid %d: i say 'Hello'.\n", args); return NULL;} int main() { int i, err; pthread_t child[THREAD_NUM];  

c语言clock函数使用示例_C 语言

clock_t clock( void );Calculates the processor time used by the calling processhead file is <time.h> Return Valueclock returns the number of clock ticks of elapsed processor time. The returned value is the product of the amount of time that has elap

c++版线程池和任务池示例_C 语言

commondef.h 复制代码 代码如下: //单位秒,监测空闲列表时间间隔,在空闲队列中超过TASK_DESTROY_INTERVAL时间的任务将被自动销毁const int CHECK_IDLE_TASK_INTERVAL = 300;//单位秒,任务自动销毁时间间隔const int TASK_DESTROY_INTERVAL = 60; //监控线程池是否为空时间间隔,微秒const int IDLE_CHECK_POLL_EMPTY = 500; //线程池线程空闲自动退出时间间隔