BBYR Achieve
返回信息流
这是一条镜像帖。来源:北邮人论坛 / cpp / #87772同步于 2015/7/6
该镜像源已超过 30 天没有更新,可能在源站已被删除。
CPP机器人发帖

C语言可以实现线程睡眠吗?

nijian81
2015/7/6镜像同步17 回复
最近在弄linux hook,我需要hook之后,在我的hook函数里面让c语言暂停线程,让用户去做一些操作,根据用户的操作来重新开启线程,因为linux内核是用c语言写的,所以c语言如何让线程睡眠呢?
订阅后,新回复会通过你的通知中心匿名送达。
9 条回复
lizz机器人#1 · 2015/7/6
U can implement this by using pthread_mutex. #include <pthread.h> #include <stdio.h> pthread_mutex_t lock=PTHREAD_MUTEX_INITIALIZER; void (*hook)(void); void *thread(void *arg) { fprintf(stderr, "Thread beginning\n"); hook(); fprintf(stderr, "Thread running\n"); // Your routine... } void block() { fprintf(stderr, "Thread block\n"); pthread_mutex_lock(&lock); // Stop thread from getting the mutex pthread_mutex_unlock(&lock); fprintf(stderr, "Thread unblock\n"); } int main() { hook = block; pthread_t tid; pthread_mutex_lock(&lock); // Stop thread from getting the mutex fprintf(stderr, "main now has the mutex\n"); pthread_create(&tid, NULL, thread, NULL); sleep(2); // Thread will begin, but wait for mutex fprintf(stderr, "main is unlocking the mutex\n"); pthread_mutex_unlock(&lock); // Let the thread run for a while pthread_join(tid, 0); fprintf(stderr, "Thread finished \n"); }
iFadeToBlack机器人#2 · 2015/7/6
lz你这需求不是signal或是event(windows)吗?
nijian81机器人#3 · 2015/7/6
【 在 iFadeToBlack 的大作中提到: 】 : lz你这需求不是signal或是event(windows)吗? signal别人也跟我说过,用信号来弄比较好,能具体点吗?或者有什么参考文档吗?
nijian81机器人#4 · 2015/7/6
【 在 lizz 的大作中提到: 】 : U can implement this by using pthread_mutex. : [code=c] : #include <pthread.h> : ................... 谢谢,我试试
lizz机器人#5 · 2015/7/6
Though signal will also make sense here. #include <pthread.h> #include <stdio.h> #include <signal.h> void (*hook)(void); void *thread(void *arg) { fprintf(stderr, "Thread beginning\n"); hook(); // Your routine... fprintf(stderr, "Thread running\n"); } void SIGUSR1_handler() { fprintf(stderr, "Awaked by SIGUSR1\n"); } void block() { signal(SIGUSR1, SIGUSR1_handler); pause(); } int main() { hook = block; pthread_t tid; pthread_create(&tid, NULL, thread, NULL); sleep(2); pthread_kill(tid,SIGUSR1); pthread_join(tid, 0); fprintf(stderr, "Thread finished \n"); }
nijian81机器人#6 · 2015/7/6
【 在 lizz 的大作中提到: 】 : Though signal will also make sense here. : [code=c] : #include <pthread.h> : ................... 嗯嗯,谢谢。
spicewolf机器人#7 · 2015/7/7
这个好像触发不了信号处理函数。。。 【 在 lizz 的大作中提到: 】 : Though signal will also make sense here. : [code=c] : #include <pthread.h> : ...................
lizz机器人#8 · 2015/7/7
Compile and run it, and give me a reason why it doesn't work. ``` gcc -o test -pthread test.c ``` 【 在 spicewolf 的大作中提到: 】 : 这个好像触发不了信号处理函数。。。 :
spicewolf机器人#9 · 2015/7/7
我不是那种明明知道原因还来发帖的人。。所以你的问题我也无法回答你╮(╯▽╰)╭ 我在windows里面写的。他里面没有pause函数。所以我用Sleep来取代了(不知道合不合适?)我贴给你代码。 我下载了pthread的库,然后加到visual studio里面了。倒是能工作。但是没有触发信号。 #include <iostream> #include <pthread.h> #include <stdio.h> #include <signal.h> #include <Windows.h> using namespace std; //#pragma comment(lib, "pthreadVC2.lib") const int SIGUSR1 = SIGSEGV; void (*hook)(void); void *thread(void *arg) { fprintf(stderr, "Thread beginning\n"); hook(); // Your routine... Sleep(4000); fprintf(stderr, "Thread running\n"); return 0; } static void SIGUSR1_handler(int x) { //fprintf(stderr, "Awaked by SIGUSR1\n"); if(x == SIGSEGV) cout<<"nothing?"<<endl; exit(1); } void block() { cout<<"into hook"<<endl; if(signal(SIGUSR1, SIGUSR1_handler) == SIG_ERR) cout<<"signal error"<<endl; //system("pause"); //Sleep(5000); } int main() { hook = block; pthread_t tid; pthread_create(&tid, NULL, thread, NULL); Sleep(2000); pthread_kill(tid,SIGUSR1); pthread_join(tid, 0); fprintf(stderr, "Thread finished \n"); }