日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 运维知识 > linux >内容正文

linux

挂起某线程命令 Linux,linux 线程挂起恢复的简单示例

發布時間:2023/12/10 linux 39 豆豆
生活随笔 收集整理的這篇文章主要介紹了 挂起某线程命令 Linux,linux 线程挂起恢复的简单示例 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

參考:

寫了個demo:

#include

#include

static pthread_mutex_t mutex;

static pthread_cond_t cond;

static int flag = 0;

void srpthread_init()

{

pthread_mutex_init(&mutex,NULL);

pthread_cond_init(&cond,NULL);

}

void srpthread_suspend()

{

pthread_mutex_lock(&mutex);

flag--;

pthread_mutex_unlock(&mutex);

}

void srpthread_resume()

{

pthread_mutex_lock(&mutex);

flag++;

pthread_cond_signal(&cond);

pthread_mutex_unlock(&mutex);

}

void *thread_run()

{

while(1)

{

pthread_mutex_lock(&mutex);

while(flag<=0)

{

pthread_cond_wait(&cond,&mutex);

}

pthread_mutex_unlock(&mutex);

//actual work

printf("i am running!\n");

}

}

int main(int argc,char *argv[])

{

char ch;

pthread_t p1;

srpthread_init();

pthread_create(&p1,NULL,(void *)thread_run,NULL);

while(1)

{

scanf("%c",&ch);

switch(ch)

{

case 's':

srpthread_suspend();

break;

case 'r':

srpthread_resume();

break;

default:

break;

}

}

#if 0

printf("1111\n");

srpthread_resume();

printf("2222\n");

sleep(3);

printf("3333\n");

srpthread_suspend();

printf("4444\n");

#endif

return 1;

}

編譯命令: gcc file.c -o file lpthread

總結

以上是生活随笔為你收集整理的挂起某线程命令 Linux,linux 线程挂起恢复的简单示例的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。