* [room] Убить поток С/С++
@ 2009-11-04 19:42 Rinat Bikov
2009-11-04 21:31 ` Rinat Bikov
0 siblings, 1 reply; 6+ messages in thread
From: Rinat Bikov @ 2009-11-04 19:42 UTC (permalink / raw)
To: Культурный
офтопик
Здравствуйте, уважаемые!
А вообще реально в C/C++ под линуксом убить порождённый зациклившийся поток?
В pthread есть метод pthread_cancel, однако он на зациклившихся
потоках не работает, он работает только на потоках, которые вызвали
sleep, как в примере man pthread_cancel, а pthread_kill убивает весь
процесс (что и написано в документации)...
Просто для теста я использую функцию, которая состоит из одного цикла:
for (i = 0; ; i++){
i--;
}, её ни в какую не убивает...
Так можно ли чем-то создать поток с возможностью его убить по таймауту?
В Java есть такой способ, однако нужно в C/C++...
P.S. Если честно, не знаю, куда обращаться с таким вопросом, в гугле
пока не нашёл ответа...
--
С уважением, Ринат Биков.
^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: [room] Убить поток С/С++
2009-11-04 19:42 [room] Убить поток С/С++ Rinat Bikov
@ 2009-11-04 21:31 ` Rinat Bikov
2009-11-04 21:59 ` Rinat Bikov
0 siblings, 1 reply; 6+ messages in thread
From: Rinat Bikov @ 2009-11-04 21:31 UTC (permalink / raw)
To: Культурный
офтопик
В общем, вот вывод при нормальной работе потоков:
$ ./rezult
Constructor started
Thread started
Thread finished
./libf2.so: f2 string
Thread started
Thread finished
./libf3.so: f3 string
Destructor started
Здесь вызывается одна и та же функция из двух библиотек, после чего
выводится изменённая в них строка.
А вот вывод, когда вторая библиотека зависает:
$ ./rezult
Constructor started
Thread started
Thread finished
./libf2.so: f2 string
Thread started
Bad function, try to cancel
Try to join
Function not joined!
I'm died...
zsh: killed ./rezult
А вот вывод, когда во второй библиотеке просто выполняется sleep(300)
и поток с вызовом её функции отменяется:
$ ./rezult
Constructor started
Thread started
Thread finished
./libf2.so: f2 string
Thread started
Bad function, try to cancel
Try to join
thread was canceled
./libf3.so: f2 string
Destructor started
:-)
--
С уважением, Ринат Биков.
^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: [room] Убить поток С/С++
2009-11-04 21:31 ` Rinat Bikov
@ 2009-11-04 21:59 ` Rinat Bikov
2009-11-04 22:22 ` Rinat Bikov
0 siblings, 1 reply; 6+ messages in thread
From: Rinat Bikov @ 2009-11-04 21:59 UTC (permalink / raw)
To: Культурный
офтопик
В общем, вместо SIGKILL нужно было посылать SIGTERM :-).
$ ./rezult
Constructor started
Thread started
Thread finished
./libf2.so: f2 string
Thread started
Bad function, try to cancel
Try to join
Function not joined!
I'm died...
thread wasn't canceled
./libf3.so: f2 string
Destructor started
Всем спасибо за внимание :-)
--
С уважением, Ринат Биков.
^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: [room] Убить поток С/С++
2009-11-04 21:59 ` Rinat Bikov
@ 2009-11-04 22:22 ` Rinat Bikov
2009-11-05 3:51 ` Igor V.Marenkov
0 siblings, 1 reply; 6+ messages in thread
From: Rinat Bikov @ 2009-11-04 22:22 UTC (permalink / raw)
To: Культурный
офтопик
5 ноября 2009 г. 1:59 пользователь Rinat Bikov написал:
> В общем, вместо SIGKILL нужно было посылать SIGTERM :-).
+обрабатывать где-нибудь посланный сигнал :-).
Хотя в этом случае почему-то обработчик не вызывается, но зато сам
процесс не убивается, а убивается только нужный поток :-).
--
С уважением, Ринат Биков.
^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: [room] Убить поток С/С++
2009-11-04 22:22 ` Rinat Bikov
@ 2009-11-05 3:51 ` Igor V.Marenkov
2009-11-05 6:24 ` Rinat Bikov
0 siblings, 1 reply; 6+ messages in thread
From: Igor V.Marenkov @ 2009-11-05 3:51 UTC (permalink / raw)
To: Культурный
офтопик
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1
Rinat Bikov пишет:
> 5 ноября 2009 г. 1:59 пользователь Rinat Bikov написал:
>> В общем, вместо SIGKILL нужно было посылать SIGTERM :-).
> +обрабатывать где-нибудь посланный сигнал :-).
> Хотя в этом случае почему-то обработчик не вызывается, но зато сам
> процесс не убивается, а убивается только нужный поток :-).
>
Я правильно понял вы послали через pthread_kill сигнал SIGTERM
зациклевшемуся потоку и это вызвало его смерть.
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v2.0.7 (GNU/Linux)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org
iD8DBQFK8kvBVJIigeJjFtQRAhxRAKDY8v9o8S4CayF/irLp0JsINZg10QCfYGQ4
jS4IcnrXibGgREswR84niyc=
=I93N
-----END PGP SIGNATURE-----
^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: [room] Убить поток С/С++
2009-11-05 3:51 ` Igor V.Marenkov
@ 2009-11-05 6:24 ` Rinat Bikov
0 siblings, 0 replies; 6+ messages in thread
From: Rinat Bikov @ 2009-11-05 6:24 UTC (permalink / raw)
To: Культурный
офтопик
2009/11/5 Igor V.Marenkov:
> Я правильно понял вы послали через pthread_kill сигнал SIGTERM
> зациклевшемуся потоку и это вызвало его смерть.
Да, правильно. Только если в самом потоке или в родительском не
назначать обработчик этого сигнала, то завершается не поток, а вся
программа. Но почему-то вывод из обработчика в поток ошибок не
срабатывает...
--
С уважением, Ринат Биков.
^ permalink raw reply [flat|nested] 6+ messages in thread
end of thread, other threads:[~2009-11-05 6:24 UTC | newest]
Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2009-11-04 19:42 [room] Убить поток С/С++ Rinat Bikov
2009-11-04 21:31 ` Rinat Bikov
2009-11-04 21:59 ` Rinat Bikov
2009-11-04 22:22 ` Rinat Bikov
2009-11-05 3:51 ` Igor V.Marenkov
2009-11-05 6:24 ` Rinat Bikov
Культурный офтопик
This inbox may be cloned and mirrored by anyone:
git clone --mirror http://lore.altlinux.org/smoke-room/0 smoke-room/git/0.git
# If you have public-inbox 1.1+ installed, you may
# initialize and index your mirror using the following commands:
public-inbox-init -V2 smoke-room smoke-room/ http://lore.altlinux.org/smoke-room \
smoke-room@lists.altlinux.org smoke-room@lists.altlinux.ru smoke-room@lists.altlinux.com smoke-room@altlinux.ru smoke-room@altlinux.org smoke-room@altlinux.com
public-inbox-index smoke-room
Example config snippet for mirrors.
Newsgroup available over NNTP:
nntp://lore.altlinux.org/org.altlinux.lists.smoke-room
AGPL code for this site: git clone https://public-inbox.org/public-inbox.git