<?xml version="1.0" encoding="utf-8"?>
<feed xmlns="http://www.w3.org/2005/Atom">
	<title type="html"><![CDATA[Форум компьютерной помощи &mdash; Средства синхронизации потоков, критические секции и тупики]]></title>
	<link rel="self" href="https://itpmr.ru/extern.php?action=feed&amp;tid=751&amp;type=atom" />
	<updated>2012-12-24T11:47:54Z</updated>
	<generator>PunBB</generator>
	<id>https://itpmr.ru/viewtopic.php?id=751</id>
		<entry>
			<title type="html"><![CDATA[Средства синхронизации потоков, критические секции и тупики]]></title>
			<link rel="alternate" href="https://itpmr.ru/viewtopic.php?pid=123794#p123794" />
			<content type="html"><![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>]]></content>
			<author>
				<name><![CDATA[admin]]></name>
				<uri>https://itpmr.ru/profile.php?id=2</uri>
			</author>
			<updated>2012-12-24T11:47:54Z</updated>
			<id>https://itpmr.ru/viewtopic.php?pid=123794#p123794</id>
		</entry>
</feed>
