<?xml version="1.0" encoding="utf-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom">
	<channel>
		<title><![CDATA[Форум компьютерной помощи &mdash; Средства синхронизации потоков, критические секции и тупики]]></title>
		<link>https://itpmr.ru/viewtopic.php?id=751</link>
		<atom:link href="https://itpmr.ru/extern.php?action=feed&amp;tid=751&amp;type=rss" rel="self" type="application/rss+xml" />
		<description><![CDATA[Недавние сообщения в теме «Средства синхронизации потоков, критические секции и тупики».]]></description>
		<lastBuildDate>Mon, 24 Dec 2012 11:47:54 +0000</lastBuildDate>
		<generator>PunBB</generator>
		<item>
			<title><![CDATA[Средства синхронизации потоков, критические секции и тупики]]></title>
			<link>https://itpmr.ru/viewtopic.php?pid=123794#p123794</link>
			<description><![CDATA[<p>Задание: написать приложение, увеличивающее значение глобальной переменной с помощью двух потоков и возможностью установления приоритета для этих потоков.<br />Код программы:<br />using System;<br />using System.Collections.Generic;<br />using System.ComponentModel;<br />using System.Data;<br />using System.Drawing;<br />using System.Linq;<br />using System.Text;<br />using System.Windows.Forms;<br />using System.Threading; // содержит классы и интерфейсы, которые дают возможность программировать в многопоточном режиме</p><p>namespace Laba4TVP<br />{<br />&nbsp; &nbsp; public partial class Form1 : Form<br />&nbsp; &nbsp; {<br />&nbsp; &nbsp; &nbsp; &nbsp; private static Mutex mutex = new Mutex(); // примитив синхронизации, который также может использоваться в межпроцессорной синхронизации<br />&nbsp; &nbsp; &nbsp; &nbsp; private static int global = 100;<br />&nbsp; &nbsp; &nbsp; &nbsp; public static string str1 = &quot;Значения из потока №1:&quot;;<br />&nbsp; &nbsp; &nbsp; &nbsp; public static string str2 = &quot;Значения из потока №2:&quot;;<br />&nbsp; &nbsp; &nbsp; &nbsp; public static void Runner1()<br />&nbsp; &nbsp; &nbsp; &nbsp; {<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; mutex.WaitOne(); // блокирует текущий поток<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; for (int i = 0; i &lt; 6; i++)<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; str1 += &quot; &quot; + global++;<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; mutex.ReleaseMutex(); // освобождает объект Mutex один раз<br />&nbsp; &nbsp; &nbsp; &nbsp; }<br />&nbsp; &nbsp; &nbsp; &nbsp; public static void Runner2()<br />&nbsp; &nbsp; &nbsp; &nbsp; {<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; for (int i = 0; i &lt; 6; i++)<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; str2 += &quot; &quot; + global++;<br />&nbsp; &nbsp; &nbsp; &nbsp; }<br />&nbsp; &nbsp; &nbsp; &nbsp; public Form1()<br />&nbsp; &nbsp; &nbsp; &nbsp; {<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; InitializeComponent();<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; CenterToScreen();<br />&nbsp; &nbsp; &nbsp; &nbsp; }</p><p>&nbsp; &nbsp; &nbsp; &nbsp; private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)<br />&nbsp; &nbsp; &nbsp; &nbsp; {<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; string select = comboBox1.SelectedItem.ToString();<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (select == &quot;Lowest&quot;)<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; label3.Text = &quot;Выполнение потока может быть \nзапланировано после выполнения \nпотоков с любыми другими \nприоритетами.&quot;;<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (select == &quot;BelowNormal&quot;)<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; label3.Text = &quot;Выполнение потока может быть \nзапланировано после выполнения \nпотоков с приоритетом Normal \nи до потоков с приоритетом Lowest.&quot;;<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (select == &quot;Normal&quot;)<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; label3.Text = &quot;Выполнение потока может быть \nзапланировано после выполнения \nпотоков с приоритетом AboveNormal \nи до потоков с приоритетом \nBelowNormal. По умолчанию потоки \nимеют приоритет Normal.&quot;;<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (select == &quot;AboveNormal&quot;)<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; label3.Text = &quot;Выполнение потока может быть \nзапланировано после выполнения \nпотоков с приоритетом Highest \nи до потоков с приоритетом Normal.&quot;;<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (select == &quot;Highest&quot;)<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; label3.Text = &quot;Выполнение потока может быть \nзапланировано до выполнения \nпотоков с любыми другими \nприоритетами.&quot;;<br />&nbsp; &nbsp; &nbsp; &nbsp; }</p><p>&nbsp; &nbsp; &nbsp; &nbsp; private void comboBox2_SelectedIndexChanged(object sender, EventArgs e)<br />&nbsp; &nbsp; &nbsp; &nbsp; {<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; string select = comboBox2.SelectedItem.ToString();<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (select == &quot;Lowest&quot;)<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; label4.Text = &quot;Выполнение потока может быть \nзапланировано после выполнения \nпотоков с любыми другими \nприоритетами.&quot;;<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (select == &quot;BelowNormal&quot;)<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; label4.Text = &quot;Выполнение потока может быть \nзапланировано после выполнения \nпотоков с приоритетом Normal \nи до потоков с приоритетом Lowest.&quot;;<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (select == &quot;Normal&quot;)<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; label4.Text = &quot;Выполнение потока может быть \nзапланировано после выполнения \nпотоков с приоритетом AboveNormal \nи до потоков с приоритетом \nBelowNormal. По умолчанию потоки \nимеют приоритет Normal.&quot;;<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (select == &quot;AboveNormal&quot;)<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; label4.Text = &quot;Выполнение потока может быть \nзапланировано после выполнения \nпотоков с приоритетом Highest \nи до потоков с приоритетом Normal.&quot;;<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (select == &quot;Highest&quot;)<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; label4.Text = &quot;Выполнение потока может быть \nзапланировано до выполнения \nпотоков с любыми другими \nприоритетами.&quot;;<br />&nbsp; &nbsp; &nbsp; &nbsp; }</p><p>&nbsp; &nbsp; &nbsp; &nbsp; private void button1_Click(object sender, EventArgs e)<br />&nbsp; &nbsp; &nbsp; &nbsp; {<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ThreadStart runner1 = new ThreadStart(Runner1);<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ThreadStart runner2 = new ThreadStart(Runner2);<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Thread th1 = new Thread(runner1);<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Thread th2 = new Thread(runner2);<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; string select1 = comboBox1.SelectedItem.ToString();<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; string select2 = comboBox2.SelectedItem.ToString();<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (select1 == &quot;Lowest&quot;)<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; th1.Priority = ThreadPriority.Lowest;<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (select1 == &quot;BelowNormal&quot;)<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; th1.Priority = ThreadPriority.BelowNormal;<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (select1 == &quot;Normal&quot;)<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; th1.Priority = ThreadPriority.Normal;<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (select1 == &quot;AboveNormal&quot;)<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; th1.Priority = ThreadPriority.AboveNormal;<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (select1 == &quot;Highest&quot;)<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; th1.Priority = ThreadPriority.Highest;<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (select2 == &quot;Lowest&quot;)<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; th2.Priority = ThreadPriority.Lowest;<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (select2 == &quot;BelowNormal&quot;)<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; th2.Priority = ThreadPriority.BelowNormal;<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (select2 == &quot;Normal&quot;)<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; th2.Priority = ThreadPriority.Normal;<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (select2 == &quot;AboveNormal&quot;)<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; th2.Priority = ThreadPriority.AboveNormal;<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (select2 == &quot;Highest&quot;)<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; th2.Priority = ThreadPriority.Highest;<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; th1.Start(); label5.Text = str1;<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; th2.Start(); label6.Text = str2;<br />&nbsp; &nbsp; &nbsp; &nbsp; }<br />&nbsp; &nbsp; }<br />}</p>]]></description>
			<author><![CDATA[null@example.com (admin)]]></author>
			<pubDate>Mon, 24 Dec 2012 11:47:54 +0000</pubDate>
			<guid>https://itpmr.ru/viewtopic.php?pid=123794#p123794</guid>
		</item>
	</channel>
</rss>
